【问题标题】:Maintain state of toggle switch flutter drawer保持拨动开关颤振抽屉的状态
【发布时间】:2022-07-20 17:48:20
【问题描述】:

我的应用中有一个抽屉,我想在其中保持拨动开关的状态。 我怎样才能做到这一点 ? 这是代码

SizedBox(
                          height: 40,
                          child: ListTile(
                            leading: SizedBox(
                              height: 25,
                              width: 25,
                              child: Image.asset(
                                'assets/sideMenu/Bell.png',
                              ),
                            ),
                            title: Transform.translate(
                                offset: const Offset(-14, 0),
                                child: const Text('Notifications',
                                    style: TextStyle(
                                        fontSize: 12, color: Colors.white))),
                            trailing: SizedBox(
                                height: 20,
                                width: 40,
                                child: Transform.scale(
                                  scale: 1.3,
                                  child: Switch.adaptive(
                                      value: value,
                                      activeColor: Colors.white,
                                      activeTrackColor: const Color.fromARGB(
                                          255, 35, 187, 40),
                                      onChanged: (value) =>
                                          setState(() => this.value = value)),
                                )),
                          ),
                        ),

我能做些什么来让它保持它的状态?

这就是它现在的工作方式:

【问题讨论】:

  • 你能包含你的构建方法吗?
  • 使用值通知器来维护状态和值通知器构建器在没有任何状态管理解决方案的情况下监听更改

标签: flutter dart flutter-layout


【解决方案1】:

您可以使用共享首选项并在按下时保存状态

onChanged: (val)async{
  setState((){value = val});
  SharedPreferences prefs= await SharedPreferences.getInstance();
  prefs.setBool("notificationStatus", val);
}

然后在initstate中就可以设置值了

bool value = true;
@override
initState() {
  super.initState();
 fetchNotificationStatus();
}
fetchNotificationStatus() async{
  SharedPreferences prefs = await SharedPreferences.getInstance();
  value = prefs.getBool("notificationStatus")??true;
 setState((){});
}

【讨论】:

    【解决方案2】:

    value 放在构建方法之外。

    class _DTEsState extends State<DTEs> {
      bool value = false;
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          drawer: SizedBox(
            height: 40,
            child: ListTile(
              leading: SizedBox(
                height: 25,
                width: 25,
                child: Image.asset(
                  'assets/sideMenu/Bell.png',
                ),
              ),
              title: Transform.translate(
                  offset: const Offset(-14, 0),
                  child: const Text('Notifications',
                      style: TextStyle(fontSize: 12, color: Colors.white))),
              trailing: SizedBox(
                  height: 20,
                  width: 40,
                  child: Transform.scale(
                    scale: 1.3,
                    child: Switch.adaptive(
                        value: value,
                        activeColor: Colors.white,
                        activeTrackColor: const Color.fromARGB(255, 35, 187, 40),
                        onChanged: (value) => setState(() => this.value = value)),
                  )),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 2019-04-17
      • 2019-10-30
      • 1970-01-01
      相关资源
      最近更新 更多