[英]Flutter : Can use StreamBuilder to detect changes happens to a bool variable? If yes, how ? If no, what method can I use?
I want to manage the theme of my app.我想管理我的应用程序的主题。 I am using SharedPreferences to store a bool value which indicate darkmode/lightmode.
我正在使用 SharedPreferences 来存储指示暗模式/亮模式的布尔值。 Whenever the user changes the mode the bool value will change.
每当用户更改模式时,布尔值都会更改。 I want to change the theme of my app whenever the bool value changes.
每当 bool 值更改时,我想更改我的应用程序的主题。 I am trying to use a streambuilder but it gives me error.
我正在尝试使用流构建器,但它给了我错误。 I am new to flutter and I don't know whether it can be used or not.
我是flutter新手,不知道能不能用。 If yes, how to use it?
如果是,如何使用? If no, then how can I change theme while the bool value changes ?
如果不是,那么当 bool 值发生变化时如何更改主题? Can someone help?
有人可以帮忙吗? Here is my code
这是我的代码
class SharedPreference {
bool _darkmode = false;
get darkMode {
return _darkmode;
}
setTheme(bool value) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setBool('isdark', value);
_darkmode = value;
}
Future<bool> getTheme() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return preferences.getBool('isdark') ?? false;
}
}
// button to change mode
Switch.adaptive(
value: off_on,
activeColor: Colors.deepOrange,
onChanged: (value) {
setState(() => this.off_on = value);
SharedPreference().setTheme(value);
}
),
// main
class _MyappState extends State<Myapp> {
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: SharedPreference().darkMode,
builder: (context, snapshot) {
return MaterialApp(
// theme: ThemeData(
// fontFamily: 'OpenSans',
// primarySwatch: Colors.deepOrange,
// ),
theme: ModeSpecifications.themeData(SharedPreference().darkMode, context),
navigatorKey: navigatorKey,
initialRoute: '/mainpage',
routes: {
'/mainpage': (context)=> MainPage(),
},
);
}
);
}
}
I got the following errors: type 'bool' is not a subtype of type 'Stream<Object?>?'我收到以下错误:“bool”类型不是“Stream<Object?>?”类型的子类型
and Unhandled Exception: MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences_android)和未处理的异常:MissingPluginException(在通道 plugins.flutter.io/shared_preferences_android 上找不到方法 getAll 的实现)
Can someone help to resolve the issue?有人可以帮助解决问题吗?
You can use a StreamController
to create a stream that streamBuilder
could list to.您可以使用
StreamController
创建streamBuilder
可以列出的流。 This should work.这应该有效。
class SharedPreference {
final _controller = StreamController<bool>();
Stream<bool> get darkMode async* {
yield* _controller.stream;
}
void setTheme(bool value) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setBool('isdark', value);
_controller.add(value); // update value here.
}
Future<bool> getTheme() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
return preferences.getBool('isdark') ?? false;
}
// call when user closes the app or something.
void dispose() => _controller.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.