You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何DropdownButtonFormField无需setState重建,而DropdownButton却需要?

问题:StatelessWidget中DropdownButton与DropdownButtonFormField的UI更新差异

我尝试探究该问题的原因并查阅了两者的官方文档,但未找到答案。以下是相关示例说明:

List<String> items = ["item1", "item2", "item3", "item4"];
class HomeScreen extends StatelessWidget {
  HomeScreen({super.key});
  String selectedItem = items[0];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: DropdownButton(
              value: selectedItem,
              onChanged: (value) => selectedItem = value!,
              items: items
                  .map(
                    (e) => DropdownMenuItem<String>(
                      value: e,
                      child: Text(e),
                    ),
                  )
                  .toList(),
            ),
...

这是一个居中放置DropdownButton的简单StatelessWidget:运行时选中项变更后,UI不会同步更新显示选中状态。

若仅将组件替换为DropdownButtonFormField,其余代码保持不变,选中项的变更会直接在UI中体现。

内容的提问来源于stack exchange,提问作者Abdelrahman Anwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:55:20