2022-11-01 09:19:20 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022 Iconica
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2023-10-16 15:46:23 +02:00
|
|
|
import 'dart:async';
|
2022-09-20 15:51:22 +02:00
|
|
|
import 'dart:collection';
|
|
|
|
import 'package:flutter/material.dart';
|
2023-03-09 09:12:05 +01:00
|
|
|
import 'package:flutter_registration/flutter_registration.dart';
|
2022-09-20 15:51:22 +02:00
|
|
|
|
|
|
|
class AuthScreen extends StatefulWidget {
|
|
|
|
const AuthScreen({
|
2024-02-06 09:51:25 +01:00
|
|
|
required this.appBarTitle,
|
2022-09-20 15:51:22 +02:00
|
|
|
required this.steps,
|
|
|
|
required this.submitBtnTitle,
|
2022-09-26 10:35:53 +02:00
|
|
|
required this.nextBtnTitle,
|
|
|
|
required this.previousBtnTitle,
|
2022-09-20 15:51:22 +02:00
|
|
|
required this.onFinish,
|
2022-09-28 09:23:41 +02:00
|
|
|
this.customAppBar,
|
2023-03-31 11:15:48 +02:00
|
|
|
this.customBackgroundColor,
|
2023-02-16 15:02:03 +01:00
|
|
|
this.nextButtonBuilder,
|
|
|
|
this.previousButtonBuilder,
|
2024-02-02 22:18:03 +01:00
|
|
|
this.titleWidget,
|
|
|
|
this.loginButton,
|
2022-09-20 15:51:22 +02:00
|
|
|
super.key,
|
|
|
|
}) : assert(steps.length > 0, 'At least one step is required');
|
|
|
|
|
2024-02-06 09:51:25 +01:00
|
|
|
final String appBarTitle;
|
2023-10-16 15:46:23 +02:00
|
|
|
final Future<void> Function({
|
2024-02-05 13:00:21 +01:00
|
|
|
required HashMap<String, dynamic> values,
|
2023-10-03 14:38:52 +02:00
|
|
|
required void Function(int? pageToReturn) onError,
|
2022-09-27 12:00:03 +02:00
|
|
|
}) onFinish;
|
2022-09-20 15:51:22 +02:00
|
|
|
final List<AuthStep> steps;
|
|
|
|
final String submitBtnTitle;
|
2022-09-26 10:35:53 +02:00
|
|
|
final String nextBtnTitle;
|
|
|
|
final String previousBtnTitle;
|
2022-09-28 09:23:41 +02:00
|
|
|
final AppBar? customAppBar;
|
2023-03-31 11:15:48 +02:00
|
|
|
final Color? customBackgroundColor;
|
2023-10-16 15:46:23 +02:00
|
|
|
final Widget Function(Future<void> Function(), String)? nextButtonBuilder;
|
2024-02-01 14:40:25 +01:00
|
|
|
final Widget? Function(VoidCallback, String)? previousButtonBuilder;
|
2024-02-02 22:18:03 +01:00
|
|
|
final Widget? titleWidget;
|
|
|
|
final Widget? loginButton;
|
2022-09-20 15:51:22 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<AuthScreen> createState() => _AuthScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AuthScreenState extends State<AuthScreen> {
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
2022-09-26 09:38:57 +02:00
|
|
|
final _pageController = PageController();
|
|
|
|
final _animationDuration = const Duration(milliseconds: 300);
|
|
|
|
final _animationCurve = Curves.ease;
|
2022-09-20 15:51:22 +02:00
|
|
|
|
2022-09-28 09:23:41 +02:00
|
|
|
AppBar get _appBar =>
|
|
|
|
widget.customAppBar ??
|
|
|
|
AppBar(
|
2024-02-06 09:51:25 +01:00
|
|
|
title: Text(widget.appBarTitle),
|
2022-09-28 09:23:41 +02:00
|
|
|
);
|
|
|
|
|
2023-02-16 15:02:03 +01:00
|
|
|
void onPrevious() {
|
|
|
|
_pageController.previousPage(
|
|
|
|
duration: _animationDuration,
|
|
|
|
curve: _animationCurve,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-16 15:46:23 +02:00
|
|
|
Future<void> onNext(AuthStep step) async {
|
2023-02-16 15:02:03 +01:00
|
|
|
if (!_formKey.currentState!.validate()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_formKey.currentState!.save();
|
|
|
|
|
|
|
|
FocusScope.of(context).unfocus();
|
|
|
|
|
|
|
|
if (widget.steps.last == step) {
|
2024-02-05 13:00:21 +01:00
|
|
|
var values = HashMap<String, dynamic>();
|
2023-02-16 15:02:03 +01:00
|
|
|
|
|
|
|
for (var step in widget.steps) {
|
|
|
|
for (var field in step.fields) {
|
2024-02-05 13:00:21 +01:00
|
|
|
values[field.name] = field.value;
|
2023-02-16 15:02:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 15:46:23 +02:00
|
|
|
await widget.onFinish(
|
2023-02-16 15:02:03 +01:00
|
|
|
values: values,
|
2023-10-03 14:38:52 +02:00
|
|
|
onError: (int? pageToReturn) => _pageController.animateToPage(
|
|
|
|
pageToReturn ?? 0,
|
2023-02-16 15:02:03 +01:00
|
|
|
duration: _animationDuration,
|
|
|
|
curve: _animationCurve,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_pageController.nextPage(
|
|
|
|
duration: _animationDuration,
|
|
|
|
curve: _animationCurve,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 15:51:22 +02:00
|
|
|
@override
|
2023-03-31 11:15:48 +02:00
|
|
|
Widget build(BuildContext context) {
|
2024-02-01 14:40:25 +01:00
|
|
|
var previousButton = widget.previousButtonBuilder?.call(
|
|
|
|
onPrevious,
|
|
|
|
widget.previousBtnTitle,
|
|
|
|
);
|
|
|
|
|
2023-03-31 11:15:48 +02:00
|
|
|
return Scaffold(
|
|
|
|
backgroundColor: widget.customBackgroundColor ?? Colors.white,
|
|
|
|
appBar: _appBar,
|
|
|
|
body: Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: PageView(
|
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
controller: _pageController,
|
|
|
|
children: <Widget>[
|
|
|
|
for (AuthStep step in widget.steps)
|
|
|
|
Column(
|
2024-02-06 09:51:25 +01:00
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
2023-03-31 11:15:48 +02:00
|
|
|
children: [
|
2024-02-02 22:18:03 +01:00
|
|
|
if (widget.titleWidget != null) widget.titleWidget!,
|
2024-02-06 12:54:08 +01:00
|
|
|
Expanded(
|
2024-02-02 22:18:03 +01:00
|
|
|
child: ListView(
|
|
|
|
physics: const ClampingScrollPhysics(),
|
|
|
|
shrinkWrap: true,
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: 8.0,
|
|
|
|
horizontal: 30.0,
|
2022-09-26 10:35:53 +02:00
|
|
|
),
|
2024-02-02 22:18:03 +01:00
|
|
|
children: [
|
2024-02-06 12:54:08 +01:00
|
|
|
const SizedBox(height: 40),
|
2024-02-02 22:18:03 +01:00
|
|
|
for (AuthField field in step.fields)
|
|
|
|
Align(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
if (field.title != null) ...[
|
|
|
|
field.title!,
|
|
|
|
],
|
|
|
|
field.build(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2022-09-26 10:35:53 +02:00
|
|
|
),
|
2023-03-31 11:15:48 +02:00
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
top: 15.0,
|
2024-02-06 09:51:25 +01:00
|
|
|
bottom: 30.0,
|
2023-03-31 11:15:48 +02:00
|
|
|
),
|
2024-02-06 16:28:51 +01:00
|
|
|
child: Column(
|
2023-03-31 11:15:48 +02:00
|
|
|
children: [
|
2024-02-06 16:28:51 +01:00
|
|
|
Row(
|
2024-02-08 10:29:32 +01:00
|
|
|
mainAxisAlignment:
|
|
|
|
(widget.previousButtonBuilder != null &&
|
|
|
|
previousButton == null)
|
|
|
|
? MainAxisAlignment.spaceAround
|
|
|
|
: widget.steps.first != step
|
|
|
|
? MainAxisAlignment.spaceBetween
|
|
|
|
: MainAxisAlignment.end,
|
2024-02-06 16:28:51 +01:00
|
|
|
children: [
|
|
|
|
if (widget.steps.first != step)
|
|
|
|
if (widget.previousButtonBuilder == null) ...[
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: onPrevious,
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
const Icon(
|
|
|
|
Icons.arrow_back,
|
|
|
|
size: 18,
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding:
|
|
|
|
const EdgeInsets.only(left: 4.0),
|
|
|
|
child: Text(widget.previousBtnTitle),
|
|
|
|
),
|
|
|
|
],
|
2024-02-01 14:40:25 +01:00
|
|
|
),
|
2024-02-06 16:28:51 +01:00
|
|
|
),
|
|
|
|
] else if (previousButton != null) ...[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: previousButton,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: widget.nextButtonBuilder?.call(
|
|
|
|
() async {
|
|
|
|
await onNext(step);
|
|
|
|
},
|
2023-03-31 11:15:48 +02:00
|
|
|
widget.steps.last == step
|
|
|
|
? widget.submitBtnTitle
|
|
|
|
: widget.nextBtnTitle,
|
2024-02-06 16:28:51 +01:00
|
|
|
) ??
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () async {
|
|
|
|
await onNext(step);
|
|
|
|
},
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
widget.steps.last == step
|
|
|
|
? widget.submitBtnTitle
|
|
|
|
: widget.nextBtnTitle,
|
|
|
|
),
|
|
|
|
const Padding(
|
|
|
|
padding: EdgeInsets.only(left: 4.0),
|
|
|
|
child: Icon(
|
|
|
|
Icons.arrow_forward,
|
|
|
|
size: 18,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2023-03-31 11:15:48 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2024-02-06 16:28:51 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
if (widget.loginButton != null)
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 20.0),
|
|
|
|
child: widget.loginButton!,
|
|
|
|
),
|
2023-03-31 11:15:48 +02:00
|
|
|
],
|
|
|
|
),
|
2024-02-02 22:18:03 +01:00
|
|
|
),
|
2023-03-31 11:15:48 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
2022-09-20 15:51:22 +02:00
|
|
|
),
|
2023-03-31 11:15:48 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-09-20 15:51:22 +02:00
|
|
|
}
|