【问题标题】:How do i save a bool value in shared preferences with flutter?如何使用颤振在共享首选项中保存布尔值?
【发布时间】: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


【解决方案1】:

您可以将此库用于SharedPreference

我刚刚与您分享了如何使用共享偏好。你可以像其他人一样存储 bool。

将此添加到您的包的 pubspec.yaml 文件中:

依赖: shared_preferences: ^0.5.12+4

saveBoolVaue() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setBool('status', true);
 
}

getBoolVaue() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  await prefs.getBool('status');
 
}

【讨论】:

  • 谢谢,我试过这个并实现了 pubspec 包,但是当我按下图标按钮或读取值以确定背景颜色时不知道如何存储值...也许知道该怎么做?
  • 您可以将材质主题用于黑暗和明亮的情绪。
猜你喜欢
  • 1970-01-01
  • 2021-08-16
  • 2021-12-29
  • 2014-05-07
  • 2019-01-09
  • 2017-10-28
  • 2020-04-02
  • 1970-01-01
  • 2020-05-29
相关资源
最近更新 更多