在Flutter中用sharedPreferences保存开关状态。

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

如何保存 switch 现在,当我点击我的 "我的 "的时候,我就会发现,我的 "我的 "是一个 "我的",我的 "我的 "是一个 "我的"。switch 在真实的情况下,再回到上一页,我的。switch 变为false,但我想把这个状态保存到将来的另一个用户操作中。

我读到,我可以使用 AutomaticKeepAliveClientMixin 但对我没用,所以我认为 sharedPreferences 将是最好的选择,但我不知道如何做到这一点。

我的代码。

class LocationScreenState extends State<LocationScreen> with AutomaticKeepAliveClientMixin{
  @override bool get wantKeepAlive => true;
  bool state = false;
  // PermissionStatus _status;
  PermissionStatus _status;


  @override
  Widget build(BuildContext context){
    // super.build(context);
    return Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            backgroundColor: Colors.white,
            iconTheme: IconThemeData(color: Colors.black),
            title: Text(AppTranslations.of(context).text("settings_location"), style: TextStyle(color: Colors.black, letterSpacing: 1)),
            elevation: 0.0,
            centerTitle: true,
            bottom: PreferredSize(child: Container(color: Colors.black, height: 0.1), preferredSize: Size.fromHeight(0.1),),
          ),
          body: Container(
            child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(top: 40.0),
                    child: SizedBox(
                      height: 150,
                      width: 800,
                    child: Card(
                      elevation: 5.0,
                      child: Padding(
                        padding: EdgeInsets.all(15.0),
                        child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          const ListTile(
                            title: Text('xxx'),
                            subtitle: Text('xxx'),
                          ),
                          Row(
                            children: <Widget>[
                              Switch(
                                value: state,
                                onChanged: (bool s) {
                                  setState(() {
                                    state = s;
                                    if(state){
                                      // _askPermission();
                                    }
                                  });
                                },
                              )
                            ],
                          )
                        ],
                      ),
                      )
                    )
                  ),
                  )
                ],
              )
            ],
          )
    );
  }
}

谢谢你的帮助

/////////////////////////////

android flutter dart switch-statement
1个回答
1
投票

试试这个。

  @override
  void initState(){
    super.initState();
    getSwitchStatus();
  }

  Future<bool> getSwitchStatus() async {
    sharedPreferences = await SharedPreferences.getInstance();
    bool status = sharedPreferences.getBool("switchStatus");
    return status;
  }

在您的构建方法中。

Row(
 children: <Widget>[
     FutureBuilder(
         future: SharedPreferences.getInstance(),
         builder: (context, snapshot){
            return Switch(
             value: sharedPreferences.getBool("switchStatus"),
             onChanged: (bool s) {
               switchState = s;
                 sharedPreferences.setBool("switchStatus", switchState);
                 setState((){
                     if(switchState){
                       requestLocationPermission();
                        }
                       });
                      },
                     );
                    }
                   ),

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