flutter_app/lib/widgets/passwordfield.dart

71 lines
3.0 KiB
Dart
Raw Normal View History

2021-06-03 18:32:25 +00:00
import 'package:cwtch/cwtch_icons_icons.dart';
2021-03-03 00:42:44 +00:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
2021-03-10 17:40:14 +00:00
import '../settings.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2021-03-03 00:42:44 +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 {
2021-06-03 18:32:25 +00:00
CwtchPasswordField({required this.controller, required this.validator, this.action, this.autofocus = false});
2021-03-03 00:42:44 +00:00
final TextEditingController controller;
final FormFieldValidator validator;
final Function(String)? action;
final bool autofocus;
2021-03-03 00:42:44 +00:00
@override
_CwtchTextFieldState createState() => _CwtchTextFieldState();
}
class _CwtchTextFieldState extends State<CwtchPasswordField> {
bool obscureText = true;
2021-03-03 00:42:44 +00:00
@override
Widget build(BuildContext context) {
// todo: translations
var label = AppLocalizations.of(context)!.tooltipShowPassword;
if (!obscureText) {
label = AppLocalizations.of(context)!.tooltipHidePassword;
}
2021-03-10 17:40:14 +00:00
return Consumer<Settings>(builder: (context, theme, child) {
2021-03-03 01:38:47 +00:00
return TextFormField(
autofocus: widget.autofocus,
2021-03-03 01:38:47 +00:00
controller: widget.controller,
validator: widget.validator,
obscureText: obscureText,
autovalidateMode: AutovalidateMode.always,
onFieldSubmitted: widget.action,
textInputAction: TextInputAction.unspecified,
2021-03-03 01:38:47 +00:00
enableSuggestions: false,
autocorrect: false,
decoration: InputDecoration(
suffixIcon: IconButton(
onPressed: () {
setState(() {
obscureText = !obscureText;
});
},
2021-06-03 18:32:25 +00:00
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(),
),
2021-03-10 17:40:14 +00:00
errorStyle: TextStyle(
color: theme.current().textfieldErrorColor(),
fontWeight: FontWeight.bold,
),
2021-03-16 23:33:03 +00:00
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(15.0), borderSide: BorderSide(color: theme.current().textfieldBorderColor(), width: 3.0)),
focusedErrorBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(15.0), borderSide: BorderSide(color: theme.current().textfieldErrorColor(), width: 3.0)),
errorBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(15.0), borderSide: BorderSide(color: theme.current().textfieldErrorColor(), width: 3.0)),
2021-03-10 17:40:14 +00:00
filled: true,
fillColor: theme.current().textfieldBackgroundColor(),
2021-03-16 23:33:03 +00:00
enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(15.0), borderSide: BorderSide(color: theme.current().textfieldBorderColor(), width: 3.0)),
2021-03-03 22:08:15 +00:00
),
2021-03-03 01:38:47 +00:00
);
2021-03-03 00:42:44 +00:00
});
}
}