fluffychat/lib/pages/homeserver_picker/homeserver_picker_view.dart

277 lines
10 KiB
Dart
Raw Normal View History

2021-10-16 10:28:50 +02:00
import 'package:flutter/cupertino.dart';
2021-10-26 18:50:34 +02:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';
import 'package:url_launcher/url_launcher.dart';
2021-06-11 10:08:04 +02:00
import 'package:vrouter/vrouter.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/platform_infos.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/default_app_bar_search_field.dart';
import 'package:fluffychat/widgets/layouts/one_page_card.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-06-11 10:08:04 +02:00
import '../../utils/localized_exception_extension.dart';
2021-11-09 21:32:16 +01:00
import 'homeserver_picker.dart';
2021-04-09 16:28:26 +02:00
2021-05-22 09:13:47 +02:00
class HomeserverPickerView extends StatelessWidget {
2021-04-09 16:28:26 +02:00
final HomeserverPickerController controller;
2021-05-22 09:13:47 +02:00
const HomeserverPickerView(this.controller, {Key key}) : super(key: key);
2021-04-09 16:28:26 +02:00
@override
Widget build(BuildContext context) {
return OnePageCard(
child: Scaffold(
appBar: AppBar(
titleSpacing: 8,
title: DefaultAppBarSearchField(
prefixText: 'https://',
hintText: L10n.of(context).enterYourHomeserver,
searchController: controller.homeserverController,
2021-10-14 18:09:30 +02:00
suffix: const Icon(Icons.edit_outlined),
2021-04-09 16:28:26 +02:00
padding: EdgeInsets.zero,
2021-06-11 10:08:04 +02:00
onChanged: controller.setDomain,
2021-04-09 16:28:26 +02:00
readOnly: !AppConfig.allowOtherHomeservers,
2021-05-01 07:40:12 +02:00
onSubmit: (_) => controller.checkHomeserverAction(),
unfocusOnClear: false,
autocorrect: false,
labelText: L10n.of(context).homeserver,
2021-04-09 16:28:26 +02:00
),
elevation: 0,
),
2021-06-11 10:08:04 +02:00
body: ListView(children: [
2021-11-15 09:48:21 +01:00
Image.asset(
Theme.of(context).brightness == Brightness.dark
? 'assets/banner_dark.png'
: 'assets/banner.png',
2021-06-11 10:08:04 +02:00
),
controller.isLoading
2021-10-14 18:09:30 +02:00
? const Center(
2021-10-10 13:38:06 +02:00
child: CircularProgressIndicator.adaptive(strokeWidth: 2))
2021-06-11 10:08:04 +02:00
: controller.error != null
? Center(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
controller.error,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
color: Colors.red[900],
),
),
),
)
: FutureBuilder(
future: controller.getLoginTypes(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text(
snapshot.error.toLocalizedString(context),
textAlign: TextAlign.center,
),
);
}
if (!snapshot.hasData) {
2021-10-14 18:09:30 +02:00
return const Center(
2021-10-10 13:38:06 +02:00
child: CircularProgressIndicator.adaptive(
strokeWidth: 2));
2021-06-11 10:08:04 +02:00
}
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 12.0,
vertical: 4.0,
),
2021-06-11 10:08:04 +02:00
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
2021-11-15 09:48:21 +01:00
if (controller.ssoLoginSupported)
Row(children: [
const Expanded(child: Divider()),
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
L10n.of(context).loginWithOneClick),
2021-06-11 10:08:04 +02:00
),
2021-11-15 09:48:21 +01:00
const Expanded(child: Divider()),
]),
Wrap(
children: [
if (controller.ssoLoginSupported) ...{
for (final identityProvider
in controller.identityProviders)
_SsoButton(
onPressed: () =>
controller.ssoLoginAction(
identityProvider.id),
identityProvider: identityProvider,
),
},
].toList(),
),
if (controller.ssoLoginSupported &&
(controller.registrationSupported ||
controller.passwordLoginSupported))
Row(children: [
const Expanded(child: Divider()),
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(L10n.of(context).or),
),
const Expanded(child: Divider()),
]),
if (controller.passwordLoginSupported) ...[
2021-10-16 10:28:50 +02:00
Center(
child: _LoginButton(
onPressed: () =>
VRouter.of(context).to('login'),
icon: Icon(
CupertinoIcons.lock_open_fill,
color: Theme.of(context)
.textTheme
.bodyText1
.color,
2021-06-11 10:08:04 +02:00
),
2021-10-16 10:28:50 +02:00
labelText: L10n.of(context).login,
),
),
2021-11-15 09:48:21 +01:00
const SizedBox(height: 12),
],
2021-11-04 17:10:16 +01:00
if (controller.registrationSupported)
2021-10-16 10:28:50 +02:00
Center(
child: _LoginButton(
onPressed: controller.signUpAction,
icon: Icon(
CupertinoIcons.person_add,
color: Theme.of(context)
.textTheme
.bodyText1
.color,
2021-06-11 10:08:04 +02:00
),
2021-10-16 10:28:50 +02:00
labelText: L10n.of(context).register,
),
),
2021-11-15 09:48:21 +01:00
],
2021-06-11 10:08:04 +02:00
),
);
}),
2021-11-15 09:48:21 +01:00
]),
bottomNavigationBar: Material(
elevation: 6,
color: Theme.of(context).scaffoldBackgroundColor,
child: Wrap(
2021-06-11 10:08:04 +02:00
alignment: WrapAlignment.center,
2021-04-09 16:28:26 +02:00
children: [
2021-06-11 10:08:04 +02:00
TextButton(
onPressed: () => launch(AppConfig.privacyUrl),
child: Text(
L10n.of(context).privacy,
2021-10-14 18:09:30 +02:00
style: const TextStyle(
2021-06-11 10:08:04 +02:00
decoration: TextDecoration.underline,
color: Colors.blueGrey,
),
),
2021-04-09 16:28:26 +02:00
),
2021-06-11 10:08:04 +02:00
TextButton(
onPressed: () => PlatformInfos.showDialog(context),
2021-04-09 16:28:26 +02:00
child: Text(
2021-06-11 10:08:04 +02:00
L10n.of(context).about,
2021-10-14 18:09:30 +02:00
style: const TextStyle(
2021-06-11 10:08:04 +02:00
decoration: TextDecoration.underline,
color: Colors.blueGrey,
2021-04-09 16:28:26 +02:00
),
),
),
],
),
2021-11-15 09:48:21 +01:00
),
),
);
}
}
class _SsoButton extends StatelessWidget {
final IdentityProvider identityProvider;
final void Function() onPressed;
const _SsoButton({
Key key,
@required this.identityProvider,
this.onPressed,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onPressed,
borderRadius: BorderRadius.circular(7),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
identityProvider.icon == null
? const Icon(Icons.web_outlined)
: CachedNetworkImage(
imageUrl: Uri.parse(identityProvider.icon)
.getDownloadLink(Matrix.of(context).getLoginClient())
.toString(),
width: 32,
height: 32,
),
const SizedBox(height: 8),
Text(
identityProvider.name ??
identityProvider.brand ??
L10n.of(context).singlesignon,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: Theme.of(context).textTheme.subtitle2.color,
),
),
],
),
),
);
}
}
class _LoginButton extends StatelessWidget {
final String labelText;
final Widget icon;
final void Function() onPressed;
const _LoginButton({
Key key,
this.labelText,
this.icon,
this.onPressed,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return OutlinedButton.icon(
style: OutlinedButton.styleFrom(
2021-10-16 10:28:50 +02:00
minimumSize: const Size(256, 56),
textStyle: const TextStyle(fontWeight: FontWeight.bold),
backgroundColor: Theme.of(context).backgroundColor,
2021-11-15 09:48:21 +01:00
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
),
2021-10-16 10:28:50 +02:00
),
onPressed: onPressed,
icon: icon,
label: Text(
labelText,
style: TextStyle(
color: Theme.of(context).textTheme.bodyText1.color,
2021-04-09 16:28:26 +02:00
),
),
);
}
}