text stringlengths 1 474 |
|---|
super.key, |
required this.text, |
}); |
final String text; |
@override |
Widget build(BuildContext context) { |
return Center( |
child: Text( |
text, |
textDirection: TextDirection.ltr, |
), |
); |
} |
}<code_end> |
The previous example uses the constructor of the MyStatelessWidget |
class to pass the text, which is marked as final. |
This class extends StatelessWidget—it contains immutable data.The build method of a stateless widget is typically called |
in only three situations:<topic_end> |
<topic_start> |
The StatefulWidget |
A StatefulWidget is a widget that changes state. |
Use the setState method to manage the |
state changes for a StatefulWidget. |
A call to setState() tells the Flutter |
framework that something has changed in a state, |
which causes an app to rerun the build() method |
so that the app can reflect the change.State is information that can be read synchronously when a widget |
is built and might change during the lifetime of the widget. |
It’s the responsibility of the widget implementer to ensure that |
the state object is promptly notified when the state changes. |
Use StatefulWidget when a widget can change dynamically. |
For example, the state of the widget changes by typing into a form, |
or moving a slider. |
Or, it can change over time—perhaps a data feed updates the UI.Checkbox, Radio, Slider, InkWell, |
Form, and TextField |
are examples of stateful widgets that subclass |
StatefulWidget.The following example declares a StatefulWidget |
that requires a createState() method. |
This method creates the state object that manages the widget’s state, |
_MyStatefulWidgetState. |
<code_start>class MyStatefulWidget extends StatefulWidget { |
const MyStatefulWidget({ |
super.key, |
required this.title, |
}); |
final String title; |
@override |
State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); |
}<code_end> |
The following state class, _MyStatefulWidgetState, |
implements the build() method for the widget. |
When the state changes, for example, when the user toggles |
the button, setState() is called with the new toggle value. |
This causes the framework to rebuild this widget in the UI. |
<code_start>class _MyStatefulWidgetState extends State<MyStatefulWidget> { |
bool showText = true; |
bool toggleState = true; |
Timer? t2; |
void toggleBlinkState() { |
setState(() { |
toggleState = !toggleState; |
}); |
if (!toggleState) { |
t2 = Timer.periodic(const Duration(milliseconds: 1000), (t) { |
toggleShowText(); |
}); |
} else { |
t2?.cancel(); |
} |
} |
void toggleShowText() { |
setState(() { |
showText = !showText; |
}); |
} |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
body: Center( |
child: Column( |
children: <Widget>[ |
if (showText) |
const Text( |
'This execution will be done before you can blink.', |
), |
Padding( |
padding: const EdgeInsets.only(top: 70), |
child: ElevatedButton( |
onPressed: toggleBlinkState, |
child: toggleState |
? const Text('Blink') |
: const Text('Stop Blinking'), |
), |
), |
], |
), |
), |
); |
} |
}<code_end> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.