如何在扑动中更改“永不再显示”并更改主页?

问题描述 投票:0回答:1

如何根据SharedPreferences值更改主页?我的initialRoute和主页是“免责声明”页面,复选框“永远不再显示此内容?”我想在sharepref和下一个应用程序重新加载主页上保存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并相应地更改主页...

android flutter sharedpreferences
1个回答
0
投票

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

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

initialRoute: checkDisclaimerBool,

© www.soinside.com 2019 - 2024. All rights reserved.