仅在显示AlertDialog时弹出Flutter导航器

时间:2020-01-30 16:21:24

标签: flutter

我希望像Navigator.of(context).pop();这样的代码仅在显示如通过showGeneralDialog()调用的对话框时才运行。如果没有对话框,我希望它什么都不做,而不是关闭我的支架,屏幕变黑。有办法吗?

我的代码显示弹出对话框(忽略这个繁琐的助手,让我们想到在flutter中调用的简单对话框):

Future showAlert({String title, Widget header, Widget dialog, Widget body, Widget listView, EdgeInsetsGeometry contentPadding, bool barrierDismissible = true, bool showButton = true, String buttonText = "OK", Widget customButton, Color warnaAksen}) {
  playSound("butt_press.wav");
  return showGeneralDialog(
    barrierColor: Colors.black.withOpacity(0.5),
    barrierDismissible: barrierDismissible,
    transitionBuilder: (context, a1, a2, widget) {
      final curvedValue = Curves.easeInOutBack.transform(a1.value) - 1.0;
      return Theme(
        data: ThemeProvider.themeOf(context).copyWith(id: 'general_dialog', data: ThemeData(
          dialogBackgroundColor: h.cardColor(),
          primarySwatch: warnaAksen ?? THEME_COLOR,
          fontFamily: THEME_FONT,
          textTheme: TextTheme(
            bodyText1: TextStyle(fontFamily: "Lato", color: textColor()),
            bodyText2: TextStyle(fontFamily: "Lato", color: textColor()),
            button: TextStyle(fontWeight: FontWeight.bold, color: THEME_COLOR)
          )
        )).data,
THEME_FONT,),
        child: Transform(
          transform: Matrix4.identity()..scale(1.0, 1.0 + curvedValue, 1.0),
          child: Opacity(
            opacity: a1.value,
            child: dialog ?? AlertDialog(
              shape: ContinuousRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
              title: header ?? (title != null ? Text(title, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0, color: textColor()),) : null),
              titlePadding: header != null ? EdgeInsets.zero : EdgeInsets.only(left: 24.0, right: 24.0, top: 24.0),
              content: listView ?? SingleChildScrollView(child: body,),
              contentPadding: contentPadding ?? EdgeInsets.only(left: 24.0, top: (title != null || header != null) ? 12.0 : 24.0, right: 24.0, bottom: 24.0),
              actions: showButton ? <Widget>[
                customButton ?? SizedBox(),
                FlatButton(
                  onPressed: () => Navigator.of(context).pop(),
                  child: Text(buttonText, style: TextStyle(fontWeight: FontWeight.bold),),
                ),
              ] : null,
            ),
          ),
        ),
      );
    },
    transitionDuration: Duration(milliseconds: 500),
    barrierLabel: '',
    context: context,
    pageBuilder: (context, animation1, animation2) => Container()
  );
}

我尝试过的事情:

// TODO FIXME when there's no dialogue, there's no need to do anything
closeDialog() => Navigator.of(context, rootNavigator: true).pop('dialog');

1 个答案:

答案 0 :(得分:0)

不确定这是否对您有帮助,但是我遇到了一个问题,如果用户使用backpress关闭对话框,则会出现问题。我所做的是使用std::size,并将onWillPopScope函数设置为返回onWillPop。这并没有使用户关闭对话框,因此我的默认false仅弹出了最上面的对话框,而没有对我的路线造成任何问题。

pop()