fluffychat/lib/components/dialogs/new_private_chat_dialog.dart

100 lines
3.0 KiB
Dart
Raw Normal View History

2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/views/chat.dart';
import 'package:flutter/material.dart';
import '../matrix.dart';
class NewPrivateChatDialog extends StatelessWidget {
final TextEditingController controller = TextEditingController();
final _formKey = GlobalKey<FormState>();
2020-01-01 19:10:13 +01:00
void submitAction(BuildContext context) async {
if (controller.text.isEmpty) return;
if (!_formKey.currentState.validate()) return;
2020-01-01 19:10:13 +01:00
final MatrixState matrix = Matrix.of(context);
if ("@" + controller.text.trim() == matrix.client.userID) return;
2020-01-01 19:10:13 +01:00
final User user = User(
"@" + controller.text.trim(),
2020-01-01 19:10:13 +01:00
room: Room(id: "", client: matrix.client),
);
final String roomID =
await matrix.tryRequestWithLoadingDialog(user.startDirectChat());
Navigator.of(context).pop();
2020-01-02 22:31:39 +01:00
if (roomID != null) {
await Navigator.push(
2020-01-01 19:10:13 +01:00
context,
MaterialPageRoute(builder: (context) => Chat(roomID)),
);
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text("New private chat"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Form(
key: _formKey,
child: TextFormField(
controller: controller,
autofocus: true,
autocorrect: false,
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(
2020-01-01 19:10:13 +01:00
labelText: "Enter a username",
icon: Icon(Icons.account_circle),
prefixText: "@",
hintText: "username:homeserver",
),
),
2020-01-01 19:10:13 +01:00
),
SizedBox(height: 16),
Text(
"Your username is ${Matrix.of(context).client.userID}",
style: TextStyle(
color: Colors.blueGrey,
fontSize: 12,
),
),
],
),
actions: <Widget>[
FlatButton(
child: Text("Close".toUpperCase(),
style: TextStyle(color: Colors.blueGrey)),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text("Continue".toUpperCase()),
onPressed: () => submitAction(context),
),
],
);
}
}