如何在颤振中保存"never show again"并更改主页?



如何根据共享首选项值更改主页?我的初始路由和主页是带有复选框"不再显示此内容"的"免责声明"页面。我想在 sharepref 中保存真或假,并在下一个应用程序重新加载主页以成为"菜单"页面或再次"免责声明"......

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),
                                          ),
                                        ),
                                      ]),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              )));

现在我在共享偏好中保存价值...

  _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并相应地更改主页...

使用正确的路由名称创建一个返回函数:

String checkDisclaimerBool() {
If (prefs==true) {
  return 'MyHomePage'
else {
  return 'OtherPage'
}`enter code here`

initialRoute: checkDisclaimerBool,

相关内容

最新更新