我创建了一个Flutter入门套件,可以在应用内切换主题。
现在这很好用,但是有一个小问题。
我已将切换按钮放置在设置屏幕中, 但是当我转到另一个屏幕然后返回到设置屏幕时 -开关按钮似乎自己变成了关闭模式。
我要附上图片和my code on github,以便大家明白我的意思。
class ThemeSwitchState extends State {
bool switchControl = false;
Future<bool> darkMode() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.getBool("isDark");
}
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: darkMode(),
builder: (context, _snapshot) {
return Transform.scale(
scale: 1.5,
child: Switch(
value: _snapshot.data ?? false,
onChanged: (value) {
save(value);
setState(() {
});
},
activeColor: CustomColors().duskBlue,
activeTrackColor: CustomColors().noroGrey,
inactiveThumbColor: CustomColors().lureGrey,
inactiveTrackColor: CustomColors().noroGrey,
),
);
},
);
}
void save(bool value) async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setBool("isDark", value);
}
void toggleSwitch(bool value) {
ThemeChanger _themeChanger =
Provider.of<ThemeChanger>(context, listen: false);
if (switchControl == false) {
setState(() {
switchControl = true;
});
print('Theme is Dark');
_themeChanger.setTheme(CustomThemes.darkTheme.copyWith(
textTheme:
CustomThemes.darkTextTheme(CustomThemes.darkTheme.textTheme)));
} else {
setState(() {
switchControl = false;
});
print('Theme is Light');
_themeChanger.setTheme(CustomThemes.lightTheme.copyWith(
textTheme:
CustomThemes.lightTextTheme(CustomThemes.lightTheme.textTheme)));
}
}
}
答案 0 :(得分:0)
您可以使用https://pub.dev/packages/shared_preferences中的 SharedPreferences 保存复选框
的最后一个值我写了一个例子
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Settings extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
Future<bool> darkMode() async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.getBool("isNight");
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: <Widget>[
Text("Night Mode"),
FutureBuilder<bool>(
future: darkMode(),
builder: (context, _snapshot){
return Checkbox(
value: _snapshot.data ?? false,
onChanged: (value){
save(value);
setState(() {
toggleSwitch(value);
});
},
);
},
)
],
),
),
);
}
void save(bool value) async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setBool("isNight", value);
}
}
编辑:带开关
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Settings extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
Future<bool> darkMode() async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return sharedPreferences.getBool("isNight");
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: <Widget>[
Text("Night Mode"),
FutureBuilder<bool>(
future: darkMode(),
builder: (context, _snapshot){
return Switch(
value: _snapshot.data ?? false,
onChanged: (value){
save(value);
setState(() {
});
},
);
},
)
],
),
),
);
}
void save(bool value) async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setBool("isNight", value);
}
}
更新 使用值代替switchControl
void toggleSwitch(bool value) {
ThemeChanger _themeChanger =
Provider.of<ThemeChanger>(context, listen: false);
if (value) {
setState(() {
switchControl = true;
});
print('Theme is Dark');
_themeChanger.setTheme(CustomThemes.darkTheme.copyWith(
textTheme:
CustomThemes.darkTextTheme(CustomThemes.darkTheme.textTheme)));
} else {
setState(() {
switchControl = false;
});
print('Theme is Light');
_themeChanger.setTheme(CustomThemes.lightTheme.copyWith(
textTheme:
CustomThemes.lightTextTheme(CustomThemes.lightTheme.textTheme)));
}
}
}