Flutter学习第二课-基础组件 DropdownButton

/*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(),
    );
  }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值