flutter_login_widget/lib/src/widgets/mfa_widget.dart

129 lines
4.5 KiB
Dart
Raw Normal View History

2023-03-07 10:37:07 +01:00
import 'package:flutter/material.dart';
import 'package:pinput/pinput.dart';
class MFAWidget extends StatefulWidget {
2024-02-19 16:03:18 +01:00
/// Constructs an [MFAWidget].
///
/// [onCompleted]: Callback function triggered when the MFA code is completed.
/// [onSubmitted]: Callback function triggered when the MFA code is submitted.
/// [length]: The length of the MFA code.
/// [defaultPinTheme]: The theme for the default state of the input pins.
/// [focusedPinTheme]: The theme for the focused state of the input pins.
/// [submittedPinTheme]: The theme for the submitted state of the input pins.
/// [followingPinTheme]: The theme for the pins following the submitted pin.
/// [disabledPinTheme]: The theme for disabled input pins.
/// [errorPinTheme]: The theme for input pins in error state.
/// [seperatorPositions]: Positions for separators between input pins.
/// [errorText]: Text to display when there's an error.
/// [validator]: Validator function to validate the input.
/// [errorBuilder]: Builder function to customize the error display.
/// [errorTextStyle]: Style for the error text.
/// [submitButtonBuilder]: Builder function to customize the submit button.
2023-05-08 18:09:19 +02:00
const MFAWidget({
required this.onCompleted,
this.onSubmitted,
this.defaultPinTheme,
this.focusedPinTheme,
this.submittedPinTheme,
this.followingPinTheme,
this.disabledPinTheme,
this.errorPinTheme,
this.seperatorPositions,
this.errorBuilder,
this.errorText,
this.errorTextStyle,
this.validator,
this.submitButtonBuilder,
this.length = 6,
super.key,
}) : assert(
2023-03-07 11:20:33 +01:00
(onSubmitted == null && submitButtonBuilder == null) ||
(onSubmitted != null && submitButtonBuilder != null),
2024-02-06 16:05:46 +01:00
'onSubmitted and submitButtonBuilder must be both null or both'
' not null',
2023-03-07 10:37:07 +01:00
);
2024-02-19 16:03:18 +01:00
/// Callback function triggered when the MFA code is completed.
2023-03-07 10:37:07 +01:00
final Function(String code) onCompleted;
2024-02-19 16:03:18 +01:00
/// Callback function triggered when the MFA code is submitted.
2023-03-07 10:37:07 +01:00
final Function(String code)? onSubmitted;
2024-02-19 16:03:18 +01:00
/// The length of the MFA code.
2023-03-07 11:20:33 +01:00
final int length;
2024-02-19 16:03:18 +01:00
/// The theme for the default state of the input pins.
2023-03-07 10:37:07 +01:00
final PinTheme? defaultPinTheme;
2024-02-19 16:03:18 +01:00
/// The theme for the focused state of the input pins.
2023-03-07 10:37:07 +01:00
final PinTheme? focusedPinTheme;
2024-02-19 16:03:18 +01:00
/// The theme for the submitted state of the input pins.
2023-03-07 10:37:07 +01:00
final PinTheme? submittedPinTheme;
2024-02-19 16:03:18 +01:00
/// The theme for the pins following the submitted pin.
2023-03-07 10:37:07 +01:00
final PinTheme? followingPinTheme;
2024-02-19 16:03:18 +01:00
/// The theme for disabled input pins.
2023-03-07 10:37:07 +01:00
final PinTheme? disabledPinTheme;
2024-02-19 16:03:18 +01:00
/// The theme for input pins in error state.
2023-03-07 10:37:07 +01:00
final PinTheme? errorPinTheme;
2024-02-19 16:03:18 +01:00
/// Positions for separators between input pins.
2023-03-07 10:37:07 +01:00
final List<int>? seperatorPositions;
2024-02-19 16:03:18 +01:00
/// Text to display when there's an error.
2023-03-07 10:37:07 +01:00
final String? errorText;
2024-02-19 16:03:18 +01:00
/// Validator function to validate the input.
2023-03-07 10:37:07 +01:00
final String? Function(String?)? validator;
2024-02-19 16:03:18 +01:00
/// Builder function to customize the error display.
2023-03-07 10:37:07 +01:00
final Widget Function(String?, String)? errorBuilder;
2024-02-19 16:03:18 +01:00
/// Style for the error text.
2023-03-07 10:37:07 +01:00
final TextStyle? errorTextStyle;
2024-02-19 16:03:18 +01:00
/// Builder function to customize the submit button.
2023-03-07 10:37:07 +01:00
final Widget Function(Function onTap)? submitButtonBuilder;
@override
State<MFAWidget> createState() => _MFAWidgetState();
}
class _MFAWidgetState extends State<MFAWidget> {
final TextEditingController _controller = TextEditingController();
@override
2024-02-06 16:05:46 +01:00
Widget build(BuildContext context) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Pinput(
defaultPinTheme: widget.defaultPinTheme,
focusedPinTheme: widget.focusedPinTheme,
submittedPinTheme: widget.submittedPinTheme,
followingPinTheme: widget.followingPinTheme,
disabledPinTheme: widget.disabledPinTheme,
errorPinTheme: widget.errorPinTheme,
separatorPositions: widget.seperatorPositions,
errorBuilder: widget.errorBuilder,
errorText: widget.errorText,
errorTextStyle: widget.errorTextStyle,
validator: widget.validator,
controller: _controller,
length: widget.length,
onCompleted: (_) {
widget.onCompleted(_controller.text);
},
),
if (widget.onSubmitted != null &&
widget.submitButtonBuilder != null) ...[
widget.submitButtonBuilder!(() {
widget.onSubmitted!(_controller.text);
}),
],
2023-03-07 10:37:07 +01:00
],
2024-02-06 16:05:46 +01:00
);
2023-03-07 10:37:07 +01:00
}