-1

I have been trying to save a bool value "DarkMode" in shared preferences but can't seem to succeed. The darkmode variable is used to change the app to darkmode. It will also be greatly appreciated if someone could post a very simple example of shared preferences using a bool value. Any help will be appreciated. Thanks

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
Steve
  • 103
  • 1
  • 2
  • 8

1 Answers1

0

You can use this library for SharedPreference

I just shared with you how you can use shared preference. you can store bool like others.

Add this to your package's pubspec.yaml file:

dependencies: 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');
 
}
  • Thanks, I tried this and have implemented the pubspec package but don't know how to store the value when I press a iconbutton or read a value to determine the color of the background... Do you perhaps know how to do this? – Steve Nov 28 '20 at 08:34
  • You can use the material theme for dark and light moods. – Kamruzzaman Sajib Nov 29 '20 at 09:27