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