From 97ed8bbc44987cf2b29fbc89631d4447a8e4c0ec Mon Sep 17 00:00:00 2001 From: shirne Date: Tue, 20 Jul 2021 23:14:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++ example/linux/my_application.cc | 2 +- example/pubspec.lock | 2 +- example/web/index.html | 2 +- example/windows/runner/main.cpp | 4 +- lib/src/controller.dart | 18 ------- lib/src/my_dialog.dart | 87 ++++++++++++++++----------------- lib/src/popup.dart | 26 +--------- pubspec.yaml | 2 +- 9 files changed, 54 insertions(+), 92 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed28221..5f736b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [1.4.0] + * 返回值优化 + ## [1.3.0] * add imagePreview * add color settings on progress widget diff --git a/example/linux/my_application.cc b/example/linux/my_application.cc index 543eaca..b789270 100644 --- a/example/linux/my_application.cc +++ b/example/linux/my_application.cc @@ -48,7 +48,7 @@ static void my_application_activate(GApplication* application) { gtk_window_set_title(window, "example"); } - gtk_window_set_default_size(window, 1280, 720); + gtk_window_set_default_size(window, 700, 720); gtk_widget_show(GTK_WIDGET(window)); g_autoptr(FlDartProject) project = fl_dart_project_new(); diff --git a/example/pubspec.lock b/example/pubspec.lock index 60025ba..4128a38 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -94,7 +94,7 @@ packages: path: ".." relative: true source: path - version: "1.3.0" + version: "1.4.0" sky_engine: dependency: transitive description: flutter diff --git a/example/web/index.html b/example/web/index.html index 1460b5e..e59f02d 100644 --- a/example/web/index.html +++ b/example/web/index.html @@ -26,7 +26,7 @@ - example + Shirne Dialog Demo diff --git a/example/windows/runner/main.cpp b/example/windows/runner/main.cpp index b637809..089d573 100644 --- a/example/windows/runner/main.cpp +++ b/example/windows/runner/main.cpp @@ -29,8 +29,8 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, FlutterWindow window(&run_loop, project); Win32Window::Point origin(10, 10); - Win32Window::Size size(1280, 720); - if (!window.CreateAndShow(L"example", origin, size)) { + Win32Window::Size size(700, 720); + if (!window.CreateAndShow(L"Shirne Dialog Demo", origin, size)) { return EXIT_FAILURE; } window.SetQuitOnClose(true); diff --git a/lib/src/controller.dart b/lib/src/controller.dart index 31a1873..0b2a500 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -44,24 +44,6 @@ class ProgressController extends DialogController { } } -/// controller of [MyDialog.modal] -class ModalController extends DialogController { - ModalController(BuildContext context, [ValueNotifier? notifier]) - : super.of(context, notifier); - - open() {} - - update(int value) {} - - close() { - remove(); - } - - remove() { - Navigator.pop(context); - } -} - /// controller of any popup use [Overlay] exp. [MyDialog.snack] class EntryController extends DialogController { OverlayEntry? entry; diff --git a/lib/src/my_dialog.dart b/lib/src/my_dialog.dart index 3c6c114..1342fa9 100644 --- a/lib/src/my_dialog.dart +++ b/lib/src/my_dialog.dart @@ -59,13 +59,15 @@ class MyDialog { /// show a confirm Modal box. /// the [message] may be a [Widget] or [String] - Future? confirm(dynamic message, - {String buttonText = 'OK', - String title = '', - String cancelText = 'Cancel'}) { - late ModalController controller; - Completer completer = Completer(); - controller = modal( + Future confirm( + dynamic message, { + String buttonText = 'OK', + String title = '', + String cancelText = 'Cancel', + bool barrierDismissible = true, + Color? barrierColor = Colors.black54, + }) { + return modal( message is Widget ? message : ListBody( @@ -77,30 +79,31 @@ class MyDialog { [ TextButton( onPressed: () { - controller.close(); - completer.complete(false); + Navigator.pop(context, false); }, child: Text(cancelText)), ElevatedButton( onPressed: () { - controller.close(); - completer.complete(true); + Navigator.pop(context, true); }, child: Text(buttonText)), ], title: title, - ) as ModalController; - controller.result = completer.future; - - return controller.result as Future?; + barrierDismissible: barrierDismissible, + barrierColor: barrierColor, + ); } /// show a small modal with one button which text is `buttonText`. /// the `message` may be a [Widget] or [String] - Future alert(message, {String buttonText = 'OK', String title = ''}) { - late ModalController controller; - Completer completer = Completer(); - controller = modal( + Future alert( + message, { + String buttonText = 'OK', + String title = '', + bool barrierDismissible = true, + Color? barrierColor = Colors.black54, + }) { + return modal( message is Widget ? message : ListBody( @@ -112,26 +115,27 @@ class MyDialog { [ ElevatedButton( onPressed: () { - controller.close(); - completer.complete(); + Navigator.pop(context, true); }, child: Text(buttonText), ), ], title: title, - ) as ModalController; - - return completer.future; + barrierDismissible: barrierDismissible, + barrierColor: barrierColor, + ); } /// show a modal witch content is `body`,with any `buttons`. /// The modal title will be hidden if `title` isEmpty - DialogController modal(Widget body, List buttons, - {String title = '', - barrierDismissible = false, - Color? barrierColor = Colors.black54}) { - ModalController controller = ModalController(context); - controller.result = showDialog( + Future modal( + Widget body, + List buttons, { + String title = '', + bool barrierDismissible = false, + Color? barrierColor = Colors.black54, + }) { + return showDialog( context: context, barrierDismissible: barrierDismissible, barrierColor: barrierColor, @@ -145,15 +149,15 @@ class MyDialog { ); }, ); - return controller; } - DialogController imagePreview(List images, - {String? currentImage, - barrierDismissible = true, - Color? barrierColor = Colors.black54}) { - ModalController controller = ModalController(context); - controller.result = showDialog( + Future imagePreview( + List images, { + String? currentImage, + bool barrierDismissible = true, + Color? barrierColor = Colors.black54, + }) { + return showDialog( context: context, barrierDismissible: barrierDismissible, barrierColor: barrierColor, @@ -164,11 +168,10 @@ class MyDialog { ); }, ); - return controller; } /// show a modal popup with `body` witch width will fill the screen - DialogController popup(Widget body, + Future popup(Widget body, {barrierDismissible = false, double height = 0, double borderRound = 10, @@ -183,8 +186,7 @@ class MyDialog { Icons.cancel, color: Colors.black38, )}) { - ModalController controller = ModalController(context); - controller.result = showModalBottomSheet( + return showModalBottomSheet( backgroundColor: Colors.transparent, barrierColor: barrierColor, context: context, @@ -198,14 +200,11 @@ class MyDialog { borderRound: borderRound, backgroundColor: backgroundColor, padding: padding, - controller: controller, showClose: showClose, closeButton: closeButton, ); }, ); - - return controller; } /// show a loading progress within an [OverlayEntry]. diff --git a/lib/src/popup.dart b/lib/src/popup.dart index 8c9bd0b..2948b59 100644 --- a/lib/src/popup.dart +++ b/lib/src/popup.dart @@ -2,15 +2,12 @@ library shirne_dialog; import 'package:flutter/material.dart'; -import 'controller.dart'; - /// A popup Widget wrapper class PopupWidget extends StatefulWidget { final Widget? child; final double? height; final double borderRound; final EdgeInsetsGeometry padding; - final DialogController? controller; final bool showClose; final Widget closeButton; final Color backgroundColor; @@ -19,7 +16,6 @@ class PopupWidget extends StatefulWidget { Key? key, this.child, this.height, - this.controller, this.borderRound = 10, this.padding = const EdgeInsets.all(10), this.backgroundColor = Colors.white, @@ -38,30 +34,12 @@ class _PopupWidgetState extends State { void initState() { super.initState(); if (widget.height != null) height = widget.height!; - - if (widget.controller!.notifier != null) { - widget.controller!.notifier!.addListener(_onController); - } - } - - void _onController() { - if (widget.controller!.notifier!.value == 101) { - print('will close'); - _close(); - } - } - - void _close() { - if (widget.controller!.notifier != null) { - widget.controller!.notifier!.removeListener(_onController); - } - widget.controller!.remove(); } @override Widget build(BuildContext context) { var size = MediaQuery.of(context).size; - print(size); + if (height <= 0) { // 按具体高度 height = size.height * 0.8; @@ -91,7 +69,7 @@ class _PopupWidgetState extends State { child: widget.showClose ? GestureDetector( onTap: () { - _close(); + Navigator.pop(context); }, child: Padding( padding: EdgeInsets.all(8), diff --git a/pubspec.yaml b/pubspec.yaml index a4d8c59..c5192c0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: shirne_dialog description: A package to use alert, confirm, toast, imagePreview, snack and popup within one line code. -version: 1.3.0 +version: 1.4.0 homepage: https://www.shirne.com/demo/easydialog/ repository: https://github.com/shirne/shirne_dialog