in my case, i have to customize TextField
style.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class ClearableTextField extends StatefulWidget {
const ClearableTextField({
Key? key,
this.prefixText,
this.suffixText,
this.prefix,
this.suffix,
this.inputFormatters,
this.crossAxisAlignment = CrossAxisAlignment.center,
this.hintText,
this.margin,
this.keyboardType,
this.fontSize = 16,
this.height = 1.5,
this.maxLength,
required this.value,
required this.onChanged,
}) : super(key: key);
final String? prefixText;
final String? suffixText;
final Widget? prefix;
final Widget? suffix;
final List<TextInputFormatter>? inputFormatters;
final CrossAxisAlignment crossAxisAlignment;
final String? hintText;
final EdgeInsetsGeometry? margin;
final TextInputType? keyboardType;
final double? fontSize;
final double? height;
final int? maxLength;
final dynamic value;
final ValueChanged<String> onChanged;
@override
State<ClearableTextField> createState() => _ClearableTextFieldState();
}
class _ClearableTextFieldState extends State<ClearableTextField> {
Color _underlineColor = const Color(0xFFE2E5EF);
bool _showClear = false;
late TextEditingController _controller;
final FocusNode _focusNode = FocusNode();
@override
void initState() {
super.initState();
_controller = TextEditingController(text: widget.value);
_controller.addListener(_onTextChanged);
if (_controller.text.isNotEmpty) _showClear = true;
_focusNode.addListener(_handleFocusChange);
}
@override
void didUpdateWidget(ClearableTextField oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.value == widget.value) return;
// _controller.text = widget.value; // this would make the cursor weird
_controller.value = TextEditingValue(
text: widget.value,
selection: TextSelection.collapsed(offset: widget.value.length),
); // this works fine
}
@override
void dispose() {
_controller.dispose();
_focusNode.dispose();
super.dispose();
}
void _onTextChanged() {
setState(() {
_showClear = _controller.text.isNotEmpty;
});
widget.onChanged(_controller.text);
}
void _handleFocusChange() {
if (!_focusNode.hasFocus) {
FocusManager.instance.primaryFocus?.unfocus();
_underlineColor = const Color(0xFFE2E5EF);
} else {
_underlineColor = Theme.of(context).primaryColor;
}
setState(() {});
}
void _clearText() {
widget.onChanged('');
setState(() {
_controller.clear();
_showClear = false;
});
}
@override
Widget build(BuildContext context) {
return Container(
margin: widget.margin,
padding: EdgeInsets.symmetric(vertical: 5.w),
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: _underlineColor)),
),
child: Row(
crossAxisAlignment: widget.crossAxisAlignment,
children: [
if (widget.prefix != null || widget.prefixText != null) ...{
if (widget.prefix != null) widget.prefix!,
if (widget.prefixText != null) SizedBox(width: 8.w),
},
Expanded(
child: TextField(
controller: _controller,
// or use this
// _controller..selection =
// TextSelection.collapsed(offset: widget.value.length),
focusNode: _focusNode,
inputFormatters: widget.inputFormatters,
keyboardType: widget.keyboardType,
maxLength: widget.maxLength,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: widget.fontSize,
height: widget.height,
),
decoration: InputDecoration(
counterText: '',
contentPadding: EdgeInsets.zero,
border: InputBorder.none,
hintText: widget.hintText,
hintStyle: TextStyle(
fontSize: 16.sp,
color: const Color(0xFF9498AB),
fontWeight: FontWeight.w400,
),
),
),
),
if (_showClear)
SizedBox(
width: 32.w,
height: 32.w,
child: IconButton(
iconSize: 18.w,
icon: Image.asset('assets/images/public/clear_input.png'),
onPressed: _clearText,
),
),
if (widget.suffix != null || widget.suffixText != null) ...{
if (widget.suffix != null) widget.suffix!,
if (widget.suffixText != null)
Text(
widget.suffixText!,
style: TextStyle(
fontSize: 24.sp,
fontWeight: FontWeight.w500,
),
),
},
],
),
);
}
}
if you got didUpdateWidget
, just be careful, use _controller.value
instead of _controller.text
, or just remove it.