I'm trying to clear my TextField, here is what i do
IconButton(
onPressed: (){
FocusScope.of(context).requestFocus(new FocusNode());
_searchFieldController.clear();
setState(() {
searchClicked = false;
});
},
icon: Icon(Icons.close),
);
}
when i run it i get this error
I/flutter ( 4547): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter ( 4547): The following assertion was thrown while handling a gesture: I/flutter ( 4547): invalid text selection: TextSelection(baseOffset: 6, extentOffset: 6, affinity: I/flutter ( 4547): TextAffinity.upstream, isDirectional: false)
and here is my the widget
TextField(
onChanged: (text) {
if(text.length >= 4){
searchResult = productTemp.where((i) => i.productName.contains(text.toString())).toList();
_productController.sink.add(searchResult);
}else{
_productController.sink.add(productTemp);
}
},
focusNode: _focus,
controller: _searchFieldController,
style: TextStyle(fontSize: 15),
decoration: InputDecoration(
filled: true,
hintStyle:new TextStyle(color: Colors.grey[800],fontSize: 20),
fillColor: Colors.white70,
hintText: "Search",
suffixIcon: searchClicked ? buildCancelIcon() : buildSearchIcon()
),
)
Material design allows a text field to indicate an error via a small red label under the input box: https://material.io/components/text-fields (see screenshot below).
To validate the user input, we need the Form widget. The Form widget serves as a container to validate multiple TextFormFields and needs a GlobalKey to uniquely identify the form. It is also used to get the current state of the form.
To remove TextField underline/border in Flutter, you can simply set the border property to InputBorder. none. This will remove the underline for all states such as Focused, Enabled, Error, Disabled.
https://github.com/flutter/flutter/issues/17647
WidgetsBinding.instance.addPostFrameCallback((_) => _searchFieldController.clear());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With