Flutter自定義彈窗Dialog效果
本文實例為大家分享了Flutter自定義彈窗Dialog效果的具體代碼,供大家參考,具體內(nèi)容如下
主要是基于系統(tǒng)的dialog機(jī)制做一些使用限制和自定義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,
? ? ? ? );
? ? ? },
? ? ? //不設(shè)置會默認(rèn)使用屏幕最大寬度而不是子組件寬度
? ? ? 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) {
? ? ? //延時關(guān)閉
? ? ? Timer(const Duration(seconds: 2), () => dismiss());
?
? ? ? return Container(
? ? ? ? width: 200,
? ? ? ? height: 100,
? ? ? ? color: Colors.yellow,
? ? ? ? child: const Center(child: Text("等待"),),
? ? ? );
? ? }, cancellable: true);
? }
}其中show方法中使用到的Dialog默認(rèn)使用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,
? ? ? ? ? ),
? ? ? ? ),
? ? ? ),
? ? );
? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android App開發(fā)中使用RecyclerView實現(xiàn)Gallery畫廊的實例
這篇文章主要介紹了Android App開發(fā)中使用RecyclerView實現(xiàn)Gallery畫廊的實例,比普通的ListView實現(xiàn)的效果更為強(qiáng)大,需要的朋友可以參考下2016-04-04
Android Studio4.0解決Gradle下載超時問題
這篇文章主要介紹了Android Studio4.0解決Gradle下載超時問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Android帶清除功能的輸入框控件EditTextWithDel
這篇文章主要為大家詳細(xì)介紹了Android帶清除功能的輸入框控件EditTextWithDel,感興趣的小伙伴們可以參考一下2016-09-09
Android開發(fā)之進(jìn)度條ProgressBar的示例代碼
本篇文章主要介紹了Android開發(fā)之進(jìn)度條ProgressBar的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
android傳送照片到FTP服務(wù)器的實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了android傳送照片到FTP服務(wù)器的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Android第三方開源下拉框NiceSpinner使用詳解
這篇文章主要為大家詳細(xì)介紹了Android第三方開源下拉框NiceSpinner的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

