Line data Source code
1 : import 'package:flutter/material.dart';
2 : import 'package:cwtch/settings.dart';
3 : import 'package:provider/provider.dart';
4 :
5 : // Provides a styled Text Field for use in Form Widgets.
6 : // Callers must provide a text controller, label helper text and a validator.
7 : class CwtchButtonTextField extends StatefulWidget {
8 1 : CwtchButtonTextField({
9 : required this.controller,
10 : required this.onPressed,
11 : required this.icon,
12 : required this.tooltip,
13 : this.readonly = true,
14 : this.labelText,
15 : this.testKey,
16 : this.onChanged,
17 : this.textStyle,
18 : });
19 : final TextEditingController controller;
20 : final Function()? onPressed;
21 : final Function(String)? onChanged;
22 : final Icon? icon;
23 : final String tooltip;
24 : final bool readonly;
25 : final Key? testKey;
26 : final TextStyle? textStyle;
27 : String? labelText;
28 :
29 1 : @override
30 1 : _CwtchButtonTextFieldState createState() => _CwtchButtonTextFieldState();
31 : }
32 :
33 : class _CwtchButtonTextFieldState extends State<CwtchButtonTextField> {
34 : late final FocusNode _focusNode;
35 :
36 1 : @override
37 : void initState() {
38 2 : _focusNode = FocusNode();
39 2 : _focusNode.addListener(() {
40 : // Select all...
41 0 : if (_focusNode.hasFocus) widget.controller.selection = TextSelection(baseOffset: 0, extentOffset: widget.controller.text.length);
42 : });
43 1 : super.initState();
44 : }
45 :
46 1 : @override
47 : Widget build(BuildContext context) {
48 1 : return Consumer<Settings>(
49 1 : builder: (context, theme, child) {
50 1 : return Container(
51 : clipBehavior: Clip.antiAlias,
52 1 : decoration: BoxDecoration(),
53 : // Horrifying Hack: Flutter doesn't give us direct control over system menus but instead picks BG color from TextButtonThemeData ¯\_(ツ)_/¯
54 1 : child: Theme(
55 2 : data: Theme.of(context).copyWith(
56 6 : textButtonTheme: TextButtonThemeData(style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Provider.of<Settings>(context).theme.menuBackgroundColor))),
57 : ),
58 1 : child: TextFormField(
59 2 : key: widget.testKey,
60 2 : controller: widget.controller,
61 2 : readOnly: widget.readonly,
62 2 : showCursor: !widget.readonly,
63 1 : focusNode: _focusNode,
64 : enableIMEPersonalizedLearning: false,
65 2 : onChanged: widget.onChanged,
66 : maxLines: 1,
67 3 : style: widget.textStyle == null ? TextStyle(overflow: TextOverflow.clip) : widget.textStyle,
68 1 : decoration: InputDecoration(
69 2 : labelText: widget.labelText,
70 5 : labelStyle: TextStyle(color: theme.current().mainTextColor, backgroundColor: theme.current().textfieldBackgroundColor),
71 2 : suffixIcon: widget.icon == null
72 0 : ? SizedBox.shrink()
73 1 : : IconButton(
74 2 : onPressed: widget.onPressed,
75 2 : icon: widget.icon!,
76 1 : splashRadius: Material.defaultSplashRadius / 2,
77 1 : padding: EdgeInsets.fromLTRB(0.0, 4.0, 2.0, 2.0),
78 2 : tooltip: widget.tooltip,
79 : enableFeedback: true,
80 2 : color: theme.current().mainTextColor,
81 2 : highlightColor: theme.current().defaultButtonColor,
82 2 : focusColor: theme.current().defaultButtonActiveColor,
83 2 : splashColor: theme.current().defaultButtonActiveColor,
84 : ),
85 : floatingLabelBehavior: FloatingLabelBehavior.never,
86 : filled: true,
87 2 : fillColor: theme.current().textfieldBackgroundColor,
88 1 : focusedBorder: OutlineInputBorder(
89 1 : borderRadius: BorderRadius.circular(6.0),
90 3 : borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0),
91 : ),
92 1 : focusedErrorBorder: OutlineInputBorder(
93 1 : borderRadius: BorderRadius.circular(6.0),
94 3 : borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0),
95 : ),
96 1 : errorBorder: OutlineInputBorder(
97 1 : borderRadius: BorderRadius.circular(6.0),
98 3 : borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0),
99 : ),
100 3 : errorStyle: TextStyle(color: theme.current().textfieldErrorColor, fontWeight: FontWeight.bold),
101 1 : contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
102 1 : enabledBorder: OutlineInputBorder(
103 1 : borderRadius: BorderRadius.circular(6.0),
104 3 : borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0),
105 : ),
106 : ),
107 : ),
108 : ),
109 : );
110 : },
111 : );
112 : }
113 : }
|