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

Migrated package [selectcity] to null-safety #56

Merged
merged 5 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
26 changes: 12 additions & 14 deletions lib/src/components/selectcity/brn_az_common.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// @dart=2.9

import 'package:flutter/material.dart';

/// ISuspension Bean.
abstract class ISuspensionBean {
bool isShowSuspension;
String name;
String tag; //Suspension Tag
bool isShowSuspension = false;
String name = "";
String tag = ""; //Suspension Tag
}

/// AzListView Header.
class AzListViewHeader {
AzListViewHeader({
@required this.height,
@required this.builder,
required this.height,
required this.builder,
this.tag = "↑",
});

Expand All @@ -26,7 +24,7 @@ class AzListViewHeader {
class SuspensionUtil {
/// sort list by suspension tag.
/// 根据[A-Z]排序。
static void sortListBySuspensionTag(List<ISuspensionBean> list) {
static void sortListBySuspensionTag(List<ISuspensionBean>? list) {
if (list == null || list.isEmpty) return;
list.sort((a, b) {
if (a.tag == "@" || b.tag == "#") {
Expand All @@ -41,10 +39,10 @@ class SuspensionUtil {

/// get index data list by suspension tag.
/// 获取索引列表。
static List<String> getTagIndexList(List<ISuspensionBean> list) {
List<String> indexData = List();
static List<String> getTagIndexList(List<ISuspensionBean>? list) {
List<String> indexData = [];
if (list != null && list.isNotEmpty) {
String tempTag;
String? tempTag;
for (int i = 0, length = list.length; i < length; i++) {
String tag = list[i].tag;
if (tag.length > 2) tag = tag.substring(0, 2);
Expand All @@ -58,11 +56,11 @@ class SuspensionUtil {
}

/// set show suspension status.
static void setShowSuspensionStatus(List<ISuspensionBean> list) {
static void setShowSuspensionStatus(List<ISuspensionBean>? list) {
if (list == null || list.isEmpty) return;
String tempTag;
String? tempTag;
for (int i = 0, length = list.length; i < length; i++) {
String tag = list[i].tag;
String? tag = list[i].tag;
if (tempTag != tag) {
tempTag = tag;
list[i].isShowSuspension = true;
Expand Down
55 changes: 26 additions & 29 deletions lib/src/components/selectcity/brn_az_listview.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @dart=2.9

import 'package:bruno/src/components/selectcity/brn_az_common.dart';
import 'package:bruno/src/components/selectcity/brn_index_bar.dart';
import 'package:bruno/src/components/selectcity/brn_suspension_view.dart';
Expand All @@ -20,7 +18,7 @@ typedef Widget IndexHintBuilder(BuildContext context, String hint);

/// _Header.
class _Header extends ISuspensionBean {
String tag;
String tag = "";

String getSuspensionTag() => tag;

Expand All @@ -31,15 +29,15 @@ class _Header extends ISuspensionBean {
/// AzListView.
class AzListView extends StatefulWidget {
AzListView(
{Key key,
{Key? key,
this.data,
this.topData,
this.itemBuilder,
required this.itemBuilder,
this.controller,
this.physics,
this.shrinkWrap = true,
this.padding = EdgeInsets.zero,
this.suspensionWidget,
required this.suspensionWidget,
this.isUseRealIndex = true,
this.itemHeight = 50,
this.suspensionHeight = 40,
Expand All @@ -48,20 +46,19 @@ class AzListView extends StatefulWidget {
this.indexBarBuilder,
this.indexHintBuilder,
this.showIndexHint = true})
: assert(itemBuilder != null),
super(key: key);
: super(key: key);

///with ISuspensionBean Data
final List<ISuspensionBean> data;
final List<ISuspensionBean>? data;

///with ISuspensionBean topData, Do not participate in [A-Z] sorting (such as hotList).
final List<ISuspensionBean> topData;
final List<ISuspensionBean>? topData;

final ItemWidgetBuilder itemBuilder;

final ScrollController controller;
final ScrollController? controller;

final ScrollPhysics physics;
final ScrollPhysics? physics;

final bool shrinkWrap;

Expand All @@ -80,13 +77,13 @@ class AzListView extends StatefulWidget {
final int suspensionHeight;

///on sus tag change callback.
final ValueChanged<String> onSusTagChanged;
final ValueChanged<String>? onSusTagChanged;

final AzListViewHeader header;
final AzListViewHeader? header;

final IndexBarBuilder indexBarBuilder;
final IndexBarBuilder? indexBarBuilder;

final IndexHintBuilder indexHintBuilder;
final IndexHintBuilder? indexHintBuilder;

final bool showIndexHint;

Expand All @@ -100,12 +97,12 @@ class _AzListViewState extends State<AzListView> {
//右侧索引tag 与 距离offset的value,比如 a---0, b---item*个数,c---a+b
Map<String, int> _suspensionSectionMap = Map();

List<ISuspensionBean> _cityList = List();
List<String> _indexTagList = List();
List<ISuspensionBean> _cityList = [];
List<String> _indexTagList = [];
bool _isShowIndexBarHint = false;
String _indexBarHint = "";

ScrollController _scrollController;
late ScrollController _scrollController;

@override
void initState() {
Expand All @@ -115,15 +112,15 @@ class _AzListViewState extends State<AzListView> {

@override
void dispose() {
_scrollController?.dispose();
_scrollController.dispose();
super.dispose();
}

void _onIndexBarTouch(IndexBarDetails model) {
setState(() {
_indexBarHint = model.tag;
_isShowIndexBarHint = model.isTouchDown;
int offset = _suspensionSectionMap[model.tag];
int? offset = _suspensionSectionMap[model.tag];
if (offset != null) {
_scrollController.jumpTo(offset
.toDouble()
Expand All @@ -134,10 +131,10 @@ class _AzListViewState extends State<AzListView> {

void _init() {
_cityList.clear();
if (widget.topData != null && widget.topData.isNotEmpty) {
_cityList.addAll(widget.topData);
if (widget.topData != null && widget.topData!.isNotEmpty) {
_cityList.addAll(widget.topData!);
}
List<ISuspensionBean> list = widget.data;
List<ISuspensionBean>? list = widget.data;
if (list != null && list.isNotEmpty) {
// SuspensionUtil.sortListBySuspensionTag(list);
_cityList.addAll(list);
Expand All @@ -146,7 +143,7 @@ class _AzListViewState extends State<AzListView> {
SuspensionUtil.setShowSuspensionStatus(_cityList);

if (widget.header != null) {
_cityList.insert(0, _Header()..tag = widget.header.tag);
_cityList.insert(0, _Header()..tag = widget.header!.tag);
}
_indexTagList.clear();
if (widget.isUseRealIndex) {
Expand All @@ -171,8 +168,8 @@ class _AzListViewState extends State<AzListView> {
itemBuilder: (BuildContext context, int index) {
if (index == 0 && _cityList[index] is _Header) {
return SizedBox(
height: widget.header.height.toDouble(),
child: widget.header.builder(context));
height: widget.header!.height.toDouble(),
child: widget.header!.builder(context));
}
return widget.itemBuilder(context, _cityList[index]);
}),
Expand All @@ -195,7 +192,7 @@ class _AzListViewState extends State<AzListView> {
onTouch: _onIndexBarTouch,
);
} else {
indexBar = widget.indexBarBuilder(
indexBar = widget.indexBarBuilder!(
context,
_indexTagList,
_onIndexBarTouch,
Expand All @@ -207,7 +204,7 @@ class _AzListViewState extends State<AzListView> {
));
Widget indexHint;
if (widget.indexHintBuilder != null) {
indexHint = widget.indexHintBuilder(context, '$_indexBarHint');
indexHint = widget.indexHintBuilder!(context, '$_indexBarHint');
} else {
indexHint = Card(
color: Colors.black54,
Expand Down
34 changes: 18 additions & 16 deletions lib/src/components/selectcity/brn_base_azlistview_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @dart=2.9

import 'dart:async';

import 'package:bruno/src/components/empty/brn_empty_status.dart';
Expand Down Expand Up @@ -27,10 +25,10 @@ abstract class BaseAZListViewPage extends StatefulWidget {
Widget buildItemWidget(ISuspensionBean item);

//悬浮的widget
Widget buildSuspensionWidget(String tag);
Widget buildSuspensionWidget(String? tag);

List<ISuspensionBean> getTopData() {
return List<ISuspensionBean>();
return <ISuspensionBean>[];
}

//item的高度 默认50
Expand All @@ -41,16 +39,19 @@ abstract class BaseAZListViewPage extends StatefulWidget {

//每个modal 对应的 tag,默认是拼音来设置
String createTagByModal(ISuspensionBean bean) {
String pinyin = PinyinHelper.getPinyinE(bean.name);
return pinyin.substring(0, 1).toUpperCase();
if (bean.name.isNotEmpty) {
String pinyin = PinyinHelper.getPinyinE(bean.name);
return pinyin.substring(0, 1).toUpperCase();
}
return "";
}
}

class _BaseAZListViewPageState extends State<BaseAZListViewPage> {
String suspensionTag = "";
String? suspensionTag = "";

List<ISuspensionBean> _dataList = List();
StreamController<String> streamController;
List<ISuspensionBean> _dataList = [];
late StreamController<String> streamController;

@override
void initState() {
Expand All @@ -72,14 +73,15 @@ class _BaseAZListViewPageState extends State<BaseAZListViewPage> {
if (snapShot.connectionState == ConnectionState.done) {
if (snapShot.hasError) {
return BrnAbnormalStateUtils.getEmptyWidgetByState(
context, AbnormalState.networkConnectError,action: (index) {
context, AbnormalState.networkConnectError,
action: (index) {
setState(() {});
});
} else {
return buildContentBody(snapShot.data);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark 一下

return null;
return Container();
},
));
}
Expand All @@ -98,7 +100,7 @@ class _BaseAZListViewPageState extends State<BaseAZListViewPage> {

if (_dataList.isEmpty && top.isEmpty) {
return BrnAbnormalStateUtils.getEmptyWidgetByState(
context, AbnormalState.noData,);
context, AbnormalState.noData);
}

suspensionTag = top.isEmpty ? _dataList[0].tag : top[0].tag;
Expand All @@ -109,7 +111,7 @@ class _BaseAZListViewPageState extends State<BaseAZListViewPage> {
child: StreamBuilder(
initialData: suspensionTag,
stream: streamController.stream,
builder: (context, snapShot) {
builder: (context, AsyncSnapshot snapShot) {
return AzListView(
data: _dataList,
topData: top,
Expand All @@ -128,14 +130,14 @@ class _BaseAZListViewPageState extends State<BaseAZListViewPage> {
);
}

Widget _buildSusWidget(String susTag) {
Widget _buildSusWidget(String? susTag) {
return Container(
height: widget.getSuspensionHeight(),
child: widget.buildSuspensionWidget(susTag),
);
}

void _handleList(List<ISuspensionBean> list) {
void _handleList(List<ISuspensionBean>? list) {
if (list == null || list.isEmpty) return;
for (int i = 0, length = list.length; i < length; i++) {
String tag = widget.createTagByModal(list[i]);
Expand All @@ -154,7 +156,7 @@ class _BaseAZListViewPageState extends State<BaseAZListViewPage> {
}

Widget _buildListItem(ISuspensionBean model) {
String susTag = model.tag;
String? susTag = model.tag;
return Column(
children: <Widget>[
//当offstage为true,当前控件不会被绘制在屏幕上
Expand Down
Loading