I m trying to build a search field with a clear button who appear when i type some text in. But i tried a ternary condition to display my button, it never appear.
预 收
这里是我的班子:
class SearchField extends StatefulWidget {
@override
_SearchFieldState createState() => _SearchFieldState();
}
class _SearchFieldState extends State<SearchField> {
TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return TextField(
controller: _controller,
style: const TextStyle(
color: Colors.white,
),
decoration: InputDecoration(
labelText: Search ,
labelStyle: TextStyle(
color: Color.fromARGB(255, 255, 255, 255).withOpacity(0.4),
fontFamily: Inter ,
),
border: InputBorder.none,
suffixIcon: _controller.text.isEmpty //my clear button
? null
: IconButton(
icon: Icon(Icons.clear),
onPressed: () {
setState(() {
_controller.clear();
});
},
)
),
onEditingComplete: () {
// TODO: search contact with _controller.text
FocusScope.of(context).unfocus();
},
);
}
}