gpt4 book ai didi

flutter - FutureBuilder因Flutter中的TextField而重建

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

问题是当我尝试在文本字段中输入一些文本时,键盘会显示一秒钟,然后整个 FutureBuilder 立即重建。这是我的 FutureBuilder

body: FutureBuilder(
future: Provider.of<Activities>(context, listen: false).fetchAndSet(),
builder: (context, snapshot) => snapshot.connectionState ==
ConnectionState.waiting
? Center(child: CircularProgressIndicator())
: Consumer<Activities>(
child: Center(
child: const Text('nothing here'),
),
builder: (context, activities, ch) => SingleChildScrollView(
child: Center(
child: Column(children: [
Container(
padding:
EdgeInsets.only(top: mediaQuery.size.height / 40),
child: Text(time, style: TextStyle(fontSize: 30)),
),
DayContainer(mediaQuery, activities),
Container(
height: 300,
child: SingleChildScrollView(
physics: ScrollPhysics(),
child: Column(
children: <Widget>[
ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: activities.activities.length,
itemBuilder: (context, index) {
return ElementPicker(
mediaQuery,
// this.callbackN,
activities.activities[index]);
})
],
),
),
),
FloatingActionButton(
onPressed: () => activities.addActivity(
'0Adding0', 'Добавить активность', 0.0, 0.0),
child: Icon(Icons.add),
)
]),
),
),
),
)

在 ElementPicker 内部,还有另一个小部件,其中有 TextField

最佳答案

您需要在 initState 中初始化您的 future,以便每当小部件因任何原因重建(在本例中是打开键盘)时,您都不会执行 fetchAndSet再次:

void initState() {
super.initState();
fetchAndSetFuture = Provider.of<Activities>(context, listen: false).fetchAndSet();
}
...

//in build()

FutureBuilder(
future: fetchAndSetFuture,
builder: ...
);

关于flutter - FutureBuilder因Flutter中的TextField而重建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66530009/

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