import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class SelectBox extends StatefulWidget { const SelectBox({super.key}); @override State createState() => _SelectBoxState(); } class _SelectBoxState extends State { String? _selectedValue = '订单号'; // 存储选中的值 final List _options = ['订单号', '物料号', '物料名',]; // 下拉框选项 @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(horizontal: 0.0), child: PopupMenuButton( onSelected: (String newValue) { setState(() { _selectedValue = newValue; // 更新选中的值 }); }, itemBuilder: (BuildContext context) { return _options.map>((String value) { return PopupMenuItem( value: value, // 下拉框选项的值 child: Text(value, style: TextStyle(color: Colors.black54)), // 下拉框选项的显示 ); }).toList(); }, offset: Offset(-10, 45), // 调整下拉窗口的位置 color: Colors.grey[300], child: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Text(_selectedValue ?? '', style: TextStyle(color: Colors.black)), // 显示当前选中的值 Icon(Icons.arrow_drop_down), // 添加一个下拉箭头图标 ], ), ), ); } }