fluffychat/lib/pages/homeserver_picker/homeserver_picker_view.dart

259 lines
8.8 KiB
Dart
Raw Normal View History

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