網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Flutter自定義彈窗Dialog效果的具體代碼,供大家參考,具體內(nèi)容如下
主要是基于系統(tǒng)的dialog機制做一些使用限制和自定義UI的優(yōu)化
核心代碼:
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 ? StatecreateState() => _DialogTestState(); } ? class _DialogTestState extends State { ? @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組件,有其他需求也可在此定制,為什么不直接放棄使用此組件?主要涉及到其路由跳轉(zhuǎn)中的一些動畫布局之類的。
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, ? ? ? ? ? ), ? ? ? ? ), ? ? ? ), ? ? ); ? } }
原文鏈接:https://blog.csdn.net/weixin_41735943/article/details/123249557
相關推薦
- 2022-05-10 關于react中的state整理
- 2022-06-07 C++11中跳轉(zhuǎn)initializer_list實現(xiàn)分析_C 語言
- 2023-01-12 python列表添加元素append(),extend(),insert(),+list的區(qū)別及說明
- 2022-10-29 umi pro-layout : 某個頁面 禁用/移除 pro-layout ( 比如: 登錄頁不需
- 2022-10-13 詳解python-opencv?常用函數(shù)_python
- 2022-04-15 詳解Python?prometheus_client使用方式_python
- 2023-04-12 Python機器學習利用鳶尾花數(shù)據(jù)繪制ROC和AUC曲線_python
- 2022-08-26 C++超集C++/CLI模塊的基本類型_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支