refactor alertDialog
This commit is contained in:
parent
86add59e92
commit
0b0fb4f145
@ -1,7 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||||
import 'model.dart';
|
import 'package:tuple/tuple.dart';
|
||||||
|
|
||||||
class HexColor extends Color {
|
class HexColor extends Color {
|
||||||
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
|
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
|
||||||
@ -41,65 +41,23 @@ void showSuccess(String text) {
|
|||||||
bool _hasDialog = false;
|
bool _hasDialog = false;
|
||||||
|
|
||||||
// https://material.io/develop/flutter/components/dialogs
|
// 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();
|
dismissLoading();
|
||||||
if (_hasDialog) {
|
if (_hasDialog) {
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
}
|
}
|
||||||
_hasDialog = true;
|
_hasDialog = true;
|
||||||
final controller = TextEditingController();
|
|
||||||
var remember = FFI.getByName('remember', arg: id) == 'true';
|
|
||||||
var dialog = StatefulBuilder(builder: (context, setState) {
|
var dialog = StatefulBuilder(builder: (context, setState) {
|
||||||
|
var widgets = build(setState);
|
||||||
return AlertDialog(
|
return AlertDialog(
|
||||||
title: Text('Please enter your password'),
|
title: widgets.item1,
|
||||||
contentPadding: const EdgeInsets.all(20.0),
|
contentPadding: const EdgeInsets.all(20.0),
|
||||||
content: Column(
|
content: widgets.item2,
|
||||||
mainAxisSize: MainAxisSize.min,
|
actions: widgets.item3,
|
||||||
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'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
await showDialog<void>(
|
await showDialog<void>(
|
||||||
@ -109,54 +67,10 @@ Future<Null> enterPasswordDialog(String id, BuildContext context) async {
|
|||||||
_hasDialog = false;
|
_hasDialog = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Null> wrongPasswordDialog(String id, BuildContext context) async {
|
void msgbox(String type, String title, String text, BuildContext context) {
|
||||||
dismissLoading();
|
showAlertDialog(
|
||||||
if (_hasDialog) {
|
context,
|
||||||
Navigator.pop(context);
|
(_) => Tuple3(Text(title), Text(text), [
|
||||||
}
|
|
||||||
_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(
|
FlatButton(
|
||||||
textColor: MyTheme.accent,
|
textColor: MyTheme.accent,
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
@ -165,11 +79,5 @@ Future<Null> msgbox(
|
|||||||
},
|
},
|
||||||
child: Text('OK'),
|
child: Text('OK'),
|
||||||
),
|
),
|
||||||
],
|
]));
|
||||||
);
|
|
||||||
await showDialog<void>(
|
|
||||||
context: context,
|
|
||||||
barrierDismissible: false,
|
|
||||||
builder: (context) => dialog);
|
|
||||||
_hasDialog = false;
|
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,11 @@ class FfiModel with ChangeNotifier {
|
|||||||
_decoding = false;
|
_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 (;;) {
|
for (;;) {
|
||||||
var evt = FFI.popEvent();
|
var evt = FFI.popEvent();
|
||||||
if (evt == null) break;
|
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) {
|
void handleSwitchDisplay(Map<String, dynamic> evt) {
|
||||||
_pi.currentDisplay = int.parse(evt['display']);
|
_pi.currentDisplay = int.parse(evt['display']);
|
||||||
_display.x = double.parse(evt['x']);
|
_display.x = double.parse(evt['x']);
|
||||||
|
@ -7,6 +7,7 @@ import 'dart:async';
|
|||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
import 'common.dart';
|
import 'common.dart';
|
||||||
import 'model.dart';
|
import 'model.dart';
|
||||||
|
import 'package:tuple/tuple.dart';
|
||||||
|
|
||||||
class RemotePage extends StatefulWidget {
|
class RemotePage extends StatefulWidget {
|
||||||
RemotePage({Key key, this.id}) : super(key: key);
|
RemotePage({Key key, this.id}) : super(key: key);
|
||||||
@ -45,7 +46,20 @@ class _RemotePageState extends State<RemotePage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void interval() {
|
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
|
@override
|
||||||
@ -169,3 +183,82 @@ class ImagePainter extends CustomPainter {
|
|||||||
return oldDelegate != this;
|
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'),
|
||||||
|
),
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
@ -193,6 +193,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.3.2+2"
|
version: "4.3.2+2"
|
||||||
|
quiver:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: quiver
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.5"
|
||||||
sky_engine:
|
sky_engine:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description: flutter
|
description: flutter
|
||||||
@ -240,6 +247,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.19-nullsafety.2"
|
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:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -32,6 +32,7 @@ dependencies:
|
|||||||
path_provider: ^1.6.24
|
path_provider: ^1.6.24
|
||||||
provider: ^4.3.2+2
|
provider: ^4.3.2+2
|
||||||
flutter_easyloading: ^2.1.3
|
flutter_easyloading: ^2.1.3
|
||||||
|
tuple: ^1.0.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user