text stringlengths 1 372 |
|---|
widget build(BuildContext context) { |
const appTitle = 'form validation demo'; |
return MaterialApp( |
title: appTitle, |
home: scaffold( |
appBar: AppBar( |
title: const Text(appTitle), |
), |
body: const MyCustomForm(), |
), |
); |
} |
} |
// create a form widget. |
class MyCustomForm extends StatefulWidget { |
const MyCustomForm({super.key}); |
@override |
MyCustomFormState createState() { |
return MyCustomFormState(); |
} |
} |
// create a corresponding state class. |
// this class holds data related to the form. |
class MyCustomFormState extends State<MyCustomForm> { |
// create a global key that uniquely identifies the form widget |
// and allows validation of the form. |
// |
// note: this is a GlobalKey<FormState>, |
// not a GlobalKey<MyCustomFormState>. |
final _formKey = GlobalKey<FormState>(); |
@override |
widget build(BuildContext context) { |
// build a form widget using the _formKey created above. |
return form( |
key: _formKey, |
child: column( |
crossAxisAlignment: CrossAxisAlignment.start, |
children: [ |
TextFormField( |
// the validator receives the text that the user has entered. |
validator: (value) { |
if (value == null || value.isEmpty) { |
return 'please enter some text'; |
} |
return null; |
}, |
), |
padding( |
padding: const EdgeInsets.symmetric(vertical: 16), |
child: ElevatedButton( |
onPressed: () { |
// validate returns true if the form is valid, or false otherwise. |
if (_formkey.currentstate!.validate()) { |
// if the form is valid, display a snackbar. in the real world, |
// you'd often call a server or save the information in a database. |
ScaffoldMessenger.of(context).showSnackBar( |
const SnackBar(content: Text('Processing data')), |
); |
} |
}, |
child: const Text('Submit'), |
), |
), |
], |
), |
); |
} |
} |
<code_end> |
to learn how to retrieve these values, check out the |
retrieve the value of a text field recipe. |
<topic_end> |
<topic_start> |
display a snackbar |
it can be useful to briefly inform your users when certain actions |
take place. for example, when a user swipes away a message in a list, |
you might want to inform them that the message has been deleted. |
you might even want to give them an option to undo the action. |
in material design, this is the job of a SnackBar. |
this recipe implements a snackbar using the following steps: |
<topic_end> |
<topic_start> |
1. create a scaffold |
when creating apps that follow the material design guidelines, |
give your apps a consistent visual structure. |
in this example, display the SnackBar at the bottom of the screen, |
without overlapping other important |
widgets, such as the FloatingActionButton. |
the scaffold widget, from the material library, |
creates this visual structure and ensures that important |
widgets don’t overlap. |
<code_start> |
return MaterialApp( |
title: 'snackbar demo', |
home: scaffold( |
appBar: AppBar( |
title: const Text('SnackBar demo'), |
), |
body: const SnackBarPage(), |
), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.