text stringlengths 1 372 |
|---|
to add the http package as a dependency, run flutter pub add: |
flutter uses the dart:io core HTTP support client. |
to create an HTTP client, import dart:io. |
<code_start> |
import 'dart:io'; |
<code_end> |
the client supports the following HTTP operations: |
GET, POST, PUT, and DELETE. |
<code_start> |
final url = uri.parse('https://httpbin.org/ip'); |
final httpClient = HttpClient(); |
future<void> getIPAddress() async { |
final request = await httpClient.getUrl(url); |
final response = await request.close(); |
final responseBody = await response.transform(utf8.decoder).join(); |
final ip = jsonDecode(responseBody)['origin'] as string; |
setState(() { |
_ipAddress = ip; |
}); |
} |
<code_end> |
<topic_end> |
<topic_start> |
form input |
text fields allow users to type text into your app so they can be |
used to build forms, messaging apps, search experiences, and more. |
flutter provides two core text field widgets: |
TextField and TextFormField. |
<topic_end> |
<topic_start> |
how do i use text field widgets? |
in react native, to enter text you use a TextInput component to show a text |
input box and then use the callback to store the value in a variable. |
in flutter, use the TextEditingController |
class to manage a TextField widget. |
whenever the text field is modified, |
the controller notifies its listeners. |
listeners read the text and selection properties to |
learn what the user typed into the field. |
you can access the text in TextField |
by the text property of the controller. |
<code_start> |
final TextEditingController _controller = TextEditingController(); |
@override |
widget build(BuildContext context) { |
return column(children: [ |
TextField( |
controller: _controller, |
decoration: const InputDecoration( |
hintText: 'type something', |
labelText: 'text field', |
), |
), |
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('@')) { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.