【问题标题】:Remove grey background for flutter alert dialog删除颤振警报对话框的灰色背景
【发布时间】:2019-10-27 03:42:44
【问题描述】:

我的 Flutter 应用中有 CupertinoAlertDialog 和 AlertDialog。每次弹出对话框,它后面的一切都会变暗。我想删除背景。我该怎么做?

CupertinoDialogAction(
        child: Text('Delete',
                style: TextStyle(color: Colors.red),
              ),
              onPressed: () async {
                await CommentActivity.delete(postData[index]['id'])
                  .then((response) {
                  if (response) {
                    setState(() {
                      postData.removeAt(index);
                      createPageActivity();
                      renderPageActivity();
                    });
                    Navigator.of(context).pop();
                  }
                });
              }
            )
          ],
        )

【问题讨论】:

  • 只想在弹出对话框时去掉变暗的背景
  • 能否显示您要删除的背景的屏幕截图?

标签: flutter flutter-layout flutter-cupertino


【解决方案1】:

showDialog 方法中具有 barrierColor 属性的简单解决方案,我将不透明度值为零的白色设置为白色,并且屏障阴影消失了

AlertDialog alert = AlertDialog(
    backgroundColor: Colors.transparent,
    elevation: 0,
    content: new Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Loader(),
      ],
    ),
  );
  showDialog(
    barrierColor: Colors.white.withOpacity(0),
    barrierDismissible: false,
    context: context,
    builder: (BuildContext context) {
      return WillPopScope(
            onWillPop: (){},
          child: alert);
    },
  );

【讨论】:

    【解决方案2】:

    部分解决问题的替代解决方案是使用几乎透明的颜色作为屏障:

    showDialog<void>(
          barrierColor: Color(0x01000000),
    )
    

    【讨论】:

      【解决方案3】:

      只需使用 de navigator 启动对话框,而不是使用 showDialog() 并使用 PageRouteBuilder

      Navigator.of(context).push(
                        PageRouteBuilder(
                            pageBuilder: (context, _, __) => AlertDialog(),
                            opaque: false),
      );
      

      【讨论】:

        【解决方案4】:

        我认为您是在谈论对话框背景中的黑色推子... 是 material/cupertino 实现的一部分,在 Material 中有一个固定值 Colors.black54。

        您必须复制showDialog() 代码并对其进行修改。

        演示:

        // common Dialog widget shown in both implementation. 
          Widget buildDialog(BuildContext context) {
            return CupertinoDialogAction(
              child: Text(
                'Delete',
                style: TextStyle(color: Colors.red),
              ),
              onPressed: () async {
                Navigator.of(context).pop();
              },
            );
          }
        
          void openDialog(BuildContext context) {
            // open custom dialog.
            openCustomDialog(context);
        
            // open default dialog.
        //    openFlutterDialog(context);
        
          }
        
          // regular Fluter showDialog()
          void openFlutterDialog(BuildContext context) {
            showDialog(
              context: context,
              builder: (ctx) {
                return buildDialog(ctx);
              },
            );
          }
        
          void openCustomDialog(BuildContext context) {
            showCustomDialog(
              context: context,
              builder: (ctx) {
                return buildDialog(ctx);
              },
            );
          }
        
          // custom implementation of showDialog()...
          Future<T> showCustomDialog<T>({
            @required BuildContext context,
            bool barrierDismissible = true,
            WidgetBuilder builder,
          }) {
            assert(debugCheckHasMaterialLocalizations(context));
            final ThemeData theme = Theme.of(context, shadowThemeOnly: true);
            return showGeneralDialog(
              context: context,
              pageBuilder: (BuildContext buildContext, Animation<double> animation,
                  Animation<double> secondaryAnimation) {
                final Widget pageChild = Builder(builder: builder);
                return SafeArea(
                  child: Builder(builder: (BuildContext context) {
                    return theme != null
                        ? Theme(data: theme, child: pageChild)
                        : pageChild;
                  }),
                );
              },
              barrierDismissible: barrierDismissible,
              barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
              // KEY PART TO MODIFY, Flutter doesn't allow a transparent Color,
              // values under opacity .01 are considered transparent and will throw an error.
              // But this value is transparent enough.
              barrierColor: Colors.black.withOpacity(0.01),
        
                    // you can modify the default FadeTransition duration here.
              transitionDuration: const Duration(milliseconds: 2000),
            );
          }
        

        这就是你要找的吗?

        【讨论】:

        • 永远不应该将修改框架视为解决方案。
        猜你喜欢
        • 2017-11-10
        • 2013-01-07
        • 2017-05-10
        • 2022-10-06
        • 2021-11-08
        • 2021-04-12
        • 1970-01-01
        • 1970-01-01
        • 2012-09-27
        相关资源
        最近更新 更多