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 tabbar & scroll_anchor to null-safety #66

Merged
merged 25 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d2a37e0
Theme: 优化单例实现,初步优化 BrnAllThemeConfig 属性获取不为 null
zhoujuanjuan Dec 27, 2021
75a2be3
refactor:优化theme,暴露非null引用
zhoujuanjuan Dec 27, 2021
f97bf07
theme:去除无用信息及优化部分代码
zhoujuanjuan Dec 28, 2021
44ba17a
theme:增加属性类型
zhoujuanjuan Dec 28, 2021
cad7451
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Dec 31, 2021
c19203f
优化修复 EventBus
zhoujuanjuan Dec 31, 2021
c96cdd3
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 4, 2022
2c99d35
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 4, 2022
c7ae392
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 5, 2022
6e0834b
迁移example ,优化常量命名,增加export
zhoujuanjuan Jan 4, 2022
5db665d
migrate brn_empty_status.dart
zhoujuanjuan Jan 5, 2022
7c12fc8
删除 BrnThemeImg
zhoujuanjuan Jan 6, 2022
0b75394
Merge remote-tracking branch 'upstream/null-safe' into null-safe
zhoujuanjuan Jan 6, 2022
c00cfd5
fix:tagConfig merge方法增加判空处理
zhoujuanjuan Jan 6, 2022
ada5216
fix:revert tagConfig
zhoujuanjuan Jan 7, 2022
beec378
fix:BrnAbnormalStateWidget空处理
zhoujuanjuan Jan 7, 2022
b852eb6
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 7, 2022
e69a944
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 10, 2022
e31532f
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 11, 2022
162fcfe
Migrated tabbar to null-safety
zhoujuanjuan Jan 11, 2022
2bd473f
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 12, 2022
103ab13
Migrated scroll_anchor to null-safety
zhoujuanjuan Jan 11, 2022
2b141fd
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 13, 2022
04b7a3e
Merge branch 'LianjiaTech:null-safe' into null-safe
zhoujuanjuan Jan 17, 2022
5748da7
fix:修复迁移tabar问题
zhoujuanjuan Jan 17, 2022
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
127 changes: 61 additions & 66 deletions lib/src/components/scroll_anchor/brn_scroll_anchor_tab.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @dart=2.9


