Line data Source code
1 : import 'package:cwtch/themes/opaque.dart';
2 : import 'package:flutter/material.dart';
3 : import 'package:flutter/services.dart';
4 : import 'package:provider/provider.dart';
5 : import '../settings.dart';
6 :
7 1 : doNothing(String x) {}
8 :
9 : // Provides a styled Text Field for use in Form Widgets.
10 : // Callers must provide a text controller, label helper text and a validator.
11 : class CwtchTextField extends StatefulWidget {
12 1 : CwtchTextField({
13 : required this.controller,
14 : this.hintText = "",
15 : this.validator,
16 : this.autofocus = false,
17 : this.onChanged = doNothing,
18 : this.number = false,
19 : this.multiLine = false,
20 : this.key,
21 : this.testKey,
22 : });
23 : final TextEditingController controller;
24 : final String hintText;
25 : final FormFieldValidator? validator;
26 : final Function(String) onChanged;
27 : final bool autofocus;
28 : final bool multiLine;
29 : final bool number;
30 : final Key? key;
31 : final Key? testKey;
32 :
33 1 : @override
34 1 : _CwtchTextFieldState createState() => _CwtchTextFieldState();
35 : }
36 :
37 : class _CwtchTextFieldState extends State<CwtchTextField> {
38 : late final FocusNode _focusNode;
39 : late final ScrollController _scrollController;
40 1 : @override
41 : void initState() {
42 2 : _scrollController = ScrollController();
43 :
44 2 : _focusNode = FocusNode();
45 3 : _focusNode.addListener(() {
46 : // Select all...
47 10 : if (_focusNode.hasFocus) widget.controller.selection = TextSelection(baseOffset: 0, extentOffset: widget.controller.text.length);
48 : });
49 1 : super.initState();
50 : }
51 :
52 1 : @override
53 : Widget build(BuildContext context) {
54 1 : return Consumer<Settings>(
55 1 : builder: (context, theme, child) {
56 1 : return Container(
57 : clipBehavior: Clip.antiAlias,
58 1 : decoration: BoxDecoration(),
59 : // Horrifying Hack: Flutter doesn't give us direct control over system menus but instead picks BG color from TextButtonThemeData ¯\_(ツ)_/¯
60 1 : child: Theme(
61 2 : data: Theme.of(context).copyWith(
62 6 : textButtonTheme: TextButtonThemeData(style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Provider.of<Settings>(context).theme.menuBackgroundColor))),
63 : ),
64 1 : child: TextFormField(
65 2 : key: widget.testKey,
66 2 : controller: widget.controller,
67 2 : validator: widget.validator,
68 2 : onChanged: widget.onChanged,
69 2 : autofocus: widget.autofocus,
70 : autovalidateMode: AutovalidateMode.onUserInteraction,
71 2 : textAlign: widget.number ? TextAlign.end : TextAlign.start,
72 2 : keyboardType: widget.multiLine
73 : ? TextInputType.multiline
74 2 : : widget.number
75 : ? TextInputType.number
76 : : TextInputType.text,
77 2 : inputFormatters: widget.number ? <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly] : null,
78 2 : maxLines: widget.multiLine ? null : 1,
79 1 : scrollController: _scrollController,
80 : enableIMEPersonalizedLearning: false,
81 1 : focusNode: _focusNode,
82 4 : style: Provider.of<Settings>(context).scaleFonts(defaultTextStyle).copyWith(overflow: TextOverflow.clip),
83 1 : decoration: InputDecoration(
84 : errorMaxLines: 2,
85 2 : hintText: widget.hintText,
86 4 : hintStyle: TextStyle(color: (theme.current().mainTextColor as Color).withOpacity(0.5)),
87 : floatingLabelBehavior: FloatingLabelBehavior.never,
88 : filled: true,
89 1 : focusedBorder: OutlineInputBorder(
90 1 : borderRadius: BorderRadius.circular(6.0),
91 3 : borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0),
92 : ),
93 1 : focusedErrorBorder: OutlineInputBorder(
94 1 : borderRadius: BorderRadius.circular(6.0),
95 3 : borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0),
96 : ),
97 1 : errorBorder: OutlineInputBorder(
98 1 : borderRadius: BorderRadius.circular(6.0),
99 3 : borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0),
100 : ),
101 3 : errorStyle: TextStyle(color: theme.current().textfieldErrorColor, fontWeight: FontWeight.bold, overflow: TextOverflow.visible),
102 2 : fillColor: theme.current().textfieldBackgroundColor,
103 1 : contentPadding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
104 1 : enabledBorder: OutlineInputBorder(
105 1 : borderRadius: BorderRadius.circular(6.0),
106 3 : borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0),
107 : ),
108 : ),
109 : ),
110 : ),
111 : );
112 : },
113 : );
114 : }
115 : }
|