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