【发布时间】:2019-11-12 16:59:31
【问题描述】:
flutter中已经指定的主题设置如何保存?
我在这里遇到了问题。我很难在本地存储数据。我尝试了“Flutter_Secure_Storage”,但我不明白如何使用它。本质上,我希望将所选主题保存在本地,并且当应用程序重新启动时,它可以再次使用该主题。我试过使用sharedpreference,但没有用。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(38.0),
child: AppBar(
automaticallyImplyLeading: true,
title: const Text('Theme'),
leading: IconButton(
icon: const Icon(Icons.arrow_back_ios),
onPressed: () {
Navigator.pop(context);
},
),
),
),
body: new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//text
new Container(
padding: EdgeInsets.all(10.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Container(
padding: EdgeInsets.only(top: 50, left: 10, right: 10),
child: new Column(
children: <Widget>[
//light theme
new Row(
children: <Widget>[
new Column(
children: <Widget>[
new Container(
width: 120,
height: 200,
color: Color.fromRGBO(255, 127, 80, 5),
child: new Icon(
FontAwesomeIcons.cloudSun,
color: Colors.yellowAccent,
size: 50,
)),
new Text(
'Light',
style: TextStyle(fontSize: 16),
),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
Radio(
value: 1,
groupValue: selectRadio,
activeColor: Colors.blue,
onChanged: (val) {
//print('Radio $val');
setState(() {
setSelectedRadio(val);
Provider.of<ThemeNotifier>(context).switchTheme();
});
},
)
],
)
],
)
],
)
],
),
),
new Container(
padding: EdgeInsets.only(top: 50, left: 10, right: 10),
child: new Column(
children: <Widget>[
//light theme
new Row(
children: <Widget>[
new Column(
children: <Widget>[
new Container(
width: 120,
height: 200,
color: Color.fromRGBO(173, 216, 230, 5),
child: new Icon(
FontAwesomeIcons.cloudMoon,
color: Color.fromRGBO(2, 22, 69, 5),
size: 50,
)),
new Text(
'Dark',
style: TextStyle(fontSize: 16),
),
ButtonBar(
alignment: MainAxisAlignment.center,
children: <Widget>[
Radio(
value: 2,
groupValue: selectRadio,
activeColor: Colors.blue,
onChanged: (val) {
//print('Radio $val');
setState(() {
setSelectedRadio(val);
Provider.of<ThemeNotifier>(context).switchTheme();
});
},
)
],
)
],
)
],
)
],
),
)
],
),
)
],
),
),
);
}
【问题讨论】: