返回顶部
首页 > 资讯 > 移动开发 >Flutter自定义弹窗Dialog效果
  • 463
分享到

Flutter自定义弹窗Dialog效果

2024-04-02 19:04:59 463人浏览 安东尼
摘要

本文实例为大家分享了Flutter自定义弹窗Dialog效果的具体代码,供大家参考,具体内容如下 主要是基于系统的dialog机制做一些使用限制和自定义UI的优化 核心代码: cl

本文实例为大家分享了Flutter自定义弹窗Dialog效果的具体代码,供大家参考,具体内容如下

主要是基于系统的dialog机制做一些使用限制和自定义UI的优化

核心代码:

class CustomDialog {
 
  static void show(BuildContext context, Widget Function(BuildContext ctx, void Function() dismiss)builder, {bool cancellable = false}) {
    showDialog(
      context: context,
      barrierDismissible: cancellable,
      builder: (ctx) {
        return WillPopScope(
          child: Dialog(
            child: builder(ctx, () => Navigator.of(ctx).pop()),
            backgroundColor: Colors.transparent,
            shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(0.0))),
            elevation: 0,
            alignment: Alignment.center,
          ),
          onWillPop: () async => cancellable,
        );
      },
    );
  }
 
 
  static void showBottomSheet(BuildContext context, Widget Function(BuildContext ctx, void Function() dismiss)builder, {bool cancellable = true}) {
 
    showModalBottomSheet(
      context: context,
      isDismissible: cancellable,
      enableDrag: cancellable,
      isScrollControlled: true,
      builder: (BuildContext ctx) {
        return WillPopScope(
          child: builder(ctx, () => Navigator.of(ctx).pop()),
          onWillPop: () async => cancellable,
        );
      },
      //不设置会默认使用屏幕最大宽度而不是子组件宽度
      constraints: const BoxConstraints(minWidth: 0, minHeight: 0, maxWidth: double.infinity, maxHeight: double.infinity),
      backgroundColor: Colors.transparent,
    );
  }
}

使用:

import 'dart:async';
import 'package:flutter/material.dart';
 
class DialogTestPage extends StatefulWidget {
  const DialogTestPage({Key? key}) : super(key: key);
 
  @override
  State<StatefulWidget> createState() => _DialogTestState();
}
 
class _DialogTestState extends State<DialogTestPage> {
  @override
  Widget build(BuildContext context) {
 
    return Scaffold(
      body: Column(children: [
        const SizedBox(height: 0, width: double.infinity,),
        TextButton(
          child: const Text("show dialog"),
          onPressed: () => showCustomDialog(),
        ),
        TextButton(
          child: const Text("show delay dialog"),
          onPressed: () => showDelayDialog(),
        ),
        TextButton(
          child: const Text("show sheet"),
          onPressed: () => showSheetDialog(),
        ),
      ], mainAxisAlignment: MainAxisAlignment.spaceAround, crossAxisAlignment: CrossAxisAlignment.center,),
    );
  }
 
 
  void showCustomDialog() {
    CustomDialog.show(context, (context, dismiss) {
      return Container(
        width: 200,
        height: 100,
        color: Colors.white,
        child: Center(child: TextButton(onPressed: () => dismiss(), child: const Text("POP")),),
      );
    });
  }
 
  void showSheetDialog() {
    CustomDialog.showBottomSheet(context, (ctx, dismiss) {
      return Container(
        height: 300,
        color: Colors.white,
        child: Center(child: TextButton(onPressed: () => dismiss(), child: const Text("POP")),),
        margin: const EdgeInsets.all(20),
      );
    });
  }
 
  void showDelayDialog() {
    CustomDialog.show(context, (context, dismiss) {
      //延时关闭
      Timer(const Duration(seconds: 2), () => dismiss());
 
      return Container(
        width: 200,
        height: 100,
        color: Colors.yellow,
        child: const Center(child: Text("等待"),),
      );
    }, cancellable: true);
  }
}

