Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 增加热门城市的功能 #60

Merged
merged 1 commit into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example/lib/view/wip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ class WorkInProgressState extends State<WorkInProgress> {
locationCode: '110100',
provincesData: !userSelfMeta ? CityPickers.metaProvinces : provincesData,
citiesData: !userSelfMeta ? CityPickers.metaCities : citiesData,
hotCities: [
HotCity(id: 0, name: '123'),
HotCity(id: 1, name: '222'),
HotCity(id: 2, name: '333'),

],
sideBarStyle: BaseStyle(
fontSize: tagBarFontSize,
color: tagFontColor,
Expand Down
1 change: 1 addition & 0 deletions lib/city_pickers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ library city_pickers;

export 'modal/point.dart';
export 'src/cities_selector/cities_style.dart';
export 'src/cities_selector/utils.dart';
export 'modal/result.dart';
export 'src/city_picker.dart';
export 'src/show_types.dart';
36 changes: 32 additions & 4 deletions lib/src/cities_selector/cities_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CitiesSelector extends StatefulWidget {
final String title;
final Map<String, dynamic> provincesData;
final Map<String, dynamic> citiesData;
final List<HotCity> hotCities;

/// 定义右侧bar的激活与普通状态的颜色
final Color tagBarBgColor;
Expand Down Expand Up @@ -69,6 +70,7 @@ class CitiesSelector extends StatefulWidget {
this.title = '城市选择器',
this.locationCode,
this.citiesData,
this.hotCities,
this.provincesData,
this.tagBarActiveColor = Colors.yellow,
this.tagBarFontActiveColor = Colors.red,
Expand Down Expand Up @@ -127,18 +129,23 @@ class _CitiesSelectorState extends State<CitiesSelector> {
/// 用户可定义的, 选项中字体的大小
double itemFontSize;


@override
void initState() {
// TODO: implement initState
print("hotCities::::: ${widget.hotCities}");
_cities = CitiesUtils.getAllCitiesByMeta(
widget.provincesData ?? provincesData, widget.citiesData ?? citiesData);
print("_cities::: $_cities");
_initTargetCity = getInitialCityCode();
// print("_cities>>> ${_cities.length}");
// print("locationCode ${widget.locationCode}");
_tagList = CitiesUtils.getValidTagsByCityList(_cities);

_scrollController = new ScrollController();

// 向tag 与 city 列表中加入 自定义数据
formatHotCities();
_scrollController.addListener(() {
_initOffsetRangList();
// 可以用来强行关闭键盘
Expand All @@ -152,6 +159,25 @@ class _CitiesSelectorState extends State<CitiesSelector> {
itemFontSize = widget.cityItemFontSize;
}

void formatHotCities() {
if (widget.hotCities != null) {
List<Point> hotPoints = [];
List<String> hotTags = [];
widget.hotCities.forEach((HotCity hotCity) {
if (!hotTags.contains(hotCity.tag)) {
hotTags.add(hotCity.tag);
}
hotPoints.add(Point(
code: hotCity.id,
letter:hotCity.tag,
name: hotCity.name,
child: []
));
});
_cities.insertAll(0, hotPoints);
_tagList.insertAll(0, hotTags);
}
}
Point getInitialCityCode() {
if (widget.locationCode == null) {
return null;
Expand All @@ -167,7 +193,9 @@ class _CitiesSelectorState extends State<CitiesSelector> {
if (_offsetTagRangeList.isEmpty) {
double itemContainerHeight =
_key0.currentContext.findRenderObject().paintBounds.size.height;

double offstageHeight = topTagHeight;

_offsetTagRangeList = CitiesUtils.getOffsetRangeByCitiesList(
lists: _cities,
tagHeight: offstageHeight,
Expand Down Expand Up @@ -224,7 +252,7 @@ class _CitiesSelectorState extends State<CitiesSelector> {
}
_changeTimer = new Timer(Duration(milliseconds: 30), () {
CityOffsetRange cityOffsetRange = _offsetTagRangeList
.firstWhere((CityOffsetRange range) => range.tag == alpha);
.firstWhere((CityOffsetRange range) => range.tag == alpha, orElse: null);
if (cityOffsetRange != null) {
_scrollController.jumpTo(cityOffsetRange.start);
}
Expand Down Expand Up @@ -278,7 +306,7 @@ class _CitiesSelectorState extends State<CitiesSelector> {
_tagName = alpha;
});
_initOffsetRangList();
_onTagChange(alpha.toUpperCase());
_onTagChange(alpha);
},
);
}
Expand Down Expand Up @@ -316,7 +344,7 @@ class _CitiesSelectorState extends State<CitiesSelector> {
padding: const EdgeInsets.only(left: 15.0),
color: widget.topIndexBgColor,
child: Text(
_cities[index].letter.toUpperCase(),
_cities[index].letter,
softWrap: true,
style: TextStyle(
fontSize: widget.topIndexFontSize,
Expand Down Expand Up @@ -359,7 +387,7 @@ class _CitiesSelectorState extends State<CitiesSelector> {
padding: const EdgeInsets.only(left: 15.0),
color: widget.topIndexBgColor,
child: Text(
_tagName ?? _tagList.first.toUpperCase(),
_tagName ?? _tagList.first,
softWrap: true,
style: TextStyle(
fontSize: widget.topIndexFontSize,
Expand Down
8 changes: 8 additions & 0 deletions lib/src/cities_selector/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ class CitiesUtils {
return result;
}
}

// 热闹城市对象
class HotCity {
final String name;
final int id;
final String tag;
HotCity({@required this.name, @required this.id, this.tag = "★"});
}
3 changes: 3 additions & 0 deletions lib/src/city_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:city_pickers/src/base/base.dart';
import 'package:city_pickers/src/cities_selector/cities_selector.dart';
import 'package:city_pickers/src/cities_selector/utils.dart';
import 'package:city_pickers/src/full_page/full_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -128,6 +129,7 @@ class CityPickers {
String title = '城市选择器',
Map<String, dynamic> citiesData = meta.citiesData,
Map<String, dynamic> provincesData = meta.provincesData,
List<HotCity> hotCities,
BaseStyle sideBarStyle,
BaseStyle cityItemStyle,
BaseStyle topStickStyle,
Expand Down Expand Up @@ -165,6 +167,7 @@ class CityPickers {
title: title,
provincesData: provincesData,
citiesData: citiesData,
hotCities: hotCities,
locationCode: locationCode,
tagBarActiveColor: _sideBarStyle.backgroundActiveColor,
tagBarFontActiveColor: _sideBarStyle.activeColor,
Expand Down