FurryChat/lib/components/dialogs/simple_dialogs.dart
Katerina 7de6b8c700 Cosmetic improvements of homeserver and user inputs
Keyboard type for homeserver TextInput changed to url and trailing '/' with potential whitespaces is stripped.
Keyboard type for username changed to e-mail for convenience.

Should resolve: #34 and #64
2020-07-02 09:10:03 +00:00

183 lines
5.3 KiB
Dart

import 'package:fluffychat/l10n/l10n.dart';
import 'package:flutter/material.dart';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:bot_toast/bot_toast.dart';
class SimpleDialogs {
final BuildContext context;
const SimpleDialogs(this.context);
Future<String> enterText(
{String titleText,
String confirmText,
String cancelText,
String hintText,
String labelText,
String prefixText,
String suffixText,
bool password = false,
bool multiLine = false,
TextInputType keyboardType}) async {
var textEditingController = TextEditingController();
final controller = textEditingController;
String input;
await showDialog(
context: context,
builder: (c) => AlertDialog(
title: Text(titleText ?? 'Please enter a text'),
content: TextField(
controller: controller,
autofocus: true,
autocorrect: false,
onSubmitted: (s) {
input = s;
Navigator.of(context).pop();
},
minLines: multiLine ? 3 : 1,
maxLines: multiLine ? 3 : 1,
obscureText: password,
textInputAction: multiLine ? TextInputAction.newline : null,
keyboardType: keyboardType,
decoration: InputDecoration(
hintText: hintText,
labelText: labelText,
prefixText: prefixText,
suffixText: suffixText,
prefixStyle: TextStyle(color: Theme.of(context).primaryColor),
suffixStyle: TextStyle(color: Theme.of(context).primaryColor),
border: OutlineInputBorder(),
),
),
actions: <Widget>[
FlatButton(
child: Text(
cancelText?.toUpperCase() ??
L10n.of(context).close.toUpperCase(),
style: TextStyle(color: Colors.blueGrey)),
onPressed: () => Navigator.of(context).pop(),
),
FlatButton(
child: Text(
confirmText?.toUpperCase() ??
L10n.of(context).confirm.toUpperCase(),
),
onPressed: () {
input = controller.text;
Navigator.of(context).pop();
},
),
],
),
);
return input;
}
Future<bool> askConfirmation({
String titleText,
String contentText,
String confirmText,
String cancelText,
}) async {
var confirmed = false;
await showDialog(
context: context,
builder: (c) => AlertDialog(
title: Text(titleText ?? L10n.of(context).areYouSure),
content: contentText != null ? Text(contentText) : null,
actions: <Widget>[
FlatButton(
child: Text(
cancelText?.toUpperCase() ??
L10n.of(context).close.toUpperCase(),
style: TextStyle(color: Colors.blueGrey)),
onPressed: () => Navigator.of(context).pop(),
),
FlatButton(
child: Text(
confirmText?.toUpperCase() ??
L10n.of(context).confirm.toUpperCase(),
),
onPressed: () {
confirmed = true;
Navigator.of(context).pop();
},
),
],
),
);
return confirmed;
}
Future<void> inform({
String titleText,
String contentText,
String okText,
}) async {
await showDialog(
context: context,
builder: (c) => AlertDialog(
title: titleText != null ? Text(titleText) : null,
content: contentText != null ? Text(contentText) : null,
actions: <Widget>[
FlatButton(
child: Text(
okText ?? L10n.of(context).ok.toUpperCase(),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
);
}
Future<dynamic> tryRequestWithLoadingDialog(Future<dynamic> request,
{Function(MatrixException) onAdditionalAuth}) async {
showLoadingDialog(context);
final dynamic = await tryRequestWithErrorToast(request,
onAdditionalAuth: onAdditionalAuth);
Navigator.of(context)?.pop();
return dynamic;
}
Future<dynamic> tryRequestWithErrorToast(Future<dynamic> request,
{Function(MatrixException) onAdditionalAuth}) async {
try {
return await request;
} on MatrixException catch (exception) {
if (exception.requireAdditionalAuthentication &&
onAdditionalAuth != null) {
return await tryRequestWithErrorToast(onAdditionalAuth(exception));
} else {
BotToast.showText(text: exception.errorMessage);
}
} catch (exception) {
BotToast.showText(text: exception.toString());
return false;
}
}
void showLoadingDialog(BuildContext context) async {
await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) => AlertDialog(
content: Row(
children: <Widget>[
CircularProgressIndicator(),
SizedBox(width: 16),
Expanded(
child: Text(
L10n.of(context).loadingPleaseWait,
overflow: TextOverflow.ellipsis,
maxLines: 1,
)),
],
),
),
);
}
}