Flutter学习日记之自定义封装Dialog

本文地址:https://blog.csdn.net/qq_40785165/article/details/118035843,转载需附上此地址

大家好,我是小黑,一个还没秃头的程序员~~~

海到无边天作岸,山登绝顶我为峰。

今天分享的内容是关于Dialog的封装自定义,源码地址:https://gitee.com/fjjxxy/flutter-study.git,效果如下:

源码项目中这个Dialog的类是CommonDialog,参数如下:

参数说明
title标题
titleStyle标题文字的样式
content内容
contentStyle内容样式
cancelText取消按钮的文字
confirmText确认按钮的文字
onConfirm确认的点击事件
onCancel取消的点击事件
cancelBtnStyle取消按钮的样式
confirmBtnStyle确认按钮的样式
cancelTextStyle取消按钮的文字样式
confirmTextStyle确认按钮的文字样式
imageNetUrl顶部图片的网络地址
imageAssetsUrl顶部图片的本地地址

注:imageNetUrl与imageAssetsUrl不能同时出现,断言如下:

assert(!(imageNetUrl != null && imageAssetsUrl != null))

构造函数如下:两个点击事件是必选的参数

  CommonDialog(
      {@required this.onConfirm,
      @required this.onCancel,
      this.title = "提示",
      this.content = "",
      this.titleStyle,
      this.contentStyle,
      this.cancelText = "取消",
      this.confirmText = "确定",
      this.cancelBtnStyle,
      this.confirmBtnStyle,
      this.cancelTextStyle,
      this.confirmTextStyle,
      this.imageNetUrl,
      this.imageAssetsUrl})
      : assert(!(imageNetUrl != null && imageAssetsUrl != null));

完整的对话组件的代码如下:

class CommonDialog extends Dialog {
  var title;
  var titleStyle;
  var content;
  var contentStyle;
  var cancelText;
  var confirmText;
  var onConfirm;
  var onCancel;
  var cancelBtnStyle;
  var confirmBtnStyle;
  var cancelTextStyle;
  var confirmTextStyle;
  var imageNetUrl;
  var imageAssetsUrl;

