fluffychat/lib/pages/connect/connect_page_view.dart

204 lines
8.1 KiB
Dart
Raw Normal View History

2022-04-15 11:42:59 +02:00
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
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 12:37:38 +02:00
leading: controller.loading ? null : const BackButton(),
2022-04-15 11:42:59 +02:00
automaticallyImplyLeading: !controller.loading,
centerTitle: true,
title: Text(
Matrix.of(context).getLoginClient().homeserver?.host ?? '',
),
),
body: ListView(
children: [
if (Matrix.of(context).loginRegistrationSupported ?? false) ...[
Padding(
2022-08-05 21:04:37 +02:00
padding: const EdgeInsets.all(12.0),
2022-04-15 11:42:59 +02:00
child: Center(
child: Stack(
children: [
Material(
borderRadius: BorderRadius.circular(64),
2022-10-16 12:37:38 +02:00
elevation: Theme.of(context)
.appBarTheme
.scrolledUnderElevation ??
4,
color: Colors.transparent,
2022-10-16 12:37:38 +02:00
shadowColor: Theme.of(context).appBarTheme.shadowColor,
clipBehavior: Clip.hardEdge,
child: CircleAvatar(
radius: 64,
2022-10-16 12:37:38 +02:00
backgroundColor: Colors.white,
child: avatar == null
? const Icon(
2022-10-16 12:37:38 +02: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 11:42:59 +02: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 11:42:59 +02:00
),
),
),
Padding(
2022-08-05 21:04:37 +02:00
padding: const EdgeInsets.all(12.0),
2022-04-15 11:42:59 +02:00
child: TextField(
controller: controller.usernameController,
onSubmitted: (_) => controller.signUp(),
2022-07-07 18:50:13 +02:00
decoration: InputDecoration(
prefixIcon: const Icon(Icons.account_box_outlined),
2022-04-15 11:42:59 +02:00
hintText: L10n.of(context)!.chooseAUsername,
errorText: controller.signupError,
2022-08-06 13:35:59 +02:00
errorStyle: const TextStyle(color: Colors.orange),
2022-10-16 12:37:38 +02:00
fillColor: Theme.of(context).colorScheme.background,
2022-04-15 11:42:59 +02:00
),
),
),
Padding(
2022-08-05 21:04:37 +02:00
padding: const EdgeInsets.all(12.0),
2022-04-15 11:42:59 +02:00
child: Hero(
tag: 'loginButton',
child: ElevatedButton(
2022-10-16 12:37:38 +02:00
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Theme.of(context).colorScheme.onPrimary,
),
2022-07-07 18:50:13 +02:00
onPressed: controller.loading ? () {} : controller.signUp,
2022-04-15 11:42:59 +02:00
child: controller.loading
? const LinearProgressIndicator()
: Text(L10n.of(context)!.signUp),
),
),
),
2022-10-16 12:37:38 +02:00
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
Expanded(
2022-08-06 13:35:59 +02:00
child: Divider(
2022-10-16 12:37:38 +02:00
thickness: 1,
color: Theme.of(context).textTheme.subtitle1?.color,
2022-08-06 13:35:59 +02:00
),
2022-04-15 11:42:59 +02:00
),
2022-10-16 12:37:38 +02:00
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
L10n.of(context)!.or,
style: const TextStyle(fontSize: 18),
),
),
Expanded(
2022-08-06 13:35:59 +02:00
child: Divider(
2022-10-16 12:37:38 +02:00
thickness: 1,
color: Theme.of(context).textTheme.subtitle1?.color,
),
),
],
),
2022-04-15 11:42:59 +02:00
),
],
if (controller.supportsSso)
identityProviders == null
? const SizedBox(
height: 74,
2022-10-16 12:37:38 +02:00
child: Center(child: CircularProgressIndicator.adaptive()),
2022-04-15 11:42:59 +02:00
)
: Center(
child: identityProviders.length == 1
? Padding(
2022-08-05 21:04:37 +02:00
padding: const EdgeInsets.all(12.0),
2022-04-15 11:42:59 +02:00
child: ElevatedButton(
2022-10-16 12:37:38 +02:00
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context)
.colorScheme
.primaryContainer,
foregroundColor: Theme.of(context)
.colorScheme
.onPrimaryContainer,
),
2022-04-15 11:42:59 +02:00
onPressed: () => controller
.ssoLoginAction(identityProviders.single.id!),
child: Text(identityProviders.single.name ??
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 21:04:37 +02:00
padding: const EdgeInsets.all(12.0),
2022-04-15 11:42:59 +02:00
child: Hero(
tag: 'signinButton',
child: ElevatedButton(
2022-10-16 12:37:38 +02:00
style: ElevatedButton.styleFrom(
backgroundColor:
Theme.of(context).colorScheme.primaryContainer,
foregroundColor:
Theme.of(context).colorScheme.onPrimaryContainer,
),
2022-04-15 11:42:59 +02:00
onPressed: controller.loading ? () {} : controller.login,
child: Text(L10n.of(context)!.login),
),
),
),
],
),
);
}
}