[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 { class NewPrivateChatDialog extends StatelessWidget {
final TextEditingController controller = TextEditingController(); final TextEditingController controller = TextEditingController();
final _formKey = GlobalKey<FormState>();
void submitAction(BuildContext context) async { void submitAction(BuildContext context) async {
if (controller.text.isEmpty) return; if (controller.text.isEmpty) return;
if (!_formKey.currentState.validate()) return;
final MatrixState matrix = Matrix.of(context); final MatrixState matrix = Matrix.of(context);
if ("@" + controller.text.trim() == matrix.client.userID) return;
final User user = User( final User user = User(
"@" + controller.text, "@" + controller.text.trim(),
room: Room(id: "", client: matrix.client), room: Room(id: "", client: matrix.client),
); );
final String roomID = final String roomID =
@ -33,17 +38,38 @@ class NewPrivateChatDialog extends StatelessWidget {
content: Column( content: Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
TextField( Form(
controller: controller, key: _formKey,
autofocus: true, child: TextFormField(
autocorrect: false, controller: controller,
textInputAction: TextInputAction.go, autofocus: true,
onSubmitted: (s) => submitAction(context), autocorrect: false,
decoration: InputDecoration( textInputAction: TextInputAction.go,
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", labelText: "Enter a username",
icon: Icon(Icons.account_circle), icon: Icon(Icons.account_circle),
prefixText: "@", prefixText: "@",
hintText: "username:homeserver"), hintText: "username:homeserver",
),
),
), ),
SizedBox(height: 16), SizedBox(height: 16),
Text( Text(