From 0b0fb4f1455c413ecf0274a08466fe20ecea51f2 Mon Sep 17 00:00:00 2001 From: open-trade Date: Thu, 19 Nov 2020 21:59:49 +0800 Subject: [PATCH] refactor alertDialog --- flutter_hbb/lib/common.dart | 138 ++++++------------------------- flutter_hbb/lib/model.dart | 19 ++--- flutter_hbb/lib/remote_page.dart | 95 ++++++++++++++++++++- flutter_hbb/pubspec.lock | 14 ++++ flutter_hbb/pubspec.yaml | 1 + 5 files changed, 137 insertions(+), 130 deletions(-) diff --git a/flutter_hbb/lib/common.dart b/flutter_hbb/lib/common.dart index 3d7408a5b..43de33e3e 100644 --- a/flutter_hbb/lib/common.dart +++ b/flutter_hbb/lib/common.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'dart:async'; import 'package:flutter_easyloading/flutter_easyloading.dart'; -import 'model.dart'; +import 'package:tuple/tuple.dart'; class HexColor extends Color { HexColor(final String hexColor) : super(_getColorFromHex(hexColor)); @@ -41,65 +41,23 @@ void showSuccess(String text) { bool _hasDialog = false; // https://material.io/develop/flutter/components/dialogs -Future enterPasswordDialog(String id, BuildContext context) async { +Future showAlertDialog( + BuildContext context, + Tuple3> Function( + void Function(void Function())) + build) async { dismissLoading(); if (_hasDialog) { Navigator.pop(context); } _hasDialog = true; - final controller = TextEditingController(); - var remember = FFI.getByName('remember', arg: id) == 'true'; var dialog = StatefulBuilder(builder: (context, setState) { + var widgets = build(setState); return AlertDialog( - title: Text('Please enter your password'), + title: widgets.item1, contentPadding: const EdgeInsets.all(20.0), - content: Column( - mainAxisSize: MainAxisSize.min, - children: [ - TextField( - autofocus: true, - obscureText: true, - controller: controller, - decoration: const InputDecoration( - labelText: 'Password', - ), - ), - ListTile( - title: Text( - 'Remember the password', - ), - leading: Checkbox( - value: remember, - onChanged: (v) { - setState(() { - remember = v; - }); - }, - ), - ), - ], - ), - actions: [ - FlatButton( - textColor: MyTheme.accent, - onPressed: () { - Navigator.pop(context); - Navigator.pop(context); - }, - child: Text('Cancel'), - ), - FlatButton( - textColor: MyTheme.accent, - onPressed: () { - var text = controller.text.trim(); - if (text == '') return; - FFI.login(text, remember); - showLoading('Logging in...'); - Navigator.pop(context); - }, - child: Text('OK'), - ), - ], + content: widgets.item2, + actions: widgets.item3, ); }); await showDialog( @@ -109,67 +67,17 @@ Future enterPasswordDialog(String id, BuildContext context) async { _hasDialog = false; } -Future wrongPasswordDialog(String id, BuildContext context) async { - dismissLoading(); - if (_hasDialog) { - Navigator.pop(context); - } - _hasDialog = true; - var dialog = AlertDialog( - title: Text('Wrong Password'), - contentPadding: const EdgeInsets.all(20.0), - content: Text('Do you want to enter again?'), - actions: [ - FlatButton( - textColor: MyTheme.accent, - onPressed: () { - Navigator.pop(context); - Navigator.pop(context); - }, - child: Text('Cancel'), - ), - FlatButton( - textColor: MyTheme.accent, - onPressed: () { - Navigator.pop(context); - enterPasswordDialog(id, context); - }, - child: Text('Retry'), - ), - ], - ); - await showDialog( - context: context, - barrierDismissible: false, - builder: (context) => dialog); - _hasDialog = false; -} - -Future msgbox( - String type, String title, String text, BuildContext context) async { - dismissLoading(); - if (_hasDialog) { - Navigator.pop(context); - } - _hasDialog = true; - var dialog = AlertDialog( - title: Text(title), - contentPadding: const EdgeInsets.all(20.0), - content: Text(text), - actions: [ - FlatButton( - textColor: MyTheme.accent, - onPressed: () { - Navigator.pop(context); - Navigator.pop(context); - }, - child: Text('OK'), - ), - ], - ); - await showDialog( - context: context, - barrierDismissible: false, - builder: (context) => dialog); - _hasDialog = false; +void msgbox(String type, String title, String text, BuildContext context) { + showAlertDialog( + context, + (_) => Tuple3(Text(title), Text(text), [ + FlatButton( + textColor: MyTheme.accent, + onPressed: () { + Navigator.pop(context); + Navigator.pop(context); + }, + child: Text('OK'), + ), + ])); } diff --git a/flutter_hbb/lib/model.dart b/flutter_hbb/lib/model.dart index 594e73617..f3ee99956 100644 --- a/flutter_hbb/lib/model.dart +++ b/flutter_hbb/lib/model.dart @@ -41,7 +41,11 @@ class FfiModel with ChangeNotifier { _decoding = false; } - void update(String id, BuildContext context) { + void update( + String id, + BuildContext context, + void Function(Map evt, String id, BuildContext context) + handleMsgbox) { for (;;) { var evt = FFI.popEvent(); if (evt == null) break; @@ -82,19 +86,6 @@ class FfiModel with ChangeNotifier { } } - void handleMsgbox(Map evt, String id, BuildContext context) { - var type = evt['type']; - var title = evt['title']; - var text = evt['text']; - if (type == 're-input-password') { - wrongPasswordDialog(id, context); - } else if (type == 'input-password') { - enterPasswordDialog(id, context); - } else { - msgbox(type, title, text, context); - } - } - void handleSwitchDisplay(Map evt) { _pi.currentDisplay = int.parse(evt['display']); _display.x = double.parse(evt['x']); diff --git a/flutter_hbb/lib/remote_page.dart b/flutter_hbb/lib/remote_page.dart index 007f31a98..8f881474a 100644 --- a/flutter_hbb/lib/remote_page.dart +++ b/flutter_hbb/lib/remote_page.dart @@ -7,6 +7,7 @@ import 'dart:async'; import 'dart:math' as math; import 'common.dart'; import 'model.dart'; +import 'package:tuple/tuple.dart'; class RemotePage extends StatefulWidget { RemotePage({Key key, this.id}) : super(key: key); @@ -45,7 +46,20 @@ class _RemotePageState extends State { } void interval() { - FFI.ffiModel.update(widget.id, context); + FFI.ffiModel.update(widget.id, context, handleMsgbox); + } + + void handleMsgbox(Map evt, String id, BuildContext context) { + var type = evt['type']; + var title = evt['title']; + var text = evt['text']; + if (type == 're-input-password') { + wrongPasswordDialog(id, context); + } else if (type == 'input-password') { + enterPasswordDialog(id, context); + } else { + msgbox(type, title, text, context); + } } @override @@ -169,3 +183,82 @@ class ImagePainter extends CustomPainter { return oldDelegate != this; } } + +void enterPasswordDialog(String id, BuildContext context) { + final controller = TextEditingController(); + var remember = FFI.getByName('remember', arg: id) == 'true'; + showAlertDialog( + context, + (setState) => Tuple3( + Text('Please enter your password'), + Column(mainAxisSize: MainAxisSize.min, children: [ + TextField( + autofocus: true, + obscureText: true, + controller: controller, + decoration: const InputDecoration( + labelText: 'Password', + ), + ), + ListTile( + title: Text( + 'Remember the password', + ), + leading: Checkbox( + value: remember, + onChanged: (v) { + setState(() { + remember = v; + }); + }, + ), + ), + ]), + [ + TextField( + autofocus: true, + obscureText: true, + controller: controller, + decoration: const InputDecoration( + labelText: 'Password', + ), + ), + ListTile( + title: Text( + 'Remember the password', + ), + leading: Checkbox( + value: remember, + onChanged: (v) { + setState(() { + remember = v; + }); + }, + ), + ), + ])); +} + +void wrongPasswordDialog(String id, BuildContext context) { + showAlertDialog( + context, + (_) => + Tuple3(Text('Wrong Password'), Text('Do you want to enter again?'), [ + FlatButton( + textColor: MyTheme.accent, + onPressed: () { + Navigator.pop(context); + Navigator.pop(context); + }, + child: Text('Cancel'), + ), + FlatButton( + textColor: MyTheme.accent, + onPressed: () { + Navigator.pop(context); + enterPasswordDialog(id, context); + }, + child: Text('Retry'), + ), + ])); +} diff --git a/flutter_hbb/pubspec.lock b/flutter_hbb/pubspec.lock index e4652d505..e4f1ca350 100644 --- a/flutter_hbb/pubspec.lock +++ b/flutter_hbb/pubspec.lock @@ -193,6 +193,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.3.2+2" + quiver: + dependency: transitive + description: + name: quiver + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.5" sky_engine: dependency: transitive description: flutter @@ -240,6 +247,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.2.19-nullsafety.2" + tuple: + dependency: "direct main" + description: + name: tuple + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" typed_data: dependency: transitive description: diff --git a/flutter_hbb/pubspec.yaml b/flutter_hbb/pubspec.yaml index c75c075f3..9aa35bd58 100644 --- a/flutter_hbb/pubspec.yaml +++ b/flutter_hbb/pubspec.yaml @@ -32,6 +32,7 @@ dependencies: path_provider: ^1.6.24 provider: ^4.3.2+2 flutter_easyloading: ^2.1.3 + tuple: ^1.0.1 dev_dependencies: flutter_test: