gpt4 book ai didi

flutter - SuffixIcon 不适用于 AutoCompleteTextField

转载 作者:行者123 更新时间:2023-12-05 08:05:27 26 4
gpt4 key购买 nike

只要 textField 中有一些文本,我希望箭头图标出现,否则它应该消失。如果我使用 TextField 而不是 AutoCompleteTextField,它工作正常。另外,如果我删除 suffixIcon 处的条件,即使那样它也能正常工作。请参阅下面的代码。


class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
bool arrow = false;
var controller = TextEditingController();

@override
void initState() {
super.initState();
controller.addListener(() {
print(controller.text);
setState(() {
controller.text == '' ? arrow = false : arrow = true;
});
});
}

Widget suffixIcon() {
var iconButton = IconButton(
icon: Icon(Icons.arrow_forward),
onPressed: () {},
color: Theme.of(context).primaryColor);

return arrow ? iconButton : null;
// return iconButton;
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AutoCompleteTextField(
controller: controller,
decoration: InputDecoration(
suffixIcon: suffixIcon(),
),
itemSorter: (a, b) {},
suggestions: [],
itemSubmitted: (data) {},
itemFilter: (suggestion, String query) {},
itemBuilder: (BuildContext context, suggestion) {},
key: null,
),
],
),
),
);
}
}

最佳答案

suffixIcon: Controller.text.length > 0 ? InkWell(
child: Icon(Icons.arrow_forward, color: Colors.grey,),
onTap: (){
setState(() {
Controller.clear();
});
},
) : SizedBox()

关于flutter - SuffixIcon 不适用于 AutoCompleteTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65171117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com