其中show方法中使用到的Dialog默认使用material组件库的,如果觉得有不合理的尺寸约束,可替换我修改过的Dialog组件,有其他需求也可在此定制,为什么不直接放弃使用此组件?主要涉及到其路由跳转中的一些动画布局之类的。

class Dialog extends StatelessWidget {
 
  const Dialog({
    Key? key,
    this.backgroundColor,
    this.elevation,
    this.insetAnimationDuration = const Duration(milliseconds: 100),
    this.insetAnimationCurve = Curves.decelerate,
    this.insetPadding = const EdgeInsets.symmetric(horizontal: 40.0, vertical: 24.0),
    this.clipBehavior = Clip.none,
    this.shape,
    this.alignment,
    this.child,
  }) : super(key: key);
 
 
  final Color? backgroundColor;
  final double? elevation;
  final Duration insetAnimationDuration;
  final Curve insetAnimationCurve;
  final EdgeInsets? insetPadding;
  final Clip clipBehavior;
  final ShapeBorder? shape;
  final AlignmentGeometry? alignment;
  final Widget? child;
 
  static const RoundedRectangleBorder _defaultDialogShape =
  RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4.0)));
  static const double _defaultElevation = 24.0;
 
  @override
  Widget build(BuildContext context) {
    final DialogTheme dialogTheme = DialogTheme.of(context);
    final EdgeInsets effectivePadding = MediaQuery.of(context).viewInsets + (insetPadding ?? EdgeInsets.zero);
    return AnimatedPadding(
      padding: effectivePadding,
      duration: insetAnimationDuration,
      curve: insetAnimationCurve,
      child: MediaQuery.removeViewInsets(
        removeLeft: true,
        removeTop: true,
        removeRight: true,
        removeBottom: true,
        context: context,
        child: Align(
          alignment: alignment ?? dialogTheme.alignment ?? Alignment.center,
          child: Material(
            color: backgroundColor ?? dialogTheme.backgroundColor ?? Theme.of(context).dialogBackgroundColor,
            elevation: elevation ?? dialogTheme.elevation ?? _defaultElevation,
            shape: shape ?? dialogTheme.shape ?? _defaultDialogShape,
            type: MaterialType.card,
            clipBehavior: clipBehavior,
            child: child,
          ),
        ),
      ),
    );
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Flutter自定义弹窗Dialog效果

本文链接: https://www.lsjlt.com/news/143620.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • Flutter自定义弹窗Dialog效果
    本文实例为大家分享了Flutter自定义弹窗Dialog效果的具体代码,供大家参考,具体内容如下 主要是基于系统的dialog机制做一些使用限制和自定义UI的优化 核心代码: cl...
    99+
    2024-04-02
  • Android自定义弹框Dialog效果
    本文实例为大家分享了Android自定义弹框Dialog效果的具体代码,供大家参考,具体内容如下 1.dialog_delete.xml <xml version=...
    99+
    2024-04-02
  • Android自定义弹出框dialog效果
    项目要用到弹出框,还要和苹果的样式一样(Android真是没地位),所以就自己定义了一个,不是很像(主要是没图),但是也还可以。废话不多说了,直接上代码先看布局文件<?xml version="1.0" encoding="u...
    99+
    2023-05-31
    android 弹出框 dialog
  • Android 自定义View 之 Dialog弹窗
    Dialog弹窗 前言正文一、弹窗视图帮助类二、弹窗控制类三、监听接口四、样式五、简易弹窗六、常规使用七、简易使用八、源码 前言   在日常开发中用到弹窗是比较多的,常用于提示作用,比如错误操作提示,余额不足提示,退出登录提...
    99+
    2023-08-18
    自定义Dialog 简易提示弹窗 EasyDialog
  • Android怎么自定义弹框Dialog效果
    今天小编给大家分享一下Android怎么自定义弹框Dialog效果的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。具体效果如下...
    99+
    2023-06-30
  • Android自定义弹窗提示效果
    本文实例为大家分享了Android 自定义弹窗提示的具体代码,供大家参考,具体内容如下 Java文件: private void showSetDeBugDialog() { ...
    99+
    2024-04-02
  • 微信小程序原生自定义弹窗效果
    背景 微信小程序原生的在弹出层wx.showModal中可以通过配置项editable配置输入框,但是对于微信的版本有限制,微信版本过低无法显示,所以需要实现弹出层的效果 如下图 ...
    99+
    2024-04-02
  • Flutter实现底部弹窗效果
    目录实现效果代码结构基本使用自定义底部弹窗总结在实际开发过程中,经常会用到底部弹窗来进行快捷操作,例如选择一个选项,选择下一步操作等等。在 Flutter 中提供了一个 showMo...
    99+
    2024-04-02
  • Flutter自定义搜索框效果
    本文实例为大家分享了Flutter自定义搜索框效果的具体代码,供大家参考,具体内容如下 效果 实现方式 import 'package:dio/dio.dart'; impor...
    99+
    2024-04-02
  • 【Flutter入门到进阶】Flutter基础篇---弹窗Dialog
    1 AlertDialog 1.1 说明         最简单的方案是利用AlertDialog组件构建一个弹框 1.2 示例 void alertDialog(BuildContext context) async {  var res...
    99+
    2023-10-20
    flutter android 开发语言
  • Flutter自定义Appbar搜索框效果
    本文实例为大家分享了Flutter自定义Appbar搜索框效果的具体代码,供大家参考,具体内容如下 首先看一下实现本次实现的效果。 本来想直接偷懒从flutter pub上找个能用...
    99+
    2024-04-02
  • javascript+html5+css3如何实现自定义弹出窗口效果
    这篇文章主要介绍javascript+html5+css3如何实现自定义弹出窗口效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!效果图:源码:1.demo.jsp<%@&nb...
    99+
    2024-04-02
  • vue自定义气泡弹窗
    本文实例为大家分享了vue自定义气泡弹窗的具体代码,供大家参考,具体内容如下 src/components/myComponents/pop/pop.vue <templat...
    99+
    2024-04-02
  • 微信小程序自定义Dialog弹框
    本文实例为大家分享了微信小程序自定义Dialog弹框的具体代码,供大家参考,具体内容如下 一、创建组件 1、在根目录下自定义一个components文件夹,用来存放自定义的组件。2...
    99+
    2024-04-02
  • uni-app中弹窗的使用与自定义弹窗
    目录一、uni-app中自带的弹窗二、实例1、uni.showToast(OBJECT)(消息提示框)2、uni.showModal(OBJECT)(显示两个按钮的提示框)3、uni...
    99+
    2024-04-02
  • Android实现自定义的弹幕效果
    一、效果图 先来看看效果图吧~~ 二、实现原理方案 1、自定义ViewGroup-XCDanmuView,继承RelativeLayout来实现,当然也可以继承其他三大布局类...
    99+
    2022-06-06
    自定义 Android
  • C# winForm自定义弹出页面效果
    本文实例为大家分享了C# winForm自定义弹出页面效果的具体代码,供大家参考,具体内容如下 在C#的windows窗体应用程序中,添加弹出框效果.最后就是这样的效果. 页面For...
    99+
    2024-04-02
  • Android自定义scrollview实现回弹效果
    在ios手机上经常看到页面上下滑动回弹效果,安卓中没有原生控件支持,这里自己就去自定义一个scrollview实现回弹效果 1. 新建MyScrollView并继承ScrollVie...
    99+
    2024-04-02
  • 小程序自定义弹出框效果
    本文实例为大家分享了小程序自定义弹出框效果的具体代码,供大家参考,具体内容如下 my-pop----api: title ------字符串---------自定义弹窗标题con...
    99+
    2024-04-02
  • ionic如何实现自定义弹框效果
    这篇文章给大家分享的是有关ionic如何实现自定义弹框效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。思路利用ionic自带的弹框$ionicPopup。隐藏头部和尾部,只保留...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作