fluffychat/lib/components/dialogs/new_group_dialog.dart

95 lines
2.8 KiB
Dart
Raw Normal View History

2020-01-20 09:50:49 +01:00
import 'package:fluffychat/i18n/i18n.dart';
2020-01-01 19:10:13 +01:00
import 'package:fluffychat/views/chat.dart';
2020-01-19 16:56:13 +01:00
import 'package:fluffychat/views/invitation_selection.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
2020-01-19 16:56:13 +01:00
import 'package:pedantic/pedantic.dart';
2020-01-01 19:10:13 +01:00
import '../matrix.dart';
2020-01-19 16:56:13 +01:00
class NewGroupDialog extends StatefulWidget {
@override
_NewGroupDialogState createState() => _NewGroupDialogState();
}
class _NewGroupDialogState extends State<NewGroupDialog> {
TextEditingController controller = TextEditingController();
bool publicGroup = false;
2020-01-01 19:10:13 +01:00
void submitAction(BuildContext context) async {
final MatrixState matrix = Matrix.of(context);
Map<String, dynamic> params = {};
2020-01-19 16:56:13 +01:00
if (publicGroup) {
params["preset"] = "public_chat";
params["visibility"] = "public";
if (controller.text.isNotEmpty) {
params["room_alias_name"] = controller.text;
}
} else {
params["preset"] = "private_chat";
}
2020-01-01 19:10:13 +01:00
if (controller.text.isNotEmpty) params["name"] = controller.text;
final String roomID = await matrix.tryRequestWithLoadingDialog(
matrix.client.createRoom(params: params),
);
Navigator.of(context).pop();
2020-01-02 22:31:39 +01:00
if (roomID != null) {
2020-01-19 16:56:13 +01:00
unawaited(
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return Chat(roomID);
}),
),
);
2020-01-02 22:31:39 +01:00
await Navigator.push(
2020-01-01 19:10:13 +01:00
context,
2020-01-19 16:56:13 +01:00
MaterialPageRoute(
builder: (context) => InvitationSelection(
matrix.client.getRoomById(roomID),
),
),
2020-01-01 19:10:13 +01:00
);
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
}
@override
Widget build(BuildContext context) {
return AlertDialog(
2020-01-20 09:50:49 +01:00
title: Text(I18n.of(context).createNewGroup),
2020-01-01 19:10:13 +01:00
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
controller: controller,
autocorrect: false,
textInputAction: TextInputAction.go,
onSubmitted: (s) => submitAction(context),
decoration: InputDecoration(
2020-01-20 09:50:49 +01:00
labelText: I18n.of(context).optionalGroupName,
2020-01-01 19:10:13 +01:00
icon: Icon(Icons.people),
2020-01-20 09:50:49 +01:00
hintText: I18n.of(context).enterAGroupName),
2020-01-01 19:10:13 +01:00
),
2020-01-19 16:56:13 +01:00
SwitchListTile(
2020-01-20 09:50:49 +01:00
title: Text(I18n.of(context).groupIsPublic),
2020-01-19 16:56:13 +01:00
value: publicGroup,
onChanged: (bool b) => setState(() => publicGroup = b),
),
2020-01-01 19:10:13 +01:00
],
),
actions: <Widget>[
FlatButton(
2020-01-20 09:50:49 +01:00
child: Text(I18n.of(context).close.toUpperCase(),
2020-01-01 19:10:13 +01:00
style: TextStyle(color: Colors.blueGrey)),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
2020-01-20 09:50:49 +01:00
child: Text(I18n.of(context).create.toUpperCase()),
2020-01-01 19:10:13 +01:00
onPressed: () => submitAction(context),
),
],
);
}
}