【flutter】省市县三级选择

本文介绍了一个在Flutter中实现的省市县三级联动选择器组件的使用方法,包括依赖引入、页面示例代码及自定义项展示。通过该组件,可以轻松地在应用中加入地址选择功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、引入依赖

city_pickers: ^0.1.28   #省市县

2、页面的demo

bool customerItemBuilder = true;//是否自定义类型
void _toAddress(Action action, Context<AddressAddState> ctx) async{
  double customerItemExtent = 40;
  PickerItem themeAttr;
  PickerItem showTypeAttr = PickerItem(name: '省+市+县', value: ShowType.pca);
  Result resultAttr = new Result();
  Result result = new Result();
  double barrierOpacityAttr = 0.5;
  bool barrierDismissibleAttr = false;
  bool customerMeta = false;

  bool customerButtons = true;
  bool isSort = false;
  Result tempResult = await CityPickers.showCityPicker(
      context: ctx.context,
      theme: themeAttr != null ? themeAttr.value : null,
      locationCode: resultAttr != null
          ? resultAttr.areaId ??
          resultAttr.cityId ??
          resultAttr.provinceId
          : null,
      showType: showTypeAttr.value,
      isSort: true,
      barrierOpacity: barrierOpacityAttr,
      barrierDismissible: barrierDismissibleAttr,
      citiesData: null,
      provincesData: null,
      height: 250,
      itemExtent: customerItemExtent,
      cancelWidget: customerButtons ? Text('取消',style: TextStyle(fontSize: 18.0,color: Public().COLOR_THEME),) : null,
      confirmWidget: customerButtons ? Text('确认',style:TextStyle(fontSize: 18.0,color: Public().COLOR_THEME),) : null,
      itemBuilder: getItemBuilder());
  if(tempResult!=null){
    String address = tempResult.provinceName+" "+tempResult.cityName+" "+tempResult.areaName;
    ctx.dispatch(AddressAddActionCreator.onUpdateAddress(address));
  }

}
getItemBuilder() {
  if (customerItemBuilder) {
    return (item, list, index) {
      return Center(
          child: Text(item, maxLines: 1, style: TextStyle(fontSize: 18.0)));
    };
  } else {
    return null;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值