text stringlengths 1 474 |
|---|
<topic_end> |
<topic_start> |
What are the StatefulWidget and StatelessWidget best practices? |
Here are a few things to consider when designing your widget.In Flutter, widgets are either Stateful or Stateless—depending on whether |
they depend on a state change.In Flutter, there are three primary ways to manage state:When deciding which approach to use, consider the following principles:The MyStatefulWidget class manages its own state—it extends |
StatefulWidget, it overrides the createState() |
method to create the State object, |
and the framework calls createState() to build the widget. |
In this example, createState() creates an instance of |
_MyStatefulWidgetState, which |
is implemented in the next best practice. |
<code_start>class MyStatefulWidget extends StatefulWidget { |
const MyStatefulWidget({ |
super.key, |
required this.title, |
}); |
final String title; |
@override |
State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); |
} |
class _MyStatefulWidgetState extends State<MyStatefulWidget> { |
@override |
Widget build(BuildContext context) { |
//... |
} |
}<code_end> |
Add your custom StatefulWidget to the widget tree |
in the app’s build method. |
<code_start>class MyStatelessWidget extends StatelessWidget { |
// This widget is the root of your application. |
const MyStatelessWidget({super.key}); |
@override |
Widget build(BuildContext context) { |
return const MaterialApp( |
title: 'Flutter Demo', |
home: MyStatefulWidget(title: 'State Change Demo'), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start> |
Props |
In React Native, most components can be customized when they are |
created with different parameters or properties, called props. |
These parameters can be used in a child component using this.props.In Flutter, you assign a local variable or function marked |
final with the property received in the parameterized constructor. |
<code_start>/// Flutter |
class CustomCard extends StatelessWidget { |
const CustomCard({ |
super.key, |
required this.index, |
required this.onPress, |
}); |
final int index; |
final void Function() onPress; |
@override |
Widget build(BuildContext context) { |
return Card( |
child: Column( |
children: <Widget>[ |
Text('Card $index'), |
TextButton( |
onPressed: onPress, |
child: const Text('Press'), |
), |
], |
), |
); |
} |
} |
class UseCard extends StatelessWidget { |
const UseCard({super.key, required this.index}); |
final int index; |
@override |
Widget build(BuildContext context) { |
/// Usage |
return CustomCard( |
index: index, |
onPress: () { |
print('Card $index'); |
}, |
); |
} |
}<code_end> |
<topic_end> |
<topic_start> |
Local storage |
If you don’t need to store a lot of data, and it doesn’t require |
structure, you can use shared_preferences which allows you to |
read and write persistent key-value pairs of primitive data |
types: booleans, floats, ints, longs, and strings.<topic_end> |
<topic_start> |
How do I store persistent key-value pairs that are global to the app? |
In React Native, you use the setItem and getItem functions |
of the AsyncStorage component to store and retrieve data |
that is persistent and global to the app.In Flutter, use the shared_preferences plugin to |
store and retrieve key-value data that is persistent and global |
to the app. The shared_preferences plugin wraps |
NSUserDefaults on iOS and SharedPreferences on Android, |
providing a persistent store for simple data.To add the shared_preferences package as a dependency, run flutter pub add: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.