refactor alertDialog

This commit is contained in:
open-trade 2020-11-19 21:59:49 +08:00
parent 86add59e92
commit 0b0fb4f145
5 changed files with 137 additions and 130 deletions

View File

@ -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<Null> enterPasswordDialog(String id, BuildContext context) async {
Future<Null> showAlertDialog(
BuildContext context,
Tuple3<Widget, Widget, List<Widget>> 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<void>(
@ -109,67 +67,17 @@ Future<Null> enterPasswordDialog(String id, BuildContext context) async {
_hasDialog = false;
}
Future<Null> 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<void>(
context: context,
barrierDismissible: false,
builder: (context) => dialog);
_hasDialog = false;
}
Future<Null> 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<void>(
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'),
),
]));
}

View File

@ -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<String, dynamic> 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<String, dynamic> 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<String, dynamic> evt) {
_pi.currentDisplay = int.parse(evt['display']);
_display.x = double.parse(evt['x']);

View File

@ -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<RemotePage> {
}
void interval() {
FFI.ffiModel.update(widget.id, context);
FFI.ffiModel.update(widget.id, context, handleMsgbox);
}
void handleMsgbox(Map<String, dynamic> 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'),
),
]));
}

View File

@ -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:

View File

@ -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: