2022-11-29 13:16:44 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022 Iconica
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2023-12-07 14:29:38 +01:00
|
|
|
import 'package:flutter/services.dart';
|
2022-11-29 13:16:44 +01:00
|
|
|
|
2024-02-06 10:40:45 +01:00
|
|
|
/// Generates a [TextFormField] for passwords. It requires a
|
|
|
|
/// [FlutterFormInputController] as the [controller] parameter and an
|
2024-02-02 11:48:45 +01:00
|
|
|
/// optional [Widget] as [label]
|
2023-03-26 12:16:25 +02:00
|
|
|
class FlutterFormInputPassword extends StatefulWidget {
|
2022-11-29 13:16:44 +01:00
|
|
|
const FlutterFormInputPassword({
|
2024-02-02 11:48:45 +01:00
|
|
|
super.key,
|
2022-11-29 13:16:44 +01:00
|
|
|
this.label,
|
2023-01-11 10:28:25 +01:00
|
|
|
this.focusNode,
|
2023-12-08 10:55:45 +01:00
|
|
|
this.style,
|
2022-11-29 13:16:44 +01:00
|
|
|
this.initialValue,
|
2023-12-07 14:29:38 +01:00
|
|
|
this.inputFormatters,
|
2022-11-29 13:16:44 +01:00
|
|
|
this.onSaved,
|
|
|
|
this.validator,
|
|
|
|
this.onChanged,
|
|
|
|
this.onFieldSubmitted,
|
2023-10-26 14:08:21 +02:00
|
|
|
this.enabled = true,
|
2024-02-01 11:15:35 +01:00
|
|
|
this.decoration,
|
2024-02-07 16:13:35 +01:00
|
|
|
this.controller,
|
2024-02-02 11:48:45 +01:00
|
|
|
});
|
|
|
|
final Widget? label;
|
|
|
|
final FocusNode? focusNode;
|
|
|
|
final TextStyle? style;
|
|
|
|
final String? initialValue;
|
|
|
|
final List<TextInputFormatter>? inputFormatters;
|
|
|
|
final Function(String?)? onSaved;
|
|
|
|
final String? Function(String?)? validator;
|
|
|
|
final Function(String?)? onChanged;
|
|
|
|
final Function(String?)? onFieldSubmitted;
|
|
|
|
final bool enabled;
|
|
|
|
final InputDecoration? decoration;
|
2024-02-07 16:13:35 +01:00
|
|
|
final TextEditingController? controller;
|
2022-11-29 13:16:44 +01:00
|
|
|
|
|
|
|
@override
|
2023-03-26 12:49:54 +02:00
|
|
|
State<FlutterFormInputPassword> createState() => _PasswordTextFieldState();
|
2022-11-29 13:16:44 +01:00
|
|
|
}
|
|
|
|
|
2023-03-26 12:16:25 +02:00
|
|
|
class _PasswordTextFieldState extends State<FlutterFormInputPassword> {
|
2022-11-29 13:16:44 +01:00
|
|
|
bool obscured = true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-02-01 11:15:35 +01:00
|
|
|
var suffixIcon = IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
obscured = !obscured;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: Icon(obscured ? Icons.visibility_off : Icons.visibility),
|
|
|
|
);
|
|
|
|
|
|
|
|
var decoration = widget.decoration?.copyWith(suffixIcon: suffixIcon) ??
|
|
|
|
InputDecoration(suffixIcon: suffixIcon);
|
|
|
|
|
2022-11-29 13:16:44 +01:00
|
|
|
return TextFormField(
|
2024-02-07 16:13:35 +01:00
|
|
|
controller: widget.controller,
|
2023-12-08 10:55:45 +01:00
|
|
|
style: widget.style,
|
2022-11-29 13:16:44 +01:00
|
|
|
initialValue: widget.initialValue,
|
2023-12-07 14:29:38 +01:00
|
|
|
inputFormatters: widget.inputFormatters,
|
2022-11-29 13:16:44 +01:00
|
|
|
obscureText: obscured,
|
2023-01-11 10:28:25 +01:00
|
|
|
focusNode: widget.focusNode,
|
2022-11-29 13:16:44 +01:00
|
|
|
onSaved: (value) => widget.onSaved?.call(value),
|
|
|
|
validator: (value) => widget.validator?.call(value),
|
|
|
|
onChanged: (value) => widget.onChanged?.call(value),
|
|
|
|
onFieldSubmitted: (value) => widget.onFieldSubmitted?.call(value),
|
2024-02-01 11:15:35 +01:00
|
|
|
decoration: decoration,
|
2023-10-26 14:08:21 +02:00
|
|
|
enabled: widget.enabled,
|
2022-11-29 13:16:44 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|