text stringlengths 1 474 |
|---|
} |
// 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(), |
), |
);<code_end> |
<topic_end> |
<topic_start> |
2. Display a SnackBar |
With the Scaffold in place, display a SnackBar. |
First, create a SnackBar, then display it using ScaffoldMessenger. |
<code_start>const snackBar = SnackBar( |
content: Text('Yay! A SnackBar!'), |
); |
// Find the ScaffoldMessenger in the widget tree |
// and use it to show a SnackBar. |
ScaffoldMessenger.of(context).showSnackBar(snackBar);<code_end> |
info Note |
To learn more, watch this short Widget of the Week video on the ScaffoldMessenger widget:<topic_end> |
<topic_start> |
3. Provide an optional action |
You might want to provide an action to the user when |
the SnackBar is displayed. |
For example, if the user accidentally deletes a message, |
they might use an optional action in the SnackBar to recover |
the message.Here’s an example of providing |
an additional action to the SnackBar widget: |
<code_start>final snackBar = SnackBar( |
content: const Text('Yay! A SnackBar!'), |
action: SnackBarAction( |
label: 'Undo', |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.