Line data Source code
1 : import 'package:cwtch/cwtch_icons_icons.dart';
2 : import 'package:flutter/material.dart';
3 : import 'package:provider/provider.dart';
4 : import '../settings.dart';
5 : import 'package:cwtch/l10n/app_localizations.dart';
6 :
7 : const hints = [AutofillHints.password];
8 :
9 : // Provides a styled Password Input Field for use in Form Widgets.
10 : // Callers must provide a text controller, label helper text and a validator.
11 : class CwtchPasswordField extends StatefulWidget {
12 0 : CwtchPasswordField({required this.controller, required this.validator, this.action, this.autofocus = false, this.autoFillHints = hints, this.key});
13 : final TextEditingController controller;
14 : final FormFieldValidator validator;
15 : final Function(String)? action;
16 : final bool autofocus;
17 : final Iterable<String> autoFillHints;
18 : final Key? key;
19 :
20 0 : @override
21 0 : _CwtchPasswordTextFieldState createState() => _CwtchPasswordTextFieldState();
22 : }
23 :
24 : class _CwtchPasswordTextFieldState extends State<CwtchPasswordField> {
25 : bool obscureText = true;
26 :
27 0 : @override
28 : void initState() {
29 0 : super.initState();
30 : }
31 :
32 0 : @override
33 : Widget build(BuildContext context) {
34 : // todo: translations
35 0 : var label = AppLocalizations.of(context)!.tooltipShowPassword;
36 0 : if (!obscureText) {
37 0 : label = AppLocalizations.of(context)!.tooltipHidePassword;
38 : }
39 :
40 0 : return Consumer<Settings>(
41 0 : builder: (context, theme, child) {
42 : // Horrifying Hack: Flutter doesn't give us direct control over system menus but instead picks BG color from TextButtonThemeData ¯\_(ツ)_/¯
43 0 : return Theme(
44 0 : data: Theme.of(context).copyWith(
45 0 : textButtonTheme: TextButtonThemeData(style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Provider.of<Settings>(context).theme.menuBackgroundColor))),
46 : ),
47 0 : child: TextFormField(
48 0 : autofocus: widget.autofocus,
49 0 : controller: widget.controller,
50 0 : validator: widget.validator,
51 0 : obscureText: obscureText,
52 : obscuringCharacter: '*',
53 : enableIMEPersonalizedLearning: false,
54 0 : autofillHints: widget.autoFillHints,
55 : autovalidateMode: AutovalidateMode.always,
56 0 : onFieldSubmitted: widget.action,
57 : enableSuggestions: false,
58 : autocorrect: false,
59 0 : decoration: InputDecoration(
60 0 : suffixIcon: IconButton(
61 0 : onPressed: () {
62 0 : setState(() {
63 0 : obscureText = !obscureText;
64 : });
65 : },
66 0 : icon: Icon((obscureText ? CwtchIcons.eye_closed : CwtchIcons.eye_open), semanticLabel: label),
67 : tooltip: label,
68 0 : color: theme.current().mainTextColor,
69 0 : highlightColor: theme.current().defaultButtonColor,
70 0 : focusColor: theme.current().defaultButtonActiveColor,
71 0 : splashColor: theme.current().defaultButtonActiveColor,
72 0 : splashRadius: Material.defaultSplashRadius / 2,
73 : ),
74 0 : errorStyle: TextStyle(color: theme.current().textfieldErrorColor, fontWeight: FontWeight.bold),
75 0 : focusedBorder: OutlineInputBorder(
76 0 : borderRadius: BorderRadius.circular(6.0),
77 0 : borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0),
78 : ),
79 0 : focusedErrorBorder: OutlineInputBorder(
80 0 : borderRadius: BorderRadius.circular(6.0),
81 0 : borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0),
82 : ),
83 0 : errorBorder: OutlineInputBorder(
84 0 : borderRadius: BorderRadius.circular(6.0),
85 0 : borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0),
86 : ),
87 : filled: true,
88 0 : fillColor: theme.current().textfieldBackgroundColor,
89 0 : enabledBorder: OutlineInputBorder(
90 0 : borderRadius: BorderRadius.circular(6.0),
91 0 : borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0),
92 : ),
93 : ),
94 : ),
95 : );
96 : },
97 : );
98 : }
99 : }
|