2021-06-24 23:10:45 +00:00
|
|
|
import 'package:cwtch/cwtch_icons_icons.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../settings.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
2021-06-27 15:16:05 +00:00
|
|
|
const hints = [AutofillHints.password];
|
|
|
|
|
2021-06-24 23:10:45 +00:00
|
|
|
// Provides a styled Password Input Field for use in Form Widgets.
|
|
|
|
// Callers must provide a text controller, label helper text and a validator.
|
|
|
|
class CwtchPasswordField extends StatefulWidget {
|
2022-02-05 00:37:25 +00:00
|
|
|
CwtchPasswordField({required this.controller, required this.validator, this.action, this.autofocus = false, this.autoFillHints = hints, this.key});
|
2021-06-24 23:10:45 +00:00
|
|
|
final TextEditingController controller;
|
|
|
|
final FormFieldValidator validator;
|
|
|
|
final Function(String)? action;
|
|
|
|
final bool autofocus;
|
2021-06-27 15:16:05 +00:00
|
|
|
final Iterable<String> autoFillHints;
|
2022-02-05 00:37:25 +00:00
|
|
|
final Key? key;
|
2021-06-24 23:10:45 +00:00
|
|
|
|
|
|
|
@override
|
2022-02-05 00:37:25 +00:00
|
|
|
_CwtchPasswordTextFieldState createState() => _CwtchPasswordTextFieldState();
|
2021-06-24 23:10:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 00:37:25 +00:00
|
|
|
class _CwtchPasswordTextFieldState extends State<CwtchPasswordField> {
|
2021-06-24 23:10:45 +00:00
|
|
|
bool obscureText = true;
|
|
|
|
|
2022-02-05 00:37:25 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2021-06-24 23:10:45 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
// todo: translations
|
|
|
|
var label = AppLocalizations.of(context)!.tooltipShowPassword;
|
|
|
|
if (!obscureText) {
|
|
|
|
label = AppLocalizations.of(context)!.tooltipHidePassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Consumer<Settings>(builder: (context, theme, child) {
|
2024-02-07 16:16:33 +00:00
|
|
|
// Horrifying Hack: Flutter doesn't give us direct control over system menus but instead picks BG color from TextButtonThemeData ¯\_(ツ)_/¯
|
|
|
|
return Theme(
|
|
|
|
data: Theme.of(context).copyWith(
|
|
|
|
textButtonTheme: TextButtonThemeData(
|
|
|
|
style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Provider.of<Settings>(context).theme.menuBackgroundColor)),
|
|
|
|
),
|
2021-06-24 23:10:45 +00:00
|
|
|
),
|
2024-02-07 16:16:33 +00:00
|
|
|
child: TextFormField(
|
|
|
|
autofocus: widget.autofocus,
|
|
|
|
controller: widget.controller,
|
|
|
|
validator: widget.validator,
|
|
|
|
obscureText: obscureText,
|
|
|
|
obscuringCharacter: '*',
|
|
|
|
enableIMEPersonalizedLearning: false,
|
|
|
|
autofillHints: widget.autoFillHints,
|
|
|
|
autovalidateMode: AutovalidateMode.always,
|
|
|
|
onFieldSubmitted: widget.action,
|
|
|
|
enableSuggestions: false,
|
|
|
|
autocorrect: false,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
suffixIcon: IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
obscureText = !obscureText;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: Icon((obscureText ? CwtchIcons.eye_closed : CwtchIcons.eye_open), semanticLabel: label),
|
|
|
|
tooltip: label,
|
|
|
|
color: theme.current().mainTextColor,
|
|
|
|
highlightColor: theme.current().defaultButtonColor,
|
|
|
|
focusColor: theme.current().defaultButtonActiveColor,
|
|
|
|
splashColor: theme.current().defaultButtonActiveColor,
|
|
|
|
splashRadius: Material.defaultSplashRadius / 2,
|
|
|
|
),
|
|
|
|
errorStyle: TextStyle(
|
|
|
|
color: theme.current().textfieldErrorColor,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
|
|
|
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(6.0), borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0)),
|
|
|
|
focusedErrorBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(6.0), borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0)),
|
|
|
|
errorBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(6.0), borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0)),
|
|
|
|
filled: true,
|
|
|
|
fillColor: theme.current().textfieldBackgroundColor,
|
|
|
|
enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(6.0), borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0)),
|
|
|
|
),
|
|
|
|
));
|
2021-06-24 23:10:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|