Skip to content

Commit

Permalink
update init name
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiJingLong committed Jan 27, 2019
1 parent 5f2bd03 commit 2c85658
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 50 deletions.
111 changes: 61 additions & 50 deletions lib/src/city_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import 'package:flutter/cupertino.dart';

import 'datas.dart';

Future<CityResult> showCityPicker(BuildContext context) async {
Future<CityResult> showCityPicker(
BuildContext context, {
CityResult initCity,
}) async {
Completer<CityResult> completer = Completer();
var cityData = await loadCityData();

var result = showDialog(
context: context,
builder: (c) => CityPicker(
params: cityData,
initResult: initCity,
),
);

Expand All @@ -26,12 +30,11 @@ Future<CityResult> showCityPicker(BuildContext context) async {

class CityPicker extends StatefulWidget {
final Map<String, dynamic> params;
final ValueSetter<CityResult> forResult;

final CityResult initResult;
const CityPicker({
Key key,
this.params,
this.forResult,
this.initResult,
}) : super(key: key);

@override
Expand Down Expand Up @@ -61,14 +64,21 @@ class _CityPickerState extends State<CityPicker> {
@override
void initState() {
super.initState();
provinceScrollController = FixedExtentScrollController();
cityScrollController = FixedExtentScrollController();
countyScrollController = FixedExtentScrollController();

var initResult = widget.initResult;
List<int> initItems =
findIndexs(initResult?.province, initResult?.city, initResult?.county);

provinceScrollController =
FixedExtentScrollController(initialItem: initItems[0]);
cityScrollController =
FixedExtentScrollController(initialItem: initItems[1]);
countyScrollController =
FixedExtentScrollController(initialItem: initItems[2]);

cityResult.province = proviceNameByIndex(0);
cityResult.city = cityList[0]["name"];
cityResult.county = countyList[0]["name"];
widget.forResult?.call(cityResult);
}

@override
Expand All @@ -95,15 +105,7 @@ class _CityPickerState extends State<CityPicker> {
child: Column(
children: <Widget>[
_buildButtons(),
Expanded(
child: Row(
children: <Widget>[
Expanded(child: buildProvincePicker()),
Expanded(child: buildCityPicker()),
Expanded(child: buildCountyPicker()),
],
),
),
_buildPickers(),
],
),
),
Expand All @@ -113,6 +115,48 @@ class _CityPickerState extends State<CityPicker> {
);
}

Widget _buildButtons() {
Widget buildButton(String text, Function onTap) {
return CupertinoButton(
child: Text(text),
onPressed: onTap,
);
}

return Container(
color: Colors.white,
height: 40.0,
child: Row(
children: <Widget>[
buildButton("取消", () {
Navigator.pop(context);
}),
Expanded(
child: Container(),
),
buildButton("确定", () {
cityResult.province = proviceNameByIndex(provinceIndex);
cityResult.city = cityList[cityIndex]["name"];
cityResult.county = countyList[countyIndex]["name"];
Navigator.pop(context, cityResult);
}),
],
),
);
}

Widget _buildPickers() {
return Expanded(
child: Row(
children: <Widget>[
Expanded(child: buildProvincePicker()),
Expanded(child: buildCityPicker()),
Expanded(child: buildCountyPicker()),
],
),
);
}

Widget buildProvincePicker() {
return CupertinoPicker.builder(
itemExtent: 40,
Expand Down Expand Up @@ -182,7 +226,6 @@ class _CityPickerState extends State<CityPicker> {
cityResult.province = proviceNameByIndex(value);
cityResult.city = cityList[0]["name"];
cityResult.county = countyList[0]["name"];
widget.forResult?.call(cityResult);
cityScrollController.jumpTo(0);
countyScrollController.jumpTo(0);
setState(() {});
Expand All @@ -194,7 +237,6 @@ class _CityPickerState extends State<CityPicker> {
countyIndex = 0;
cityResult.city = cityList[value]["name"];
cityResult.county = countyList[0]["name"];
widget.forResult?.call(cityResult);
countyScrollController.jumpTo(0);
setState(() {});
}
Expand All @@ -203,39 +245,8 @@ class _CityPickerState extends State<CityPicker> {
_log("onCountyChanged value = $value");
countyIndex = value;
cityResult.county = countyList[value]["name"];
widget.forResult?.call(cityResult);
setState(() {});
}

Widget _buildButtons() {
Widget buildButton(String text, Function onTap) {
return CupertinoButton(
child: Text(text),
onPressed: onTap,
);
}

return Container(
color: Colors.white,
height: 40.0,
child: Row(
children: <Widget>[
buildButton("取消", () {
Navigator.pop(context);
}),
Expanded(
child: Container(),
),
buildButton("确定", () {
cityResult.province = proviceNameByIndex(provinceIndex);
cityResult.city = cityList[cityIndex]["name"];
cityResult.county = countyList[countyIndex]["name"];
Navigator.pop(context, cityResult);
}),
],
),
);
}
}

void _log(msg) {
Expand Down
33 changes: 33 additions & 0 deletions lib/src/datas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,36 @@ void releaseCityData() {
_datas.clear();
_isInit = false;
}

List<int> findIndexs(String provinceName, String cityName, String countyName) {
var proIndex = 0;
var cityIndex = 0;
var countyIndex = 0;

if (proIndex == null && cityName == null && countyName == null) {
return [0, 0, 0];
}

List<dynamic> pList = _datas["provinceList"];
out:
for (var item in pList) {
if (item["name"] == provinceName) {
proIndex = pList.indexOf(item);
List<dynamic> cityList = item["cityList"];
for (var city in cityList) {
if (city["name"] == cityName) {
cityIndex = cityList.indexOf(city);
List<dynamic> countyList = city["countyList"];
for (var county in countyList) {
if (county["name"] == countyName) {
countyIndex = countyList.indexOf(county);
break out;
}
}
}
}
}
}

return [proIndex, cityIndex, countyIndex];
}

0 comments on commit 2c85658

Please sign in to comment.