/*DropdownButton({
Key key,
@required this.items,菜单列表
this.selectedItemBuilder,//自定义下拉菜单样式
this.value,是否选中
this.hint,
this.disabledHint,
@required this.onChanged,回调
this.elevation = 8,下划线偏离距离
this.style,样式
this.underline,下划线
this.icon, 图标
this.iconDisabledColor,不可用时图标颜色
this.iconEnabledColor,可用时图标颜色
this.iconSize = 24.0,图标大小
this.isDense = false,
this.isExpanded = false,
this.itemHeight = kMinInteractiveDimension, 控件高度,默认kMinInteractiveDimension = 48 设置必须大于48
this.focusColor,
this.focusNode,
this.autofocus = false,
})*/
//如果需要好看,做成圆角按钮样式,你装饰一下包裹一层。
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  String dropdownValue = 'One';
  final List<String> items = <String>['One', 'Two', 'Free', 'Four'];
  @override
  Widget build(BuildContext context) {
    return DropdownButton<String>(
      value: dropdownValue,
      icon: Icon(Icons.arrow_downward),
      iconSize: 24,
      elevation: 8,
      style: TextStyle(fontSize: 10.0,color: Colors.deepPurple),
      iconEnabledColor: Colors.red,
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String newValue) {
        setState(() {
          dropdownValue = newValue;
        });
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );
  }
}

常用方法已经添加了注释,可复制代码直接运行看效果

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