Skip to content

Commit

Permalink
返回值调整优化
Browse files Browse the repository at this point in the history
  • Loading branch information
shirne committed Jul 20, 2021
1 parent 641ea8e commit 97ed8bb
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 92 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.4.0]
* 返回值优化

## [1.3.0]
* add imagePreview
* add color settings on progress widget
Expand Down
2 changes: 1 addition & 1 deletion example/linux/my_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.3.0"
version: "1.4.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down
2 changes: 1 addition & 1 deletion example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>

<title>example</title>
<title>Shirne Dialog Demo</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
Expand Down
4 changes: 2 additions & 2 deletions example/windows/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 0 additions & 18 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,6 @@ class ProgressController extends DialogController<int> {
}
}

/// controller of [MyDialog.modal]
class ModalController extends DialogController<int> {
ModalController(BuildContext context, [ValueNotifier<int>? 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<int> {
OverlayEntry? entry;
Expand Down
87 changes: 43 additions & 44 deletions lib/src/my_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ class MyDialog {

/// show a confirm Modal box.
/// the [message] may be a [Widget] or [String]
Future<bool?>? confirm(dynamic message,
{String buttonText = 'OK',
String title = '',
String cancelText = 'Cancel'}) {
late ModalController controller;
Completer completer = Completer<bool>();
controller = modal(
Future<bool?> confirm(
dynamic message, {
String buttonText = 'OK',
String title = '',
String cancelText = 'Cancel',
bool barrierDismissible = true,
Color? barrierColor = Colors.black54,
}) {
return modal<bool>(
message is Widget
? message
: ListBody(
Expand All @@ -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<bool?>?;
barrierDismissible: barrierDismissible,
barrierColor: barrierColor,
);
}

/// show a small modal with one button which text is `buttonText`.
/// the `message` may be a [Widget] or [String]
Future<void> alert(message, {String buttonText = 'OK', String title = ''}) {
late ModalController controller;
Completer completer = Completer<bool>();
controller = modal(
Future<bool?> alert(
message, {
String buttonText = 'OK',
String title = '',
bool barrierDismissible = true,
Color? barrierColor = Colors.black54,
}) {
return modal<bool>(
message is Widget
? message
: ListBody(
Expand All @@ -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<Widget> buttons,
{String title = '',
barrierDismissible = false,
Color? barrierColor = Colors.black54}) {
ModalController controller = ModalController(context);
controller.result = showDialog<dynamic>(
Future<T?> modal<T>(
Widget body,
List<Widget> buttons, {
String title = '',
bool barrierDismissible = false,
Color? barrierColor = Colors.black54,
}) {
return showDialog<T>(
context: context,
barrierDismissible: barrierDismissible,
barrierColor: barrierColor,
Expand All @@ -145,15 +149,15 @@ class MyDialog {
);
},
);
return controller;
}

DialogController imagePreview(List<String> images,
{String? currentImage,
barrierDismissible = true,
Color? barrierColor = Colors.black54}) {
ModalController controller = ModalController(context);
controller.result = showDialog<dynamic>(
Future<dynamic> imagePreview(
List<String> images, {
String? currentImage,
bool barrierDismissible = true,
Color? barrierColor = Colors.black54,
}) {
return showDialog<dynamic>(
context: context,
barrierDismissible: barrierDismissible,
barrierColor: barrierColor,
Expand All @@ -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<T?> popup<T>(Widget body,
{barrierDismissible = false,
double height = 0,
double borderRound = 10,
Expand All @@ -183,8 +186,7 @@ class MyDialog {
Icons.cancel,
color: Colors.black38,
)}) {
ModalController controller = ModalController(context);
controller.result = showModalBottomSheet<dynamic>(
return showModalBottomSheet<T>(
backgroundColor: Colors.transparent,
barrierColor: barrierColor,
context: context,
Expand All @@ -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].
Expand Down
26 changes: 2 additions & 24 deletions lib/src/popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -38,30 +34,12 @@ class _PopupWidgetState extends State<PopupWidget> {
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;
Expand Down Expand Up @@ -91,7 +69,7 @@ class _PopupWidgetState extends State<PopupWidget> {
child: widget.showClose
? GestureDetector(
onTap: () {
_close();
Navigator.pop(context);
},
child: Padding(
padding: EdgeInsets.all(8),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 97ed8bb

Please sign in to comment.