Flutter:如何覆盖功能

时间:2019-10-29 14:11:35

标签: flutter dart flutter-layout flutter-dependencies

基本上,我想覆盖位于showDialog上的祖先packages/flutter/lib/src/material/dialog.dart函数的一些支持

我想更改barrierColor属性,并从showGeneralDialog构建器方法中删除SafeArea小部件。

//...
Future<T> showDialog<T>({
  @required BuildContext context,
  bool barrierDismissible = true,
  @Deprecated(
    'Instead of using the "child" argument, return the child from a closure '
    'provided to the "builder" argument. This will ensure that the BuildContext '
    'is appropriate for widgets built in the dialog.'
  ) Widget child,
  WidgetBuilder builder,
}) {
  assert(child == null || builder == null);
  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 = child ?? Builder(builder: builder);
      return SafeArea( // <-- !remove SafeArea widget!
        child: Builder(
          builder: (BuildContext context) {
            return theme != null
                ? Theme(data: theme, child: pageChild)
                : pageChild;
          }
        ),
      );
    },
    barrierDismissible: barrierDismissible,
    barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
    barrierColor: Colors.black54, // <-- !change the color!
    transitionDuration: const Duration(milliseconds: 150),
    transitionBuilder: _buildMaterialDialogTransitions,
  );
}

是否可以在main.dart文件中进行操作?如果可以的话,有人可以举个例子吗?

1 个答案:

答案 0 :(得分:1)

您只需将该函数复制/粘贴到main.dart文件中,然后在其中更改名称和barrierColor即可,因为您可以使用showGeneralDialog