flutter_app/lib/widgets/buttontextfield.dart

68 lines
3.0 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2021-05-19 21:39:52 +00:00
import 'package:cwtch/settings.dart';
import 'package:provider/provider.dart';
2021-03-16 21:04:14 +00:00
// Provides a styled Text Field for use in Form Widgets.
// Callers must provide a text controller, label helper text and a validator.
class CwtchButtonTextField extends StatefulWidget {
2021-05-25 00:11:39 +00:00
CwtchButtonTextField({required this.controller, required this.onPressed, required this.icon, required this.tooltip, this.readonly = true});
final TextEditingController controller;
2021-05-25 00:11:39 +00:00
final Function()? onPressed;
final Icon icon;
final String tooltip;
final bool readonly;
@override
_CwtchButtonTextFieldState createState() => _CwtchButtonTextFieldState();
}
class _CwtchButtonTextFieldState extends State<CwtchButtonTextField> {
late final FocusNode _focusNode;
@override
void initState() {
_focusNode = FocusNode();
_focusNode.addListener(() {
// Select all...
if (_focusNode.hasFocus) widget.controller.selection = TextSelection(baseOffset: 0, extentOffset: widget.controller.text.length);
});
super.initState();
}
@override
Widget build(BuildContext context) {
2021-03-10 17:40:14 +00:00
return Consumer<Settings>(builder: (context, theme, child) {
return TextFormField(
2021-03-10 17:40:14 +00:00
controller: widget.controller,
readOnly: widget.readonly,
showCursor: !widget.readonly,
focusNode: _focusNode,
2021-03-10 17:40:14 +00:00
decoration: InputDecoration(
suffixIcon: IconButton(
onPressed: widget.onPressed,
icon: widget.icon,
2021-06-09 19:57:22 +00:00
padding: EdgeInsets.fromLTRB(0.0, 4.0, 2.0, 2.0),
2021-03-10 17:40:14 +00:00
tooltip: widget.tooltip,
enableFeedback: true,
color: theme.current().mainTextColor(),
highlightColor: theme.current().defaultButtonColor(),
focusColor: theme.current().defaultButtonActiveColor(),
splashColor: theme.current().defaultButtonActiveColor(),
),
floatingLabelBehavior: FloatingLabelBehavior.never,
filled: true,
fillColor: theme.current().textfieldBackgroundColor(),
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
errorStyle: TextStyle(
color: theme.current().textfieldErrorColor(),
fontWeight: FontWeight.bold,
),
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
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-10 17:40:14 +00:00
);
});
}
}