text stringlengths 1 372 |
|---|
check out the cupertino components package, |
which has its own versions of |
CupertinoApp, and CupertinoNavigationBar. |
<topic_end> |
<topic_start> |
handling gestures |
most applications include some form of user interaction with the system. |
the first step in building an interactive application is to detect |
input gestures. see how that works by creating a simple button: |
<code_start> |
import 'package:flutter/material.dart'; |
class MyButton extends StatelessWidget { |
const MyButton({super.key}); |
@override |
widget build(BuildContext context) { |
return GestureDetector( |
onTap: () { |
print('MyButton was tapped!'); |
}, |
child: container( |
height: 50, |
padding: const EdgeInsets.all(8), |
margin: const EdgeInsets.symmetric(horizontal: 8), |
decoration: BoxDecoration( |
borderRadius: BorderRadius.circular(5), |
color: Colors.lightGreen[500], |
), |
child: const center( |
child: Text('Engage'), |
), |
), |
); |
} |
} |
void main() { |
runApp( |
const MaterialApp( |
home: scaffold( |
body: center( |
child: MyButton(), |
), |
), |
), |
); |
} |
<code_end> |
the GestureDetector widget doesn’t have a visual |
representation but instead detects gestures made by the |
user. when the user taps the container, |
the GestureDetector calls its onTap() callback, in this |
case printing a message to the console. you can use |
GestureDetector to detect a variety of input gestures, |
including taps, drags, and scales. |
many widgets use a GestureDetector to provide |
optional callbacks for other widgets. for example, the |
IconButton, ElevatedButton, and |
FloatingActionButton widgets have onPressed() |
callbacks that are triggered when the user taps the widget. |
for more information, check out gestures in flutter. |
<topic_end> |
<topic_start> |
changing widgets in response to input |
so far, this page has used only stateless widgets. |
stateless widgets receive arguments from their parent widget, |
which they store in final member variables. |
when a widget is asked to build(), it uses these stored |
values to derive new arguments for the widgets it creates. |
in order to build more complex experiences—for example, |
to react in more interesting ways to user input—applications |
typically carry some state. flutter uses StatefulWidgets to capture |
this idea. StatefulWidgets are special widgets that know how to generate |
state objects, which are then used to hold state. |
consider this basic example, using the ElevatedButton mentioned earlier: |
<code_start> |
import 'package:flutter/material.dart'; |
class counter extends StatefulWidget { |
// this class is the configuration for the state. |
// it holds the values (in this case nothing) provided |
// by the parent and used by the build method of the |
// state. fields in a widget subclass are always marked |
// "final". |
const counter({super.key}); |
@override |
State<Counter> createState() => _CounterState(); |
} |
class _CounterState extends State<Counter> { |
int _counter = 0; |
void _increment() { |
setState(() { |
// this call to setState tells the flutter framework |
// that something has changed in this state, which |
// causes it to rerun the build method below so that |
// the display can reflect the updated values. if you |
// change _counter without calling setState(), then |
// the build method won't be called again, and so |
// nothing would appear to happen. |
_counter++; |
}); |
} |
@override |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.