import 'dart:async';
import 'dart:ui';
Expand All @@ -20,15 +20,15 @@ class BrnAnchorTab extends StatefulWidget {
final BrnAnchorTabBarStyle tabBarStyle;
final AnchorTabWidgetIndexedBuilder widgetIndexedBuilder;
final AnchorTabIndexedBuilder tabIndexedBuilder;
final Widget tabDivider;
final Widget? tabDivider;

//设置tab与widget的个数
final int itemCount;

BrnAnchorTab(
{@required this.widgetIndexedBuilder,
@required this.tabIndexedBuilder,
@required this.itemCount,
{required this.widgetIndexedBuilder,
required this.tabIndexedBuilder,
required this.itemCount,
this.tabDivider,
this.tabBarStyle = const BrnAnchorTabBarStyle()});

Expand All @@ -40,31 +40,31 @@ class BrnAnchorTab extends StatefulWidget {
class _BrnScrollAnchorTabWidgetState extends State<BrnAnchorTab>
with SingleTickerProviderStateMixin {
//用于控制 滑动
ScrollController scrollController;
late ScrollController _scrollController;

//用于 滑动 和 tab 之间的通信
StreamController<int> streamController;
late StreamController<int?> _streamController;

//用于控制tab
TabController tabController;
late TabController _tabController;

//滑动组件的 key
GlobalKey key;
late GlobalKey _key;

//当前选中的索引
int currentIndex;
int currentIndex = 0;

//滑动组件的元素、
List<Widget> bodyWidgetList;
late List<Widget> _bodyWidgetList;

//滑动组件的元素的key
List<GlobalKey> bodyKeyList;
late List<GlobalKey> _bodyKeyList;

//每个元素在滑动组件中的位置
List<double> cardOffsetList;
late List<double> _cardOffsetList;

//tab
List<BadgeTab> tabList;
late List<BadgeTab> _tabList;

//是否点击滑动
bool tab = false;
Expand All @@ -74,29 +74,28 @@ class _BrnScrollAnchorTabWidgetState extends State<BrnAnchorTab>

@override
void initState() {
streamController = StreamController();
scrollController = ScrollController();
_streamController = StreamController();
_scrollController = ScrollController();

key = GlobalKey();
cardOffsetList = List.filled(widget.itemCount, -1.0);
bodyWidgetList = List();
bodyKeyList = List();
tabList = List();
_key = GlobalKey();
_cardOffsetList = List.filled(widget.itemCount, -1.0);
_bodyWidgetList = [];
_bodyKeyList = [];
_tabList = [];

currentIndex = 0;
tabController = TabController(length: widget.itemCount, vsync: this);
_tabController = TabController(length: widget.itemCount, vsync: this);

fillKeyList();
fillList();
fillTab();

WidgetsBinding.instance.addPostFrameCallback((da) {
WidgetsBinding.instance!.addPostFrameCallback((da) {
fillOffset();
scrollController.addListener(() {
_scrollController.addListener(() {
updateOffset();
currentIndex = createIndex(scrollController.offset);
currentIndex = createIndex(_scrollController.offset);
//防止再次 发送消息
if (!tab) streamController.add(currentIndex);
if (!tab) _streamController.add(currentIndex);
});
});

Expand All @@ -108,11 +107,11 @@ class _BrnScrollAnchorTabWidgetState extends State<BrnAnchorTab>
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
StreamBuilder<int>(
StreamBuilder<int?>(
initialData: currentIndex,
stream: streamController.stream,
stream: _streamController.stream,
builder: (context, snap) {
tabController.index = currentIndex;
_tabController.index = currentIndex;
return BrnTabBar(
indicatorColor: widget.tabBarStyle.indicatorColor,
indicatorWeight: widget.tabBarStyle.indicatorWeight,
Expand All @@ -123,14 +122,14 @@ class _BrnScrollAnchorTabWidgetState extends State<BrnAnchorTab>
unselectedLabelColor: widget.tabBarStyle.unselectedLabelColor,
unselectedLabelStyle: widget.tabBarStyle.unselectedLabelStyle,
dragStartBehavior: widget.tabBarStyle.dragStartBehavior,
controller: tabController,
tabs: tabList,
controller: _tabController,
tabs: _tabList,
onTap: (state, index) {
state.refreshBadgeState(index);
currentIndex = index;
tab = true;
scrollController
.animateTo(cardOffsetList[index],
_scrollController
.animateTo(_cardOffsetList[index],
duration: Duration(milliseconds: 100),
curve: Curves.linear)
.whenComplete(() {
Expand All @@ -149,70 +148,66 @@ class _BrnScrollAnchorTabWidgetState extends State<BrnAnchorTab>
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: bodyWidgetList,
children: _bodyWidgetList,
),
key: key,
controller: scrollController,
key: _key,
controller: _scrollController,
),
)
],
);
}

void fillList() {
if (widget.widgetIndexedBuilder != null) {
for (int i = 0, n = widget.itemCount; i < n; i++) {
bodyWidgetList.add(
Container(
key: bodyKeyList[i],
child: widget.widgetIndexedBuilder(context, i)),
);
}
for (int i = 0, n = widget.itemCount; i < n; i++) {
_bodyWidgetList.add(
Container(
key: _bodyKeyList[i],
child: widget.widgetIndexedBuilder(context, i)),
);
}
}

void fillKeyList() {
for (int i = 0, n = widget.itemCount; i < n; i++) {
bodyKeyList.add(GlobalKey());
_bodyKeyList.add(GlobalKey());
}
}

void fillOffset() {
Offset globalToLocal = (key.currentContext.findRenderObject() as RenderBox)
Offset globalToLocal = (_key.currentContext!.findRenderObject() as RenderBox)
.localToGlobal(Offset.zero);
listDy = globalToLocal.dy;

for (int i = 0, n = widget.itemCount; i < n; i++) {
if (cardOffsetList[i] == -1.0) if (bodyKeyList[i].currentContext !=
if (_cardOffsetList[i] == -1.0) if (_bodyKeyList[i].currentContext !=
null) {
double cardOffset =
(bodyKeyList[i].currentContext.findRenderObject() as RenderBox)
(_bodyKeyList[i].currentContext!.findRenderObject() as RenderBox)
.localToGlobal(Offset.zero) //相对于原点 控件的位置
.dy; //y点坐标

cardOffsetList[i] = cardOffset + scrollController.offset - listDy;
_cardOffsetList[i] = cardOffset + _scrollController.offset - listDy;
}
}
}

void fillTab() {
if (widget.tabIndexedBuilder != null) {
for (int i = 0, n = widget.itemCount; i < n; i++) {
tabList.add(widget.tabIndexedBuilder(context, i));
_tabList.add(widget.tabIndexedBuilder(context, i));
}
}
}

void updateOffset() {
for (int i = 0, n = widget.itemCount; i < n; i++) {
if (cardOffsetList[i] == -1.0) if (bodyKeyList[i].currentContext !=
if (_cardOffsetList[i] == -1.0) if (_bodyKeyList[i].currentContext !=
null) {
double cardOffset =
(bodyKeyList[i].currentContext.findRenderObject() as RenderBox)
(_bodyKeyList[i].currentContext!.findRenderObject() as RenderBox)
.localToGlobal(Offset.zero) //相对于原点 控件的位置
.dy; //y点坐标

cardOffsetList[i] = cardOffset + scrollController.offset - listDy;
_cardOffsetList[i] = cardOffset + _scrollController.offset - listDy;
}
}
}
Expand All @@ -221,7 +216,7 @@ class _BrnScrollAnchorTabWidgetState extends State<BrnAnchorTab>
int createIndex(double offset) {
int index = 0;
for (int i = 0, n = widget.itemCount; i < n; i++) {
if (offset >= cardOffsetList[i] && (offset <= cardOffsetList[i + 1])) {
if (offset >= _cardOffsetList[i] && (offset <= _cardOffsetList[i + 1])) {
return i;
}
}
Expand All @@ -231,28 +226,28 @@ class _BrnScrollAnchorTabWidgetState extends State<BrnAnchorTab>
@override
void dispose() {
super.dispose();
tabController.dispose();
streamController.close();
scrollController.dispose();
_tabController.dispose();
_streamController.close();
_scrollController.dispose();
}
}

class BrnAnchorTabBarStyle {
final Color indicatorColor;
final Color? indicatorColor;

final double indicatorWeight;

final EdgeInsetsGeometry indicatorPadding;

final Color labelColor;
final Color? labelColor;

final Color unselectedLabelColor;
final Color? unselectedLabelColor;

final TextStyle labelStyle;
final TextStyle? labelStyle;

final EdgeInsetsGeometry labelPadding;
final EdgeInsetsGeometry? labelPadding;

final TextStyle unselectedLabelStyle;
final TextStyle? unselectedLabelStyle;

final DragStartBehavior dragStartBehavior;

Expand Down
17 changes: 8 additions & 9 deletions lib/src/components/tabbar/bottom/brn_bottom_tab_bar_item.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @dart=2.9


import 'dart:ui' show Color;

Expand All @@ -9,15 +9,14 @@ import 'package:flutter/material.dart';
/// 特别注意:Tab的右上角小红点可能不符合UI规范,可以使用BrnBadge小红点组件
class BrnBottomTabBarItem {
const BrnBottomTabBarItem({
@required this.icon,
required this.icon,
this.title,
Widget activeIcon,
Widget? activeIcon,
this.backgroundColor,
this.badge,
this.badgeNo,
this.maxBadgeNo = 99,
}) : activeIcon = activeIcon ?? icon,
assert(icon != null);
}) : activeIcon = activeIcon ?? icon;

/// 未选中时的icon
final Widget icon;
Expand All @@ -26,16 +25,16 @@ class BrnBottomTabBarItem {
final Widget activeIcon;

/// Tab标题名
final Widget title;
final Widget? title;

/// 背景色
final Color backgroundColor;
final Color? backgroundColor;

/// 未读信息
final Widget badge;
final Widget? badge;

/// 未读信息个数
final String badgeNo;
final String? badgeNo;

/// 未读消息最大个数
final int maxBadgeNo;
Expand Down
Loading