首页 > 解决方案 > 使用 Switch 时的 showModalBottomSheet 堆叠

问题描述

我正在尝试在我的应用程序的登录屏幕上显示隐私政策横幅。每当我单击开关时,另一个 ModalBottomSheet 就会堆叠在另一个之上。

我试图在单独的 Statefullwidget 中提取按钮,没有任何变化。

有什么建议、提示或想法吗?

    var setting = Provider.of<SettingProvider>(context);
    WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
      if (setting.data == false) {
        showModalBottomSheet(
            barrierColor: Colors.blueGrey.withOpacity(0.1),
            isDismissible: false,
            context: context,
            builder: (BuildContext context) {
              return Container(
                padding: EdgeInsets.fromLTRB(30, 10, 30, 0),
                height: 300,
                child: ListView(
                  children: [
                    Center(
                      child: Text(
                        'Datenschutz',
                        style: TextStyle(
                          fontFamily: 'Caveat',
                          fontSize: 25,
                        ),
                      ),
                    ),
                    Text(
                      'Description.',
                      style: TextStyle(fontSize: 10),
                      textAlign: TextAlign.justify,
                    ),
                    SwitchListTile(
                        title: Text(
                          'I agree.',
                          style: TextStyle(fontSize: 10),
                        ),
                        value: setting.initdata,
                        onChanged: (bool value) {
                          setting.changeInitData(value);
                        }),
                    SizedBox(
                      height: 10,
                    ),
                    ElevatedButton(
                        onPressed: () {
                          if (setting.initdata == true) {
                            setting.changeData(setting.initdata);
                            Navigator.pop(context, 'Sichern');
                          } else {
                            Fluttertoast.showToast(
                                msg:
                                    'Error.',
                                gravity: ToastGravity.TOP);
                          }
                        },
                        child: Text('Sichern')),
                  ],
                ),
              );
            });
      }
    });````

标签: flutterflutter-showmodalbottomsheet

解决方案


我刚刚找到了一种解决方法。可能不是最优雅的方式,但它有效。

在保存隐私设置的按钮中,我使用Navigator.of(context).pushReplacementNamed(LogInScreen.routeName);Navigator.pop.

按下 时,模态框仍在堆叠Switch,但用户不必单击多个模态框。


推荐阅读