網站首頁 編程語言 正文
本文實例為大家分享了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 ? 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組件,有其他需求也可在此定制,為什么不直接放棄使用此組件?主要涉及到其路由跳轉中的一些動畫布局之類的。
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-09-01 ASP.NET輕量級MVC框架Nancy的基本用法_實用技巧
- 2022-08-01 React中的Hooks進階理解教程_React
- 2022-05-12 Android 截屏實現、屏幕截圖、MediaProjection、ImageReader
- 2022-09-13 Android四大組件之Activity深入解讀生命周期_Android
- 2022-01-16 對npm模塊進行調試和測試——npm link
- 2022-10-08 ASP.NET泛型一之泛型簡介與基本語法_實用技巧
- 2022-12-07 C++?二維(多維)vector添加一個空項問題_C 語言
- 2022-06-18 Elasticsearches通過坐標位置實現對附近人的搜索_其它綜合
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支