text
stringlengths 1
474
|
---|
<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.
|
When there is insufficient space to support tabs, drawers
|
provide a good alternative.<topic_end>
|
<topic_start>Tab navigation
|
In React Native, createBottomTabNavigator
|
and TabNavigation are used to
|
show tabs and for tab navigation.Flutter provides several specialized widgets for drawer and
|
tab navigation:
|
<code_start>class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
|
late TabController controller = TabController(length: 2, vsync: this);
|
@override
|
Widget build(BuildContext context) {
|
return TabBar(
|
controller: controller,
|
tabs: const <Tab>[
|
Tab(icon: Icon(Icons.person)),
|
Tab(icon: Icon(Icons.email)),
|
],
|
);
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.