| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'package:flutter/material.dart'; |
| 6 | |
| 7 | /// Flutter code sample for [InputDecorator]. |
| 8 | |
| 9 | void main() => runApp(const LabelStyleErrorExampleApp()); |
| 10 | |
| 11 | class LabelStyleErrorExampleApp extends StatelessWidget { |
| 12 | const LabelStyleErrorExampleApp({super.key}); |
| 13 | |
| 14 | @override |
| 15 | Widget build(BuildContext context) { |
| 16 | return MaterialApp( |
| 17 | home: Scaffold( |
| 18 | appBar: AppBar(title: const Text('InputDecorator Sample' )), |
| 19 | body: const Center(child: InputDecoratorExample()), |
| 20 | ), |
| 21 | ); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | class InputDecoratorExample extends StatelessWidget { |
| 26 | const InputDecoratorExample({super.key}); |
| 27 | |
| 28 | @override |
| 29 | Widget build(BuildContext context) { |
| 30 | return TextFormField( |
| 31 | decoration: InputDecoration( |
| 32 | border: const OutlineInputBorder(), |
| 33 | labelText: 'Name' , |
| 34 | // The WidgetStateProperty's value is a text style that is orange |
| 35 | // by default, but the theme's error color if the input decorator |
| 36 | // is in its error state. |
| 37 | labelStyle: WidgetStateTextStyle.resolveWith((Set<WidgetState> states) { |
| 38 | final Color color = states.contains(WidgetState.error) |
| 39 | ? Theme.of(context).colorScheme.error |
| 40 | : Colors.orange; |
| 41 | return TextStyle(color: color, letterSpacing: 1.3); |
| 42 | }), |
| 43 | ), |
| 44 | validator: (String? value) { |
| 45 | if (value == null || value == '' ) { |
| 46 | return 'Enter name' ; |
| 47 | } |
| 48 | return null; |
| 49 | }, |
| 50 | autovalidateMode: AutovalidateMode.always, |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | |