flutter_app/lib/widgets/textfield.dart

62 lines
2.7 KiB
Dart
Raw Permalink Normal View History

2021-03-03 00:42:44 +00:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
2021-03-10 17:40:14 +00:00
import '../settings.dart';
2021-03-03 00:42:44 +00:00
2021-03-24 23:35:24 +00:00
doNothing(String x) {}
2021-03-03 00:42:44 +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 CwtchTextField extends StatefulWidget {
CwtchTextField({required this.controller, required this.labelText, this.validator, this.autofocus = false, this.onChanged = doNothing});
2021-03-03 00:42:44 +00:00
final TextEditingController controller;
final String labelText;
2021-05-26 00:02:34 +00:00
final FormFieldValidator? validator;
2021-03-24 23:35:24 +00:00
final Function(String) onChanged;
2021-06-03 18:32:25 +00:00
final bool autofocus;
2021-03-03 00:42:44 +00:00
@override
_CwtchTextFieldState createState() => _CwtchTextFieldState();
}
class _CwtchTextFieldState extends State<CwtchTextField> {
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();
}
2021-03-03 00:42:44 +00:00
@override
Widget build(BuildContext context) {
2021-03-10 17:40:14 +00:00
return Consumer<Settings>(builder: (context, theme, child) {
2021-03-03 01:38:47 +00:00
return TextFormField(
2021-03-10 17:40:14 +00:00
controller: widget.controller,
validator: widget.validator,
2021-03-24 23:35:24 +00:00
onChanged: widget.onChanged,
2021-06-03 18:32:25 +00:00
autofocus: widget.autofocus,
focusNode: _focusNode,
2021-03-10 17:40:14 +00:00
decoration: InputDecoration(
labelText: widget.labelText,
2021-03-16 23:33:03 +00:00
labelStyle: TextStyle(color: theme.current().mainTextColor(), backgroundColor: theme.current().textfieldBackgroundColor()),
2021-03-10 17:40:14 +00:00
floatingLabelBehavior: FloatingLabelBehavior.never,
filled: true,
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,
),
fillColor: theme.current().textfieldBackgroundColor(),
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
);
2021-03-03 00:42:44 +00:00
});
}
}