[New private chat dialog] Validate input before continuing

Took 19 minutes
This commit is contained in:
Marcel 2020-01-03 20:44:31 +01:00
parent c0594fa22b
commit 58f45406e8

View file

@ -6,12 +6,17 @@ import '../matrix.dart';
class NewPrivateChatDialog extends StatelessWidget {
final TextEditingController controller = TextEditingController();
final _formKey = GlobalKey<FormState>();
void submitAction(BuildContext context) async {
if (controller.text.isEmpty) return;
if (!_formKey.currentState.validate()) return;
final MatrixState matrix = Matrix.of(context);
if ("@" + controller.text.trim() == matrix.client.userID) return;
final User user = User(
"@" + controller.text,
"@" + controller.text.trim(),
room: Room(id: "", client: matrix.client),
);
final String roomID =
@ -33,17 +38,38 @@ class NewPrivateChatDialog extends StatelessWidget {
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
Form(
key: _formKey,
child: TextFormField(
controller: controller,
autofocus: true,
autocorrect: false,
textInputAction: TextInputAction.go,
onSubmitted: (s) => submitAction(context),
onFieldSubmitted: (s) => submitAction(context),
validator: (value) {
if (value.isEmpty) {
return 'Please enter a matrix identifier';
}
final MatrixState matrix = Matrix.of(context);
String mxid = "@" + controller.text.trim();
if (mxid == matrix.client.userID) {
return "You cannot invite yourself";
}
if(!mxid.contains("@")) {
return "Make sure the identifier is valid";
}
if(!mxid.contains(":")) {
return "Make sure the identifier is valid";
}
return null;
},
decoration: InputDecoration(
labelText: "Enter a username",
icon: Icon(Icons.account_circle),
prefixText: "@",
hintText: "username:homeserver"),
hintText: "username:homeserver",
),
),
),
SizedBox(height: 16),
Text(