text
stringlengths 1
372
|
|---|
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.