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
|
|
|
|
2024-02-19 13:31:12 +01:00
|
|
|
/// A widget for handling multi-step authentication processes.
|
2022-09-20 15:51:22 +02:00
|
|
|
class AuthScreen extends StatefulWidget {
|
2024-02-19 13:31:12 +01:00
|
|
|
/// Constructs an [AuthScreen] object.
|
|
|
|
///
|
|
|
|
/// [appBarTitle] specifies the title of the app bar.
|
|
|
|
///
|
|
|
|
/// [onFinish] is a function called upon completion of the authentication process.
|
|
|
|
///
|
|
|
|
/// [steps] is a list of authentication steps to be completed.
|
|
|
|
///
|
|
|
|
/// [submitBtnTitle] specifies the title of the submit button.
|
|
|
|
///
|
|
|
|
/// [nextBtnTitle] specifies the title of the next button.
|
|
|
|
///
|
|
|
|
/// [previousBtnTitle] specifies the title of the previous button.
|
|
|
|
///
|
|
|
|
/// [customAppBar] allows customization of the app bar.
|
|
|
|
///
|
|
|
|
/// [buttonMainAxisAlignment] specifies the alignment of the buttons.
|
|
|
|
///
|
|
|
|
/// [customBackgroundColor] allows customization of the background color.
|
|
|
|
///
|
|
|
|
/// [nextButtonBuilder] allows customization of the next button.
|
|
|
|
///
|
|
|
|
/// [previousButtonBuilder] allows customization of the previous button.
|
|
|
|
///
|
|
|
|
/// [titleWidget] specifies a custom widget to be displayed at the top of the screen.
|
|
|
|
///
|
|
|
|
/// [loginButton] specifies a custom login button widget.
|
|
|
|
///
|
|
|
|
/// [titleFlex] specifies the flex value for the title widget.
|
|
|
|
///
|
|
|
|
/// [formFlex] specifies the flex value for the form widget.
|
|
|
|
///
|
|
|
|
/// [beforeTitleFlex] specifies the flex value before the title widget.
|
|
|
|
///
|
|
|
|
/// [afterTitleFlex] specifies the flex value after the title widget.
|
|
|
|
///
|
|
|
|
/// [isLoading] indicates whether the screen is in a loading state.
|
2022-09-20 15:51:22 +02:00
|
|
|
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,
|
2024-02-14 20:04:22 +01:00
|
|
|
this.buttonMainAxisAlignment,
|
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,
|
2024-02-14 09:57:29 +01:00
|
|
|
this.titleFlex,
|
|
|
|
this.formFlex,
|
|
|
|
this.beforeTitleFlex,
|
|
|
|
this.afterTitleFlex,
|
2024-02-15 17:16:04 +01:00
|
|
|
this.isLoading = false,
|
2024-02-19 13:31:12 +01:00
|
|
|
Key? key,
|
|
|
|
}) : assert(steps.length > 0, 'At least one step is required'),
|
|
|
|
super(key: key);
|
2022-09-20 15:51:22 +02:00
|
|
|
|
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;
|
2024-02-14 20:04:22 +01:00
|
|
|
final MainAxisAlignment? buttonMainAxisAlignment;
|
2023-03-31 11:15:48 +02:00
|
|
|
final Color? customBackgroundColor;
|
2024-02-09 18:36:22 +01:00
|
|
|
final Widget Function(Future<void> Function()? onPressed, String label,
|
|
|
|
int step, bool enabled)? nextButtonBuilder;
|
2024-02-08 11:54:07 +01:00
|
|
|
final Widget? Function(VoidCallback onPressed, String label, int step)?
|
|
|
|
previousButtonBuilder;
|
2024-02-02 22:18:03 +01:00
|
|
|
final Widget? titleWidget;
|
|
|
|
final Widget? loginButton;
|
2024-02-14 09:57:29 +01:00
|
|
|
final int? titleFlex;
|
|
|
|
final int? formFlex;
|
|
|
|
final int? beforeTitleFlex;
|
|
|
|
final int? afterTitleFlex;
|
2024-02-15 17:16:04 +01:00
|
|
|
final bool isLoading;
|
2022-09-20 15:51:22 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<AuthScreen> createState() => _AuthScreenState();
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:31:12 +01:00
|
|
|
/// The state for [AuthScreen].
|
2022-09-20 15:51:22 +02:00
|
|
|
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;
|
2024-02-09 18:36:22 +01:00
|
|
|
bool _formValid = false;
|
2022-09-20 15:51:22 +02:00
|
|
|
|
2024-02-19 13:31:12 +01:00
|
|
|
/// Gets the app bar.
|
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
|
|
|
);
|
|
|
|
|
2024-02-19 13:31:12 +01:00
|
|
|
/// Handles previous button press.
|
2023-02-16 15:02:03 +01:00
|
|
|
void onPrevious() {
|
2024-02-14 10:44:14 +01:00
|
|
|
FocusScope.of(context).unfocus();
|
2024-02-09 18:36:22 +01:00
|
|
|
_validate(_pageController.page!.toInt() - 1);
|
2023-02-16 15:02:03 +01:00
|
|
|
_pageController.previousPage(
|
|
|
|
duration: _animationDuration,
|
|
|
|
curve: _animationCurve,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:31:12 +01:00
|
|
|
/// Handles next button press.
|
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 {
|
2024-02-09 18:36:22 +01:00
|
|
|
_validate(_pageController.page!.toInt() + 1);
|
2023-02-16 15:02:03 +01:00
|
|
|
_pageController.nextPage(
|
|
|
|
duration: _animationDuration,
|
|
|
|
curve: _animationCurve,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:31:12 +01:00
|
|
|
/// Validates the current step.
|
2024-02-09 18:36:22 +01:00
|
|
|
void _validate(int currentPage) {
|
|
|
|
bool isStepValid = true;
|
|
|
|
|
|
|
|
// Loop through each field in the current step
|
|
|
|
for (var field in widget.steps[currentPage].fields) {
|
|
|
|
for (var validator in field.validators) {
|
|
|
|
String? validationResult = validator(field.value);
|
|
|
|
if (validationResult != null) {
|
|
|
|
// If any validator returns an error, mark step as invalid and break
|
|
|
|
isStepValid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isStepValid) {
|
|
|
|
break; // No need to check further fields if one is already invalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
_formValid = isStepValid;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-20 15:51:22 +02:00
|
|
|
@override
|
2023-03-31 11:15:48 +02:00
|
|
|
Widget build(BuildContext context) {
|
2024-02-15 17:16:04 +01:00
|
|
|
return widget.isLoading
|
|
|
|
? const Center(
|
|
|
|
child: SizedBox(
|
|
|
|
height: 120,
|
|
|
|
width: 120,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: Scaffold(
|
|
|
|
backgroundColor: widget.customBackgroundColor ?? Colors.white,
|
|
|
|
appBar: _appBar,
|
|
|
|
body: Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: PageView(
|
2024-02-19 13:31:12 +01:00
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
controller: _pageController,
|
|
|
|
children: <Widget>[
|
|
|
|
for (var i = 0; i < widget.steps.length; i++)
|
|
|
|
Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
if (widget.titleWidget != null) ...[
|
|
|
|
Expanded(
|
|
|
|
flex: widget.titleFlex ?? 1,
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
flex: widget.beforeTitleFlex ?? 3,
|
|
|
|
child: Container(),
|
2024-02-06 16:28:51 +01:00
|
|
|
),
|
2024-02-19 13:31:12 +01:00
|
|
|
widget.titleWidget!,
|
|
|
|
Expanded(
|
|
|
|
flex: widget.afterTitleFlex ?? 2,
|
|
|
|
child: Container(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: widget
|
|
|
|
.buttonMainAxisAlignment !=
|
|
|
|
null
|
|
|
|
? widget.buttonMainAxisAlignment!
|
|
|
|
: (widget.previousButtonBuilder != null &&
|
|
|
|
widget.previousButtonBuilder?.call(
|
|
|
|
onPrevious,
|
|
|
|
widget.previousBtnTitle,
|
|
|
|
i,
|
|
|
|
) ==
|
|
|
|
null)
|
|
|
|
? MainAxisAlignment.start
|
|
|
|
: widget.steps.first != widget.steps[i]
|
|
|
|
? MainAxisAlignment.center
|
|
|
|
: MainAxisAlignment.end,
|
2024-02-15 17:16:04 +01:00
|
|
|
children: [
|
2024-02-19 13:31:12 +01:00
|
|
|
if (widget.previousButtonBuilder == null) ...[
|
|
|
|
if (widget.steps.first != widget.steps[i])
|
|
|
|
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-15 17:16:04 +01:00
|
|
|
),
|
2024-02-19 13:31:12 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
] else if (widget.previousButtonBuilder?.call(
|
|
|
|
onPrevious,
|
|
|
|
widget.previousBtnTitle,
|
|
|
|
i) !=
|
|
|
|
null) ...[
|
|
|
|
widget.previousButtonBuilder!.call(
|
|
|
|
onPrevious, widget.previousBtnTitle, i)!
|
|
|
|
],
|
|
|
|
widget.nextButtonBuilder?.call(
|
|
|
|
!_formValid
|
|
|
|
? null
|
|
|
|
: () async {
|
|
|
|
await onNext(widget.steps[i]);
|
|
|
|
},
|
|
|
|
widget.steps.last == widget.steps[i]
|
|
|
|
? widget.submitBtnTitle
|
|
|
|
: widget.nextBtnTitle,
|
|
|
|
i,
|
|
|
|
_formValid,
|
|
|
|
) ??
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: !_formValid
|
|
|
|
? null
|
|
|
|
: () async {
|
|
|
|
await onNext(widget.steps[i]);
|
|
|
|
},
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
widget.steps.last ==
|
|
|
|
widget.steps[i]
|
|
|
|
? widget.submitBtnTitle
|
|
|
|
: widget.nextBtnTitle,
|
|
|
|
),
|
|
|
|
const Padding(
|
|
|
|
padding:
|
|
|
|
EdgeInsets.only(left: 4.0),
|
|
|
|
child: Icon(
|
|
|
|
Icons.arrow_forward,
|
|
|
|
size: 18,
|
|
|
|
),
|
2024-02-15 17:16:04 +01:00
|
|
|
),
|
2024-02-19 13:31:12 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2024-02-15 17:16:04 +01:00
|
|
|
],
|
2024-02-14 20:04:22 +01:00
|
|
|
),
|
2024-02-19 13:31:12 +01:00
|
|
|
if (widget.loginButton != null)
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 20.0),
|
|
|
|
child: widget.loginButton!,
|
|
|
|
),
|
2024-02-15 17:16:04 +01:00
|
|
|
],
|
2024-02-19 13:31:12 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2023-03-31 11:15:48 +02:00
|
|
|
}
|
2022-09-20 15:51:22 +02:00
|
|
|
}
|