Skip to content

Commit fa7735d

Browse files
committed
实现自定义IndexBar 的builder方法
1 parent 0e0b252 commit fa7735d

File tree

7 files changed

+310
-67
lines changed

7 files changed

+310
-67
lines changed
2.94 KB
Loading
3.02 KB
Loading
3.04 KB
Loading
2.89 KB
Loading

ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ DEPENDENCIES:
2424
- webview_flutter (from `.symlinks/plugins/webview_flutter/ios`)
2525

2626
SPEC REPOS:
27-
https://github.com/cocoapods/specs.git:
27+
https://github.com/CocoaPods/Specs.git:
2828
- FMDB
2929

3030
EXTERNAL SOURCES:
@@ -52,4 +52,4 @@ SPEC CHECKSUMS:
5252

5353
PODFILE CHECKSUM: 545ae53bd1d0bdac26cae6ddb8a02d47a0916981
5454

55-
COCOAPODS: 1.7.3
55+
COCOAPODS: 1.8.4

lib/components/index_bar/mh_index_bar.dart

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class _SuspensionListViewIndexBarState extends State<MHIndexBar> {
137137
hintOffsetX: widget.hintOffsetX,
138138
tagColor: widget.tagColor,
139139
selectedTagColor: widget.selectedTagColor,
140+
indexBarTagBuilder: widget.indexBarTagBuilder,
141+
indexBarHintBuilder: widget.indexBarHintBuilder,
140142
onTouch: (details) {
141143
if (widget.onTouch != null) {
142144
if (_isTouchDown != details.isTouchDown) {
@@ -201,7 +203,11 @@ class _IndexBar extends StatefulWidget {
201203
/// 标签默认的背景色 默认是 0xFF07C160
202204
final Color selectedTagColor;
203205

204-
///
206+
/// 自定义标签,且 跟标签相关的属性将全部失效
207+
final IndexBarTagBuilder indexBarTagBuilder;
208+
209+
/// 自定义气泡弹出Hint, 且 跟hint相关的属性将全部失效
210+
final IndexBarHintBuilder indexBarHintBuilder;
205211

206212
_IndexBar({
207213
Key key,
@@ -223,6 +229,8 @@ class _IndexBar extends StatefulWidget {
223229
this.hintOffsetX = 20.0,
224230
this.tagColor = Colors.transparent,
225231
this.selectedTagColor = const Color(0xFF07C160),
232+
this.indexBarTagBuilder,
233+
this.indexBarHintBuilder,
226234
}) : assert(onTouch != null),
227235
super(key: key);
228236

@@ -287,8 +295,8 @@ class _IndexBarState extends State<_IndexBar> {
287295
return true;
288296
}
289297

290-
/// 构建索引标签部件
291-
Widget _buildIndexTagWidget(String tag) {
298+
/// 构建索引标签Item部件
299+
Widget _buildIndexTagItemWidget(String tag) {
292300
return new Container(
293301
width: widget.itemWidth.toDouble(),
294302
height: widget.itemHeight.toDouble(),
@@ -297,41 +305,32 @@ class _IndexBarState extends State<_IndexBar> {
297305
// 设置超出部分可见
298306
overflow: Overflow.visible,
299307
children: <Widget>[
300-
Container(
301-
alignment: Alignment.center,
302-
decoration: BoxDecoration(
303-
color: _fetchColor(tag),
304-
borderRadius: BorderRadius.circular(widget.tagWidth * 0.5),
305-
),
306-
child: _buildTagWidget(tag),
307-
width: widget.tagWidth.toDouble(),
308-
height: widget.tagHeight.toDouble(),
309-
),
310-
Positioned(
311-
left: -(60 + widget.hintOffsetX ?? 20),
312-
top: -(50 - widget.itemHeight) * 0.5,
313-
child: Offstage(
314-
offstage: _fetchOffstage(tag),
315-
child: Container(
316-
width: 60.0,
317-
height: 50.0,
318-
decoration: BoxDecoration(
319-
image: DecorationImage(
320-
image: AssetImage(
321-
'assets/images/contacts/ContactIndexShape_60x50.png'),
322-
fit: BoxFit.contain,
323-
),
324-
),
325-
alignment: Alignment(-0.25, 0.0),
326-
child: _buildHintChildWidget(tag),
327-
),
328-
),
329-
),
308+
_buildIndexBarTagWidget(context, tag, _indexModel),
309+
_buildIndexBarHintWidget(context, tag, _indexModel),
330310
],
331311
),
332312
);
333313
}
334314

315+
/// 构建tag
316+
Widget _buildIndexBarTagWidget(
317+
BuildContext context, String tag, IndexBarDetails indexModel) {
318+
if (widget.indexBarTagBuilder != null) {
319+
return widget.indexBarTagBuilder(context, tag, _indexModel);
320+
} else {
321+
return Container(
322+
alignment: Alignment.center,
323+
decoration: BoxDecoration(
324+
color: _fetchColor(tag),
325+
borderRadius: BorderRadius.circular(widget.tagWidth * 0.5),
326+
),
327+
child: _buildTagWidget(tag),
328+
width: widget.tagWidth.toDouble(),
329+
height: widget.tagHeight.toDouble(),
330+
);
331+
}
332+
}
333+
335334
/// 构建某个tag
336335
Widget _buildTagWidget(String tag) {
337336
// 当前选中的tag, 也就是高亮的场景
@@ -397,6 +396,35 @@ class _IndexBarState extends State<_IndexBar> {
397396
}
398397
}
399398

399+
/// 构建tag
400+
Widget _buildIndexBarHintWidget(
401+
BuildContext context, String tag, IndexBarDetails indexModel) {
402+
if (widget.indexBarHintBuilder != null) {
403+
return widget.indexBarHintBuilder(context, tag, indexModel);
404+
} else {
405+
return Positioned(
406+
left: -(60 + widget.hintOffsetX ?? 20),
407+
top: -(50 - widget.itemHeight) * 0.5,
408+
child: Offstage(
409+
offstage: _fetchOffstage(tag),
410+
child: Container(
411+
width: 60.0,
412+
height: 50.0,
413+
decoration: BoxDecoration(
414+
image: DecorationImage(
415+
image: AssetImage(
416+
'assets/images/contacts/ContactIndexShape_60x50.png'),
417+
fit: BoxFit.contain,
418+
),
419+
),
420+
alignment: Alignment(-0.25, 0.0),
421+
child: _buildHintChildWidget(tag),
422+
),
423+
),
424+
);
425+
}
426+
}
427+
400428
/// 构建某个hint中子部件
401429
Widget _buildHintChildWidget(String tag) {
402430
if (widget.mapHintTag != null && widget.mapHintTag[tag] != null) {
@@ -437,7 +465,7 @@ class _IndexBarState extends State<_IndexBar> {
437465

438466
// 配置数据源Widget
439467
widget.data.forEach((tag) {
440-
final tagWidget = _buildIndexTagWidget(tag);
468+
final tagWidget = _buildIndexTagItemWidget(tag);
441469
children.add(tagWidget);
442470
});
443471

0 commit comments

Comments
 (0)