Flutter DropdownMenuItem


简介

DropdownButton 创建的一个菜单项

  • DropdownButton 是 Material 设计风格中的一个从列表中选择某一个item的按钮
  • DropdownButton 按钮显示选定的Item的值以及打开用于选择其他item的菜单箭头
  • DropdownMenuItem<T> 这里面的T代表入参的类型,注意在给定菜单中,所有的item的类型要保持一致

基本用法

配合 DropdownButton 使用

  • value 选中返回的值
  • child 子Widget项

实例演示

import 'package:flutter/material.dart';

class DropdownMenuItemDemo extends StatefulWidget {
  _DropdownMenuItemDemoState createState() => _DropdownMenuItemDemoState();
}

class _DropdownMenuItemDemoState extends State<DropdownMenuItemDemo> {

    String dropdown1Value = 'Free';
  String dropdown2Value;
  String dropdown3Value = 'Four';


  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(24.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          ListTile(
            title: const Text('Simple dropdown:'),
            trailing: DropdownButton<String>(
              value: dropdown1Value,
              onChanged: (String newValue) {
                setState(() {
                  dropdown1Value = newValue;
                });
              },
              items: <String>['One', 'Two', 'Free', 'Four'].map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
            ),
          ),
          const SizedBox(
            height: 24.0,
          ),
          ListTile(
            title: const Text('Dropdown with a hint:'),
            trailing: DropdownButton<String>(
              value: dropdown2Value,
              hint: const Text('Choose'),
              onChanged: (String newValue) {
                setState(() {
                  dropdown2Value = newValue;
                });
              },
              items: <String>['One', 'Two', 'Free', 'Four'].map<DropdownMenuItem<String>>((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
            ),
          ),
          const SizedBox(
            height: 24.0,
          ),
          ListTile(
            title: const Text('Scrollable dropdown:'),
            trailing: DropdownButton<String>(
              value: dropdown3Value,
              onChanged: (String newValue) {
                setState(() {
                  dropdown3Value = newValue;
                });
              },
              items: <String>[
                  'One', 'Two', 'Free', 'Four', 'Can', 'I', 'Have', 'A', 'Little',
                  'Bit', 'More', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten'
                 ]
                .map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                })
                .toList(),
             ),
          ),
        ],
      ),
    );
  }
}