flutter:教你自定义Dialog

栏目: Android · 发布时间: 5年前

内容简介:####然后你会发现显示出来的Dialog回事这个样子的####你tm在逗我,这是个什么鬼,别慌,我们改一下####然后就变成了这个样子
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,
})
复制代码

构造方法中有三个参数

'context' :  这个是必须传递的参数,也是可以显示出视图的关键
'barrierDismissible':这个是安卓中触摸dialog外部自动取消Dialog
'builder':用于创建Widget 
复制代码

我们举个栗子

RaisedButton(
           child: Text("showDialog"),
           onPressed: () {
                  showDialog(
                      context: context,
                      barrierDismissible: true,
                      builder: (BuildContext context) {
                        return Center(
                          child: Text("data"),
                        );
                      });
                },
              )
复制代码

####然后你会发现显示出来的Dialog回事这个样子的

flutter:教你自定义Dialog

####你tm在逗我,这是个什么鬼,别慌,我们改一下

showDialog(
             context: context,
             barrierDismissible: true,
             builder: (BuildContext context) {
                   return Scaffold(
                          body: Center(
                            child: Text("data"),
                          ),
                        );
                      });
复制代码

####然后就变成了这个样子

flutter:教你自定义Dialog

####这种东西也不能用啊,我们继续看Dialog.dart文件然后发现了熟悉的AlterDialog,并且上面还有demo

###AlterDialog类

class AlertDialog extends StatelessWidget 
复制代码

###显示AlterDialog的示例代码

/// Future<void> _neverSatisfied() async {
///   return showDialog<void>(
///     context: context,
///     barrierDismissible: false, // user must tap button!
///     builder: (BuildContext context) {
///       return AlertDialog(
///         title: Text('Rewind and remember'),
///         content: SingleChildScrollView(
///           child: ListBody(
///             children: <Widget>[
///               Text('You will never be satisfied.'),
///               Text('You\’re like me. I’m never satisfied.'),
///             ],
///           ),
///         ),
///         actions: <Widget>[
///           FlatButton(
///             child: Text('Regret'),
///             onPressed: () {
///               Navigator.of(context).pop();
///             },
///           ),
///         ],
///       );
///     },
///   );
/// }
复制代码

####我们试一下官方示例

flutter:教你自定义Dialog

ojbk官方的dialog已经搞定。现在我们开始自定义一个Dialog

####1. 先看下AlterDialog构造

const AlertDialog({
    Key key,
    this.title,
    this.titlePadding,
    this.titleTextStyle,
    this.content,
    this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
    this.contentTextStyle,
    this.actions,
    this.backgroundColor,
    this.elevation,
    this.semanticLabel,
    this.shape,
  }) : assert(contentPadding != null),
       super(key: key);
复制代码

######我们看到一些基本属性定义,然后我们只关心content,而content是一个widget类

/// The (optional) content of the dialog is displayed in the center of the
  /// dialog in a lighter font.
  ///
  /// Typically this is a [SingleChildScrollView] that contains the dialog's
  /// message. As noted in the [AlertDialog] documentation, it's important
  /// to use a [SingleChildScrollView] if there's any risk that the content
  /// will not fit.
  final Widget content;
复制代码

######那正好,我们直接定义widget传进去就ojbk了 ####2. 我们定义一个类,比如MyDialog。按照AlterDaialog构造再搞一遍

import "package:flutter/material.dart";

class MyDailog extends StatelessWidget {
  final Widget content;

  final List<Widget> actions;

  final Color backgroundColor;

  final double elevation;

  final String semanticLabel;

  final ShapeBorder shape;

  const MyDailog({
    Key key,
    this.content,
    this.actions,
    this.backgroundColor,
    this.elevation,
    this.semanticLabel,
    this.shape,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      content: content,
      actions: actions,
      backgroundColor: backgroundColor,
      elevation: elevation,
      shape: shape,
    );
  }
}
复制代码

####3.效果和之前显示的一样,只是我们把标题去掉了。我们做了简单的封装,就实现了自己的Dialog。 ####4.我们看到使用showDialog要写很长的代码,如果我们自定义那岂不是更长,能不能把showDialog简化一下,ojbk,接着搞!

import "package:flutter/material.dart";

void showMyDialog({
  @required Widget content,
  TextStyle contentTextStyle,
  List<Widget> actions,
  Color backgroundColor,
  double elevation,
  String semanticLabel,
  ShapeBorder shape,
  bool barrierDismissible = true,
  @required BuildContext context,
}) {
  assert(context != null);
  assert(content != null);
  showDialog<void>(
    context: context,
    barrierDismissible: barrierDismissible,
    builder: (BuildContext context) {
      return MyDailog(
        content: content,
        backgroundColor: backgroundColor,
        elevation: elevation,
        semanticLabel: semanticLabel,
        shape: shape,
        actions: actions,
      );
    },
  );
}

class MyDailog extends StatelessWidget {
  final Widget content;
  此处省略,参照前........
复制代码

####4.我们使用一下

RaisedButton(
         child: Text("showDialog"),
         onPressed: () {
                  showMyDialog(
                    content: SingleChildScrollView(
                      child: ListBody(
                        children: <Widget>[
                          Text('You will never be satisfied.'),
                          Text('You\’re like me. I’m never satisfied.'),
                        ],
                      ),
                    ),
                    actions: <Widget>[
                      FlatButton(
                        child: Text('Regret'),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                    ],
                    context: context,
                  );
                },
复制代码

######搞定,和上面的显示一模一样。 ##以上就是今天自定义的Dialog的全部内容。顺便在这儿推广一下的我的Dialog库 ,后续还在添加中。。。。。


以上所述就是小编给大家介绍的《flutter:教你自定义Dialog》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Photoshop Anthology

The Photoshop Anthology

Corrie Haffly / SitePoint Pty. Ltd. / 2006 / USD 39.95

The Photoshop Anthology is full-color, question-and-answer book for Web Designers who want to use Photoshop to build Websites and create better looking web graphics more effectively. The book covers: ......一起来看看 《The Photoshop Anthology》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具