fluffychat/lib/views/homeserver_picker.dart

125 lines
4.3 KiB
Dart
Raw Normal View History

2020-04-12 10:35:45 +02:00
import 'dart:math';
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
import 'package:fluffychat/components/matrix.dart';
2020-09-18 12:58:22 +02:00
import 'package:fluffychat/config/app_config.dart';
2020-05-07 07:52:40 +02:00
import 'package:fluffychat/l10n/l10n.dart';
2020-04-12 10:35:45 +02:00
import 'package:fluffychat/utils/app_route.dart';
2020-09-08 10:55:32 +02:00
import 'package:fluffychat/utils/sentry_controller.dart';
2020-04-12 10:35:45 +02:00
import 'package:fluffychat/views/sign_up.dart';
import 'package:flutter/material.dart';
class HomeserverPicker extends StatelessWidget {
2020-05-13 15:58:59 +02:00
Future<void> _setHomeserverAction(BuildContext context) async {
2020-04-12 10:35:45 +02:00
final homeserver = await SimpleDialogs(context).enterText(
2020-05-07 07:52:40 +02:00
titleText: L10n.of(context).enterYourHomeserver,
2020-09-18 12:58:22 +02:00
hintText: AppConfig.defaultHomeserver,
prefixText: 'https://',
keyboardType: TextInputType.url);
2020-04-12 10:35:45 +02:00
if (homeserver?.isEmpty ?? true) return;
_checkHomeserverAction(homeserver, context);
}
2020-05-13 15:58:59 +02:00
void _checkHomeserverAction(String homeserver, BuildContext context) async {
2020-09-08 10:55:32 +02:00
if (await SentryController.getSentryStatus() == null || true) {
await SentryController.toggleSentryAction(context);
}
2020-04-12 10:35:45 +02:00
if (!homeserver.startsWith('https://')) {
homeserver = 'https://$homeserver';
}
// removes trailing spaces and slash from url if present (api errors on it)
homeserver = homeserver.trim();
if (homeserver.endsWith('/')) {
homeserver = homeserver.substring(0, homeserver.length - 1);
}
2020-04-27 13:36:39 +02:00
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-04-12 10:35:45 +02:00
Matrix.of(context).client.checkServer(homeserver));
if (success != false) {
await Navigator.of(context).push(AppRoute(SignUp()));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(
horizontal:
max((MediaQuery.of(context).size.width - 600) / 2, 0)),
child: Column(
children: <Widget>[
Hero(
tag: 'loginBanner',
2020-05-13 15:58:59 +02:00
child: Image.asset('assets/fluffychat-banner.png'),
2020-04-12 10:35:45 +02:00
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
2020-05-07 07:52:40 +02:00
L10n.of(context).welcomeText,
2020-04-12 10:35:45 +02:00
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 22,
),
),
),
Spacer(),
Hero(
tag: 'loginButton',
child: Container(
width: double.infinity,
height: 50,
padding: EdgeInsets.symmetric(horizontal: 12),
child: RaisedButton(
elevation: 7,
color: Theme.of(context).primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
child: Text(
2020-05-07 07:52:40 +02:00
L10n.of(context).connect.toUpperCase(),
2020-04-12 10:35:45 +02:00
style: TextStyle(color: Colors.white, fontSize: 16),
),
onPressed: () => _checkHomeserverAction(
2020-09-18 12:58:22 +02:00
AppConfig.defaultHomeserver, context),
2020-04-12 10:35:45 +02:00
),
),
),
Padding(
padding:
const EdgeInsets.only(left: 16.0, right: 16.0, top: 16.0),
child: Opacity(
opacity: 0.75,
child: Text(
2020-05-07 07:52:40 +02:00
L10n.of(context).byDefaultYouWillBeConnectedTo(
2020-09-18 12:58:22 +02:00
AppConfig.defaultHomeserver),
2020-04-12 10:35:45 +02:00
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
),
),
),
),
FlatButton(
child: Text(
2020-05-07 07:52:40 +02:00
L10n.of(context).changeTheHomeserver,
2020-04-12 10:35:45 +02:00
style: TextStyle(
decoration: TextDecoration.underline,
color: Colors.blue,
fontSize: 16,
),
),
onPressed: () => _setHomeserverAction(context),
),
SizedBox(height: 16),
],
),
),
),
);
}
}