  CommonDialog(
      {@required this.onConfirm,
      @required this.onCancel,
      this.title = "提示",
      this.content = "",
      this.titleStyle,
      this.contentStyle,
      this.cancelText = "取消",
      this.confirmText = "确定",
      this.cancelBtnStyle,
      this.confirmBtnStyle,
      this.cancelTextStyle,
      this.confirmTextStyle,
      this.imageNetUrl,
      this.imageAssetsUrl})
      : assert(!(imageNetUrl != null && imageAssetsUrl != null));

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Material(
      type: MaterialType.transparency, //透明类型
      child: Center(
        child: Container(
          height: 250,
          width: double.infinity,
          margin: EdgeInsets.fromLTRB(20, 0, 20, 0),
          decoration: BoxDecoration(
            color: ColorUtils.color_white,
            borderRadius: BorderRadius.circular(10),
            border: Border.all(color: ColorUtils.color_white),
          ),
          child: Padding(
            padding: EdgeInsets.all(20),
            child: Column(
              children: [
                imageAssetsUrl == null
                    ? imageNetUrl != null
                        ? Image.network(
                            imageNetUrl,
                            width: 40,
                            height: 40,
                          )
                        : SizedBox(
                            height: 0,
                          )
                    : Image.asset(
                        imageAssetsUrl,
                        width: 40,
                        height: 40,
                      ),
                imageAssetsUrl != null || imageNetUrl != null
                    ? SizedBox(
                        height: 20,
                      )
                    : SizedBox(
                        height: 0,
                      ),
                Text(
                  "$title",
                  style: this.titleStyle ??
                      TextStyle(fontSize: 20, color: Colors.black),
                ),
                SizedBox(
                  height: 20,
                ),
                Expanded(
                    child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                      Expanded(
                          child: Text(
                        "$content",
                        style: this.contentStyle ??
                            TextStyle(fontSize: 15, color: Colors.black),
                      ))
                    ])),
                Container(
                  width: double.infinity,
                  padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
                  child: Row(
                    children: [
                      Container(
                        height: 40,
                        width: 100,
                        child: OutlinedButton(
                            onPressed: onCancel,
                            child: Text(
                              "$cancelText",
                              style: cancelTextStyle ?? TextStyle(fontSize: 15),
                            ),
                            style: cancelBtnStyle ??
                                ButtonStyle(
                                    foregroundColor: MaterialStateProperty.all(
                                        ColorUtils.color_blue_0C84FF),
                                    backgroundColor: MaterialStateProperty.all(
                                        ColorUtils.color_blue_EEF6FF),
                                    shape: MaterialStateProperty.all(
                                        RoundedRectangleBorder(
                                            borderRadius:
                                                BorderRadius.circular(20))),
                                    side: MaterialStateProperty.all(BorderSide(
                                        color: ColorUtils.color_white)))),
                      ),
                      Expanded(child: Text("")),
                      Container(
                        height: 40,
                        width: 100,
                        child: OutlinedButton(
                            onPressed: onConfirm,
                            child: Text(
                              "$confirmText",
                              style:
                                  confirmTextStyle ?? TextStyle(fontSize: 15),
                            ),
                            style: confirmBtnStyle ??
                                ButtonStyle(
                                    foregroundColor: MaterialStateProperty.all(
                                        ColorUtils.color_white),
                                    backgroundColor: MaterialStateProperty.all(
                                        ColorUtils.color_blue_0C84FF),
                                    shape: MaterialStateProperty.all(
                                        RoundedRectangleBorder(
                                            borderRadius:
                                                BorderRadius.circular(20))),
                                    side: MaterialStateProperty.all(BorderSide(
                                        color: ColorUtils.color_white)))),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

如何使用?

                showDialog(
                    context: context,
                    builder: (context) {
                      return CommonDialog(
                        title: "温馨提示",
                        titleStyle:
                            TextStyle(color: Colors.black, fontSize: 20),
                        content: "公共场所请勿抽烟",
                        contentStyle:
                            TextStyle(color: Colors.black, fontSize: 15),
                        confirmText: "下一步",
                        cancelText: "取消",
                        onCancel: () {
                          Toast.toast(context, msg: "取消");
                          Navigator.pop(context);
                        },
                        onConfirm: () {
                          Toast.toast(context, msg: "确定");
                          Navigator.pop(context);
                        },
                        cancelBtnStyle: ButtonStyle(
                            foregroundColor: MaterialStateProperty.all(
                                ColorUtils.color_blue_0C84FF),
                            backgroundColor: MaterialStateProperty.all(
                                ColorUtils.color_blue_EEF6FF),
                            shape: MaterialStateProperty.all(
                                RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(20))),
                            side: MaterialStateProperty.all(
                                BorderSide(color: ColorUtils.color_white))),
                        confirmBtnStyle:                                 ButtonStyle(
                            foregroundColor: MaterialStateProperty.all(
                                ColorUtils.color_white),
                            backgroundColor: MaterialStateProperty.all(
                                ColorUtils.color_blue_0C84FF),
                            shape: MaterialStateProperty.all(
                                RoundedRectangleBorder(
                                    borderRadius:
                                    BorderRadius.circular(20))),
                            side: MaterialStateProperty.all(BorderSide(
                                color: ColorUtils.color_white))),
                        cancelTextStyle: TextStyle(fontSize: 15),
                        confirmTextStyle: TextStyle(fontSize: 15),
                        // imageNetUrl: "https://profile.csdnimg.cn/D/6/7/0_qq_40785165",
                        imageAssetsUrl: "assets/images/icon_warn.png",
                      );
                    });

以上的参数除了事件以外可以不定义,代码仅供参考
到此为止,封装的对话框就可以拿来用了,要是觉得有用记得点个赞,感兴趣的小伙伴可以下载源码看一下,希望大家可以点个Star,支持一下小白的flutter学习经历,最后,希望喜欢我文章的朋友们可以帮忙点赞、收藏,也可以关注一下,如果有问题可以在评论区提出,后面我会持续更新Flutter的学习记录,与大家分享,谢谢大家的支持与阅读!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一个简单的自定义列表块封装示例。以下是一个基本的代码示例: ```dart import 'package:flutter/material.dart'; class CustomListItem extends StatelessWidget { CustomListItem({ this.title, this.subtitle, this.thumbnail, }); final String title; final String subtitle; final Widget thumbnail; @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(vertical: 10.0), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ thumbnail, Expanded( child: Padding( padding: const EdgeInsets.fromLTRB(20.0, 0.0, 2.0, 0.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( title, style: const TextStyle( fontWeight: FontWeight.bold, fontSize: 18.0, ), ), const Padding(padding: EdgeInsets.symmetric(vertical: 2.0)), Text( subtitle, style: const TextStyle(fontSize: 16.0), ), ], ), ), ) ], ), ); } } ``` 您可以在需要使用自定义的列表块的地方使用此小部件。例如,以下是一个使用此自定义列表块的示例: ```dart class MyCustomList extends StatelessWidget { @override Widget build(BuildContext context) { return ListView( padding: const EdgeInsets.all(10.0), children: <Widget>[ CustomListItem( title: 'Flutter', subtitle: 'Google’s UI toolkit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase', thumbnail: Container( decoration: const BoxDecoration(color: Colors.blue), ), ), CustomListItem( title: 'Dart', subtitle: 'A client-optimized language for fast apps on any platform', thumbnail: Container( decoration: const BoxDecoration(color: Colors.green), ), ), CustomListItem( title: 'Material Design', subtitle: 'A design system for building beautiful, natively compiled applications for mobile, web, and desktop', thumbnail: Container( decoration: const BoxDecoration(color: Colors.orange), ), ), ], ); } } ``` 这个示例中,我们使用 `MyCustomList` 小部件来构建一个包含三个自定义列表块的列表。每个列表块都包含一个缩略图、标题和子标题。请注意,我们可以在自定义列表块中使用任何小部件,以便根据自己的需求进行自定义
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值