text
stringlengths 1
474
|
---|
),
|
ElevatedButton(
|
child: const Text('Submit'),
|
onPressed: () {
|
showDialog(
|
context: context,
|
builder: (context) {
|
return AlertDialog(
|
title: const Text('Alert'),
|
content: Text('You typed ${_controller.text}'),
|
);
|
});
|
},
|
),
|
]);
|
}<code_end>
|
In this example, when a user clicks on the submit button an alert dialog
|
displays the current text entered in the text field.
|
This is achieved using an AlertDialog
|
widget that displays the alert message, and the text from
|
the TextField is accessed by the text property of the
|
TextEditingController.<topic_end>
|
<topic_start>
|
How do I use Form widgets?
|
In Flutter, use the Form widget where
|
TextFormField widgets along with the submit
|
button are passed as children.
|
The TextFormField widget has a parameter called
|
onSaved that takes a callback and executes
|
when the form is saved. A FormState
|
object is used to save, reset, or validate
|
each FormField that is a descendant of this Form.
|
To obtain the FormState, you can use Form.of()
|
with a context whose ancestor is the Form,
|
or pass a GlobalKey to the Form constructor and call
|
GlobalKey.currentState().
|
<code_start>@override
|
Widget build(BuildContext context) {
|
return Form(
|
key: formKey,
|
child: Column(
|
children: <Widget>[
|
TextFormField(
|
validator: (value) {
|
if (value != null && value.contains('@')) {
|
return null;
|
}
|
return 'Not a valid email.';
|
},
|
onSaved: (val) {
|
_email = val;
|
},
|
decoration: const InputDecoration(
|
hintText: 'Enter your email',
|
labelText: 'Email',
|
),
|
),
|
ElevatedButton(
|
onPressed: _submit,
|
child: const Text('Login'),
|
),
|
],
|
),
|
);
|
}<code_end>
|
The following example shows how Form.save() and formKey
|
(which is a GlobalKey), are used to save the form on submit.
|
<code_start>void _submit() {
|
final form = formKey.currentState;
|
if (form != null && form.validate()) {
|
form.save();
|
showDialog(
|
context: context,
|
builder: (context) {
|
return AlertDialog(
|
title: const Text('Alert'),
|
content: Text('Email: $_email, password: $_password'));
|
},
|
);
|
}
|
}<code_end>
|
<topic_end>
|
<topic_start>
|
Platform-specific code
|
When building a cross-platform app, you want to re-use as much code as
|
possible across platforms. However, scenarios might arise where it
|
makes sense for the code to be different depending on the OS.
|
This requires a separate implementation by declaring a specific platform.In React Native, the following implementation would be used:In Flutter, use the following implementation:
|
<code_start>final platform = Theme.of(context).platform;
|
if (platform == TargetPlatform.iOS) {
|
return 'iOS';
|
}
|
if (platform == TargetPlatform.android) {
|
return 'android';
|
}
|
if (platform == TargetPlatform.fuchsia) {
|
return 'fuchsia';
|
}
|
return 'not recognized ';<code_end>
|
<topic_end>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.