SetState() 不会更改 PopupMenuButton 中的状态

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

我的屏幕有一个弹出菜单,用户可以通过点击按钮进入专家模式(请参见下面的屏幕截图)。

但是,尽管正在执行

setState()
,重绘始终会将 ìsExpertMode' 重置为其默认值 (true)。


class _SmartboxDashboard extends State<SmartboxDashboard>
    with
        Module<SmartboxDashboard>,
        SingleTickerProviderStateMixin, 
        AutomaticKeepAliveClientMixin<SmartboxDashboard> {

    /// Is expert mode?
    bool isExpertMode = true;

  
    return DefaultTabController(
      length: _tabSectionBodies.length,
      child: Scaffold(
        resizeToAvoidBottomInset: true,
        appBar: AppBar(
            bottom: TabBar(isScrollable: true, tabs: tabSectionTabs),
            title: Text(deviceDisplayName(widget.device.name)),
            actions: [
              PopupMenuButton(
                  icon: Icon(Icons.menu),
                  position: PopupMenuPosition.under,
                  itemBuilder: (context) {
                    return [
                      
                      PopupMenuItem<int>(
                          value: 2,
                          child: ListTile(
                              leading: ElevatedButton(
                                  child: Text(isExpertMode
                                      ? "Normal mode"
                                      : "Expert mode"),
                                  //value: isExpertMode,
                                  onPressed: () {
                                    setState(() {
                                      isExpertMode =
                                          appState.isExpertMode = !isExpertMode;
                                    });
                                  }),
                              title: Text(
                                  isExpertMode ? "Expert mode" : "Normal mode"),
                              subtitle: Text(isExpertMode
                                  ? "Turning to \"normal mode\" will streamline the information and you only see essentials informationabout your battery"
                                  : "Turning to \"expert mode\" will show more screens and deeper information about your batteries"),
                            )
                         ),
                      
                    ];
                  },
                  onSelected: (value) async {

                    switch (value) {
                      case 2:
                        //setState(() {
                        //  isExpertMode = appState.isExpertMode = !isExpertMode;
                        //  print("Expert mode turned to $isExpertMode");
                        });
                        break;
                    }
                    setState(() {
                      sharing = false;
                    });
                  }),
            ]),
        body: TabBarView(children: _tabSectionBodies),

flutter state popupmenu
4个回答
4
投票

您可以使用StatefulBuilder

布尔 isSwitched = true;

 PopupMenuItem(child: StatefulBuilder(
                  builder: (BuildContext context,
                      void Function(void Function()) setState) {
                    return Switch(
                      value: isSwitched,
                      onChanged: (value) {
                        setState(() {
                          isSwitched = value;
                          print(isSwitched);
                        });
                      },
                      activeTrackColor: Colors.lightGreenAccent,
                      activeColor: Colors.green,
                    );
                  },
                ))

1
投票

我觉得应该是

isExpertMode = !isExpertMode;


0
投票

这里需要使用StatefulBuilder,因为context和setState作用域已经改变了。每当使用新的 StatefulBuilder Widget 时,您都会获得一个新的 setState 本地函数来更新状态。


0
投票

如何一起使用statefulbuilder和blocbuilder? 因为环境不同

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