text
stringlengths 1
372
|
|---|
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:
|
<code_start>
|
import 'package:shared_preferences/shared_preferences.dart';
|
<code_end>
|
to implement persistent data, use the setter methods
|
provided by the SharedPreferences class.
|
setter methods are available for various primitive
|
types, such as setInt, setBool, and setString.
|
to read data, use the appropriate getter method provided
|
by the SharedPreferences class. for each
|
setter there is a corresponding getter method,
|
for example, getInt, getBool, and getString.
|
<code_start>
|
future<void> updateCounter() async {
|
final prefs = await SharedPreferences.getInstance();
|
int? counter = prefs.getInt('counter');
|
if (counter is int) {
|
await prefs.setInt('counter', ++counter);
|
}
|
setState(() {
|
_counter = counter;
|
});
|
}
|
<code_end>
|
<topic_end>
|
<topic_start>
|
routing
|
most apps contain several screens for displaying different
|
types of information. for example, you might have a product
|
screen that displays images where users could tap on a product
|
image to get more information about the product on a new screen.
|
in android, new screens are new activities.
|
in iOS, new screens are new ViewControllers. in flutter,
|
screens are just widgets! and to navigate to new
|
screens in flutter, use the navigator widget.
|
<topic_end>
|
<topic_start>
|
how do i navigate between screens?
|
in react native, there are three main navigators:
|
StackNavigator, TabNavigator, and DrawerNavigator.
|
each provides a way to configure and define the screens.
|
in flutter, there are two main widgets used to navigate between screens:
|
a navigator is defined as a widget that manages a set of child
|
widgets with a stack discipline. the navigator manages a stack
|
of route objects and provides methods for managing the stack,
|
like navigator.push and navigator.pop.
|
a list of routes might be specified in the MaterialApp widget,
|
or they might be built on the fly, for example, in hero animations.
|
the following example specifies named routes in the MaterialApp widget.
|
info note
|
named routes are no longer recommended for most
|
applications. for more information, see
|
limitations in the navigation overview page.
|
<code_start>
|
class NavigationApp extends StatelessWidget {
|
// this widget is the root of your application.
|
const NavigationApp({super.key});
|
@override
|
widget build(BuildContext context) {
|
return MaterialApp(
|
//...
|
routes: <string, WidgetBuilder>{
|
'/a': (context) => const UsualNavScreen(),
|
'/b': (context) => const DrawerNavScreen(),
|
},
|
//...
|
);
|
}
|
}
|
<code_end>
|
to navigate to a named route, the navigator.of()
|
method is used to specify the BuildContext
|
(a handle to the location of a widget in the widget tree).
|
the name of the route is passed to the pushNamed function to
|
navigate to the specified route.
|
<code_start>
|
Navigator.of(context).pushNamed('/a');
|
<code_end>
|
you can also use the push method of navigator which
|
adds the given route to the history of the
|
navigator that most tightly encloses the given BuildContext,
|
and transitions to it. in the following example,
|
the MaterialPageRoute widget is a modal route that
|
replaces the entire screen with a platform-adaptive
|
transition. it takes a WidgetBuilder as a required parameter.
|
<code_start>
|
navigator.push(
|
context,
|
MaterialPageRoute(
|
builder: (context) => const UsualNavScreen(),
|
),
|
);
|
<code_end>
|
<topic_end>
|
<topic_start>
|
how do i use tab navigation and drawer navigation?
|
in material design apps, there are two primary options
|
for flutter navigation: tabs and drawers.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.