fluffychat/lib/pages/sign_up.dart

72 lines
2.2 KiB
Dart
Raw Normal View History

2021-04-12 17:31:53 +02:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:file_picker_cross/file_picker_cross.dart';
import 'package:fluffychat/pages/views/sign_up_view.dart';
2021-04-12 17:31:53 +02:00
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-04-12 17:31:53 +02:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
class SignUp extends StatefulWidget {
@override
SignUpController createState() => SignUpController();
}
class SignUpController extends State<SignUp> {
final TextEditingController usernameController = TextEditingController();
String usernameError;
bool loading = false;
MatrixFile avatar;
void setAvatarAction() async {
2021-04-14 10:37:15 +02:00
final file =
2021-04-12 17:31:53 +02:00
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
if (file != null) {
setState(
() => avatar = MatrixFile(
bytes: file.toUint8List(),
name: file.fileName,
),
);
}
}
void resetAvatarAction() => setState(() => avatar = null);
void signUpAction([_]) async {
2021-04-14 10:37:15 +02:00
final matrix = Matrix.of(context);
2021-04-12 17:31:53 +02:00
if (usernameController.text.isEmpty) {
setState(() => usernameError = L10n.of(context).pleaseChooseAUsername);
} else {
setState(() => usernameError = null);
}
if (usernameController.text.isEmpty) {
return;
}
setState(() => loading = true);
final preferredUsername =
usernameController.text.toLowerCase().trim().replaceAll(' ', '-');
try {
2021-05-20 13:59:55 +02:00
await matrix.client.checkUsernameAvailability(preferredUsername);
2021-04-12 17:31:53 +02:00
} on MatrixException catch (exception) {
setState(() => usernameError = exception.errorMessage);
return setState(() => loading = false);
} catch (exception) {
setState(() => usernameError = exception.toString());
return setState(() => loading = false);
}
setState(() => loading = false);
await AdaptivePageLayout.of(context).pushNamed(
'/signup/password/${Uri.encodeComponent(preferredUsername)}/${Uri.encodeComponent(usernameController.text)}',
arguments: avatar,
);
}
@override
Widget build(BuildContext context) => SignUpUI(this);
2021-04-12 17:31:53 +02:00
}