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

Flutter中DropdownButton选中值不显示问题求助

Flutter DropdownButton选中值无法即时更新问题

我正在学习Flutter,已为DropdownButton组件加载了选项,但按照示例及官方文档实现后,选中DropdownButton中的选项时,组件无法即时显示选中值,仅关闭当前打开的弹窗(ListView所在的弹窗)后再次打开,才能看到值已变更。

示例数据

List<Map<String, dynamic>> tipos_producto = [
  {
    "ID_TIPO_PRODUCTO": 1,
    "NOMBRE_TIPO_PRODUCTO": "Gaseosas",
    "DESCRIPCION_TIPO_PRODUCTO": "Bebidas gaseosas",
    "ID_TIPO_UNIDAD": 2,
  },
  {
    "ID_TIPO_PRODUCTO": 2,
    "NOMBRE_TIPO_PRODUCTO": "Aseo del hogar",
    "DESCRIPCION_TIPO_PRODUCTO": "Todo tipo de productos de aseo.",
    "ID_TIPO_UNIDAD": 1,
  },
  {
    "ID_TIPO_PRODUCTO": 4,
    "NOMBRE_TIPO_PRODUCTO": "Snacks",
    "DESCRIPCION_TIPO_PRODUCTO": "Paquetes de papas, entre otros.",
    "ID_TIPO_UNIDAD": 1,
  },
  // 更多数据...
];

组件代码

import 'package:flutter/material.dart';
import 'package:proyecto_minimarket_apk/tipos_producto.dart';

class opciones extends StatefulWidget {
  const opciones({super.key});

  @override
  State<opciones> createState() => _opcionesState();
}

class _opcionesState extends State<opciones> {
  String ddlVlr_tipoProducto = tipos_producto.elementAt(0)["ID_TIPO_PRODUCTO"].toString();
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          DropdownButton(
            hint: const Text("Tipo de producto"),
            icon: const Icon(Icons.arrow_drop_down),
            items: tipos_producto.map((map) {
              return DropdownMenuItem<String>(
                value: map["ID_TIPO_PRODUCTO"].toString(),
                child: Text(map["NOMBRE_TIPO_PRODUCTO"]),
              );
            }).toList(),
            value: ddlVlr_tipoProducto,
            onChanged: (String? value) {
              setState(() {
                ddlVlr_tipoProducto = value!;
              });
            },
          ),
          // 其他组件...
        ],
      ),
    );
  }
}

复现步骤

  • 运行项目后,选择"Productos"选项
  • 点击屏幕右下角的"New"按钮(ElevatedButton)
  • 操作前两个DropdownButton字段,选择任意值
  • 例如第一个控件默认选中"Gaseosas",选择第二个选项"Aseo del Hogar",选中后无法即时显示变更,关闭当前弹窗后再次点击"New"按钮,才能看到选中值变为"Aseo del Hogar"

调试控制台无报错,查阅官方文档后仍无法解决,希望能指明问题所在。

内容的提问来源于stack exchange,提问作者Mauricio Arias Olave

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.09 19:13:09