flutter 省市区选择器 city_pickers 的简单实用

Github地址:https://github.com/hanxu317317/city_pickers

packages地址: https://pub.flutter-io.cn/packages/city_pickers#-readme-tab-

1、将其添加到包的pubspec.yaml文件中,并下载:

city_pickers: ^0.1.18

2、在需要使用的文件导入

import 'package:city_pickers/city_pickers.dart';

3、使用(在这只贴方法)

  Result resultArr = new Result();

  void _clickEventFunc() async{
    Result tempResult = await CityPickers.showCityPicker(
      context: context,
      theme: Theme.of(context).copyWith(primaryColor: Color(0xfffe1314)), // 设置主题
      locationCode: resultArr != null ? resultArr.areaId ?? resultArr.cityId ?? resultArr.provinceId : null, // 初始化地址信息
      cancelWidget: Text(
        '取消',
        style: TextStyle(fontSize: ScreenUtil().setSp(26),color: Color(0xff999999)),
      ),
      confirmWidget: Text(
        '确定',
        style: TextStyle(fontSize: ScreenUtil().setSp(26),color: Color(0xfffe1314)),
      ),
      height: 220.0
    );
    if(tempResult != null){
      setState(() {
        resultArr = tempResult;
      });
    }
  }

因项目和时间的原因,只使用了 showCityPicker 方法,可自行尝试下面两种选择方式。

// type 2
Result result2 = await CityPickers.showFullPageCityPicker(
  context: context,
);
// type 3
Result result2 = await CityPickers.showCitiesSelector(
  context: context,
);

注:

  在 0.1.18 版本之前无法使用自定义确定取消按钮

猜你喜欢

转载自www.cnblogs.com/wyhlightstar/p/11128402.html