如何根据SharedPreferences值更改主页? 我的initialRoute和主页是“免责声明”页面,带有复选框“不再显示此内容?”我想在true或false中保存true或false,并在下一个应用程序重新加载主页上保存为“菜单”页面或再次为“免责声明” ...
void main() => runApp(App());
class App extends StatelessWidget {
// bool x= true;
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: 'MyHomePage',
routes: {
'Menu': (context) => Menu(false),
'Page1': (context) => Page1(),
'Page2': (context) => Page2('w1'),
'Page3': (context) => Page3(),
},
home: MyHomePage()
);
}
//免责声明页面
class Disclaimer extends State<MyHomePage> {
bool _isChecked=false;
@override
Widget build(BuildContext context) {
void onChanged(bool value) {
setState(() {
_isChecked = value;
});
}
return Material(
type: MaterialType.transparency,
child: new Container(
color: Color.fromRGBO(246, 246, 246, 1.0),
child: Column(
children: <Widget>[
Center(
child: Padding(
padding: EdgeInsets.only(top: 40.0),
child: Text(
'TITLE',
),
),
),
Flexible(
child: SingleChildScrollView(
child: Center(
child: Padding(
padding: EdgeInsets.all(40.0),
child: Material(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Color.fromRGBO(149, 152, 154, 1.0),
width: 1.0),
boxShadow: [
BoxShadow(
color: Color.fromRGBO(169, 130, 116, 0.5),
blurRadius: 30.0)
]),
child: Column(
children: <Widget>[
SizedBox(height: 40.0),
Padding(
padding: EdgeInsets.all(1.0),
child: Text(
'DISCLAIMER',
textAlign: TextAlign.center,
style: TextStyle(
color: Color.fromRGBO(121, 85, 72, 1.0),
fontSize: 20.00,
fontWeight: FontWeight.bold),
),
),
Divider(),
Padding(
padding: EdgeInsets.all(30.0),
child: Text(
'Discaimer text Discaimer text Discaimer textDiscaimer textDiscaimer textDiscaimer textDiscaimer textDiscaimer text ',
textAlign: TextAlign.center,
style: TextStyle(
color: Color.fromRGBO(121, 85, 72, 1.0),
fontSize: 16.0),
),
),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Do not show this again!"),
new Checkbox(
value: _isChecked,
onChanged: (bool value) {
onChanged(value);
}),
],
),
Divider(),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
onPressed: () {
DB().init();
Navigator.pushReplacementNamed(
context, 'Menu');
//save checkbox value to sharepref
_saveDontShowAgain(_isChecked);
},
child: Text(
'Ok',
style: TextStyle(
color: Color.fromRGBO(
38, 153, 251, 1.0),
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),
]),
],
),
),
),
),
),
),
),
],
)));
//现在我将价值保存在sharepref中...
_saveDontShowAgain(bool state) async {
SharedPreferences pref = await SharedPreferences.getInstance();
pref.setBool('state', state);
return pref.commit();
}
_readData(String key) async {
final pref = await SharedPreferences.getInstance();
setState(() {
_isChecked = pref.getBool('state');
});
}
现在如何从主页调用_readShare并相应地更改主页...
答案 0 :(得分:0)
使用正确的路线名称创建返回函数:
String checkDisclaimerBool() {
If (prefs==true) {
return 'MyHomePage'
else {
return 'OtherPage'
}`enter code here`
在initialRoute: checkDisclaimerBool,