【发布时间】:2020-11-28 06:12:05
【问题描述】:
我一直在尝试在共享首选项中保存布尔值“DarkMode”,但似乎无法成功。 darkmode 变量用于将应用程序更改为暗模式。如果有人可以使用布尔值发布一个非常简单的共享偏好示例,也将不胜感激。任何帮助将不胜感激。谢谢
import 'package:flutter/material.dart';
import 'package:page_transition/page_transition.dart';
import 'package:shared_preferences/shared_preferences.dart';
bool DarkMode = false; //Here is the code that I want to save into storage.
bool Afr = true;
class Gr8Videos extends StatefulWidget {
//Gr8Videos({Key key, this.title}) : super(key: key);
// final String title;
@override
_Gr8VideosState createState() => _Gr8VideosState();
}
class _Gr8VideosState extends State<Gr8Videos> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
drawer: Container(
width: 160,
child: ClipRRect(
borderRadius: BorderRadius.vertical(top: Radius.circular(0.0)),
child: Drawer(
child: Container(
color: DarkMode ? Colors.grey[900] : Colors.white,
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 30, 0, 0),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.message,
size: 25,
color: DarkMode ? Colors.white : Colors.black,
),
onPressed: () {
// Navigator.push(
// context,
// PageTransition(
// type: PageTransitionType.rightToLeft,
// child: SavedRoute()));
},
),
Text(
'Contact',
style: TextStyle(
fontSize: 17,
color: DarkMode ? Colors.white : Colors.black,
),
),
],
),
Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.notifications,
size: 25,
color: DarkMode ? Colors.white : Colors.black,
),
onPressed: () {
// Navigator.push(
// context,
// PageTransition(
// type: PageTransitionType.rightToLeft,
// child: SavedRoute()));
},
),
Text(
'Notifications',
style: TextStyle(
fontSize: 17,
color: DarkMode ? Colors.white : Colors.black,
),
),
],
),
Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.share,
size: 25,
color: DarkMode ? Colors.white : Colors.black,
),
onPressed: () {
// Navigator.push(
// context,
// PageTransition(
// type: PageTransitionType.rightToLeft,
// child: SavedRoute()));
},
),
Text(
'Share',
style: TextStyle(
fontSize: 17,
color: DarkMode ? Colors.white : Colors.black,
),
),
],
),
Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.settings,
size: 25,
color: DarkMode ? Colors.white : Colors.black,
),
onPressed: () {
// Navigator.push(
// context,
// PageTransition(
// type: PageTransitionType.rightToLeft,
// child: SavedRoute()));
},
),
Text(
'Settings',
style: TextStyle(
fontSize: 17,
color: DarkMode ? Colors.white : Colors.black,
),
),
],
),
],
),
),
),
),
),
),
appBar: AppBar(
title: Text(
'Welcome ' + Name,
style: TextStyle(fontWeight: FontWeight.w400),
),
backgroundColor: DarkMode ? Colors.grey[900] : Colors.blue, //Here is the code that I want to get form storage.
centerTitle: true,
actions: <Widget>[
Row(...And so on
【问题讨论】:
-
介意分享一下你是如何存储布尔值的?
-
不知道,等人回复!
标签: flutter sharedpreferences shared preference pre