text
stringlengths
1
474
<topic_end>
<topic_start>
Gesture detection and touch event handling
To listen for and respond to gestures,
Flutter supports taps, drags, and scaling.
The gesture system in Flutter has two separate layers.
The first layer includes raw pointer events,
which describe the location and movement of pointers,
(such as touches, mice, and styli movements), across the screen.
The second layer includes gestures,
which describe semantic actions
that consist of one or more pointer movements.<topic_end>
<topic_start>
How do I add a click or press listeners to a widget?
In React Native, listeners are added to components
using PanResponder or the Touchable components.For more complex gestures and combining several touches into
a single gesture, PanResponder is used.In Flutter, to add a click (or press) listener to a widget,
use a button or a touchable widget that has an onPress: field.
Or, add gesture detection to any widget by wrapping it
in a GestureDetector.
<code_start>@override
Widget build(BuildContext context) {
return GestureDetector(
child: Scaffold(
appBar: AppBar(title: const Text('Gestures')),
body: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Tap, Long Press, Swipe Horizontally or Vertically'),
],
)),
),
onTap: () {
print('Tapped');
},
onLongPress: () {
print('Long Pressed');
},
onVerticalDragEnd: (value) {
print('Swiped Vertically');
},
onHorizontalDragEnd: (value) {
print('Swiped Horizontally');
},
);
}<code_end>
For more information, including a list of
Flutter GestureDetector callbacks,
see the GestureDetector class.<topic_end>
<topic_start>
Making HTTP network requests
Fetching data from the internet is common for most apps. And in Flutter,
the http package provides the simplest way to fetch data from the internet.<topic_end>
<topic_start>
How do I fetch data from API calls?
React Native provides the Fetch API for networking—you make a fetch request
and then receive the response to get the data.Flutter uses the http package.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',
),