LCOV - code coverage report
Current view: top level - lib/widgets - buttontextfield.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 44 45 97.8 %
Date: 2024-09-24 21:50:54 Functions: 0 0 -

          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           2 :     return Consumer<Settings>(builder: (context, theme, child) {
      49           1 :       return Container(
      50             :           clipBehavior: Clip.antiAlias,
      51           1 :           decoration: BoxDecoration(),
      52             :           // Horrifying Hack: Flutter doesn't give us direct control over system menus but instead picks BG color from TextButtonThemeData ¯\_(ツ)_/¯
      53           1 :           child: Theme(
      54           2 :               data: Theme.of(context).copyWith(
      55           1 :                 textButtonTheme: TextButtonThemeData(
      56           5 :                   style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Provider.of<Settings>(context).theme.menuBackgroundColor)),
      57             :                 ),
      58             :               ),
      59           1 :               child: TextFormField(
      60           2 :                 key: widget.testKey,
      61           2 :                 controller: widget.controller,
      62           2 :                 readOnly: widget.readonly,
      63           2 :                 showCursor: !widget.readonly,
      64           1 :                 focusNode: _focusNode,
      65             :                 enableIMEPersonalizedLearning: false,
      66           2 :                 onChanged: widget.onChanged,
      67             :                 maxLines: 1,
      68           3 :                 style: widget.textStyle == null ? TextStyle(overflow: TextOverflow.clip) : widget.textStyle,
      69           1 :                 decoration: InputDecoration(
      70           2 :                     labelText: widget.labelText,
      71           5 :                     labelStyle: TextStyle(color: theme.current().mainTextColor, backgroundColor: theme.current().textfieldBackgroundColor),
      72           1 :                     suffixIcon: IconButton(
      73           2 :                       onPressed: widget.onPressed,
      74           2 :                       icon: widget.icon,
      75           1 :                       splashRadius: Material.defaultSplashRadius / 2,
      76           1 :                       padding: EdgeInsets.fromLTRB(0.0, 4.0, 2.0, 2.0),
      77           2 :                       tooltip: widget.tooltip,
      78             :                       enableFeedback: true,
      79           2 :                       color: theme.current().mainTextColor,
      80           2 :                       highlightColor: theme.current().defaultButtonColor,
      81           2 :                       focusColor: theme.current().defaultButtonActiveColor,
      82           2 :                       splashColor: theme.current().defaultButtonActiveColor,
      83             :                     ),
      84             :                     floatingLabelBehavior: FloatingLabelBehavior.never,
      85             :                     filled: true,
      86           2 :                     fillColor: theme.current().textfieldBackgroundColor,
      87           5 :                     focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(6.0), borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0)),
      88           5 :                     focusedErrorBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(6.0), borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0)),
      89           5 :                     errorBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(6.0), borderSide: BorderSide(color: theme.current().textfieldErrorColor, width: 1.0)),
      90           1 :                     errorStyle: TextStyle(
      91           2 :                       color: theme.current().textfieldErrorColor,
      92             :                       fontWeight: FontWeight.bold,
      93             :                     ),
      94           1 :                     contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
      95           5 :                     enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(6.0), borderSide: BorderSide(color: theme.current().textfieldBorderColor, width: 1.0))),
      96             :               )));
      97             :     });
      98             :   }
      99             : }

Generated by: LCOV version 1.14