fluffychat/lib/pages/connect/connect_page_view.dart

224 lines
9.1 KiB
Dart
Raw Normal View History

2022-04-15 09:42:59 +00:00
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2022-11-04 10:17:22 +00:00
import 'package:matrix/matrix.dart';
2022-04-15 09:42:59 +00:00
import 'package:fluffychat/pages/connect/connect_page.dart';
import 'package:fluffychat/widgets/layouts/login_scaffold.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'sso_button.dart';
class ConnectPageView extends StatelessWidget {
final ConnectPageController controller;
const ConnectPageView(this.controller, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final avatar = Matrix.of(context).loginAvatar;
final identityProviders = controller.identityProviders;
return LoginScaffold(
appBar: AppBar(
2022-10-16 10:37:38 +00:00
leading: controller.loading ? null : const BackButton(),
2022-04-15 09:42:59 +00:00
automaticallyImplyLeading: !controller.loading,
centerTitle: true,
title: Text(
Matrix.of(context).getLoginClient().homeserver?.host ?? '',
),
),
body: ListView(
key: const Key('ConnectPageListView'),
2022-04-15 09:42:59 +00:00
children: [
if (Matrix.of(context).loginRegistrationSupported ?? false) ...[
Padding(
2022-08-05 19:04:37 +00:00
padding: const EdgeInsets.all(12.0),
2022-04-15 09:42:59 +00:00
child: Center(
child: Stack(
children: [
Material(
borderRadius: BorderRadius.circular(64),
2022-10-16 10:37:38 +00:00
elevation: Theme.of(context)
.appBarTheme
.scrolledUnderElevation ??
2022-12-29 19:38:13 +00:00
10,
color: Colors.transparent,
2022-12-29 19:38:13 +00:00
shadowColor: Theme.of(context)
.colorScheme
.onBackground
.withAlpha(64),
clipBehavior: Clip.hardEdge,
child: CircleAvatar(
radius: 64,
2022-10-16 10:37:38 +00:00
backgroundColor: Colors.white,
child: avatar == null
? const Icon(
2022-10-16 10:37:38 +00:00
Icons.person,
color: Colors.black,
size: 64,
)
: FutureBuilder<Uint8List>(
future: avatar.readAsBytes(),
builder: (context, snapshot) {
final bytes = snapshot.data;
if (bytes == null) {
return const CircularProgressIndicator
.adaptive();
}
return Image.memory(
bytes,
fit: BoxFit.cover,
width: 128,
height: 128,
);
},
),
),
2022-04-15 09:42:59 +00:00
),
Positioned(
bottom: 0,
right: 0,
child: FloatingActionButton(
mini: true,
onPressed: controller.pickAvatar,
backgroundColor: Colors.white,
foregroundColor: Colors.black,
child: const Icon(Icons.camera_alt_outlined),
),
),
],
2022-04-15 09:42:59 +00:00
),
),
),
Padding(
2022-08-05 19:04:37 +00:00
padding: const EdgeInsets.all(12.0),
2022-04-15 09:42:59 +00:00
child: TextField(
controller: controller.usernameController,
onSubmitted: (_) => controller.signUp(),
2022-07-07 16:50:13 +00:00
decoration: InputDecoration(
prefixIcon: const Icon(Icons.account_box_outlined),
2022-04-15 09:42:59 +00:00
hintText: L10n.of(context)!.chooseAUsername,
errorText: controller.signupError,
2022-08-06 11:35:59 +00:00
errorStyle: const TextStyle(color: Colors.orange),
2022-04-15 09:42:59 +00:00
),
),
),
Padding(
2022-08-05 19:04:37 +00:00
padding: const EdgeInsets.all(12.0),
2022-04-15 09:42:59 +00:00
child: Hero(
tag: 'loginButton',
2022-12-29 19:38:13 +00:00
child: ElevatedButton.icon(
2022-10-16 10:37:38 +00:00
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Theme.of(context).colorScheme.onPrimary,
),
2022-07-07 16:50:13 +00:00
onPressed: controller.loading ? () {} : controller.signUp,
2022-12-29 19:38:13 +00:00
icon: const Icon(Icons.person_add_outlined),
label: controller.loading
2022-04-15 09:42:59 +00:00
? const LinearProgressIndicator()
: Text(L10n.of(context)!.signUp),
),
),
),
2022-10-16 10:37:38 +00:00
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
Expanded(
2022-08-06 11:35:59 +00:00
child: Divider(
2022-10-16 10:37:38 +00:00
thickness: 1,
2022-11-04 12:31:29 +00:00
color: Theme.of(context).dividerColor,
2022-08-06 11:35:59 +00:00
),
2022-04-15 09:42:59 +00:00
),
2022-10-16 10:37:38 +00:00
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
L10n.of(context)!.or,
style: const TextStyle(fontSize: 18),
),
),
Expanded(
2022-08-06 11:35:59 +00:00
child: Divider(
2022-10-16 10:37:38 +00:00
thickness: 1,
2022-11-04 12:31:29 +00:00
color: Theme.of(context).dividerColor,
2022-10-16 10:37:38 +00:00
),
),
],
),
2022-04-15 09:42:59 +00:00
),
],
if (controller.supportsSso)
identityProviders == null
? const SizedBox(
height: 74,
2022-10-16 10:37:38 +00:00
child: Center(child: CircularProgressIndicator.adaptive()),
2022-04-15 09:42:59 +00:00
)
: Center(
child: identityProviders.length == 1
2022-11-04 10:17:22 +00:00
? Container(
width: double.infinity,
2022-08-05 19:04:37 +00:00
padding: const EdgeInsets.all(12.0),
2022-11-04 10:17:22 +00:00
child: ElevatedButton.icon(
2022-10-16 10:37:38 +00:00
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context)
.colorScheme
.primaryContainer,
foregroundColor: Theme.of(context)
.colorScheme
.onPrimaryContainer,
),
2022-11-04 10:17:22 +00:00
icon: identityProviders.single.icon == null
? const Icon(
Icons.web_outlined,
size: 16,
)
: Image.network(
Uri.parse(identityProviders.single.icon!)
.getDownloadLink(Matrix.of(context)
.getLoginClient())
.toString(),
width: 32,
height: 32,
),
2022-04-15 09:42:59 +00:00
onPressed: () => controller
.ssoLoginAction(identityProviders.single.id!),
2022-11-04 10:17:22 +00:00
label: Text(identityProviders.single.name ??
2022-04-15 09:42:59 +00:00
identityProviders.single.brand ??
L10n.of(context)!.loginWithOneClick),
),
)
: Wrap(
children: [
for (final identityProvider in identityProviders)
SsoButton(
onPressed: () => controller
.ssoLoginAction(identityProvider.id!),
identityProvider: identityProvider,
),
].toList(),
),
),
if (controller.supportsLogin)
Padding(
2022-08-05 19:04:37 +00:00
padding: const EdgeInsets.all(12.0),
2022-04-15 09:42:59 +00:00
child: Hero(
tag: 'signinButton',
2022-12-29 19:38:13 +00:00
child: ElevatedButton.icon(
icon: const Icon(Icons.login_outlined),
2022-10-16 10:37:38 +00:00
style: ElevatedButton.styleFrom(
backgroundColor:
Theme.of(context).colorScheme.primaryContainer,
foregroundColor:
Theme.of(context).colorScheme.onPrimaryContainer,
),
2022-04-15 09:42:59 +00:00
onPressed: controller.loading ? () {} : controller.login,
2022-12-29 19:38:13 +00:00
label: Text(L10n.of(context)!.login),
2022-04-15 09:42:59 +00:00
),
),
),
],
),
);
}
}