chatting/lib/contact_sider_list_page.dart

107 lines
3.0 KiB
Dart

import 'package:chatting/contact_data_page.dart';
import 'package:flutter/material.dart';
//好友列表渲染
//好友 具体实 类需要 个构造器及 表数
class ContactSiderListPage extends StatefulWidget {
//好友列表项数据
final List<ContactDataPage> items;
//好友列表头构造器
final IndexedWidgetBuilder headerBuilder;
//好友列表项构造器
final IndexedWidgetBuilder itemBuilder;
//字母构造器
final IndexedWidgetBuilder sectionBuilder;
//构造方法
const ContactSiderListPage(
{Key key,
@required this.items,
this.headerBuilder,
@required this.itemBuilder,
@required this.sectionBuilder})
: super(key: key);
@override
_ContactSiderListPageState createState() => _ContactSiderListPageState();
}
class _ContactSiderListPageState extends State<ContactSiderListPage> {
//列表滚动控制器
final ScrollController _scrollController = new ScrollController();
bool _onNotification(ScrollNotification notification) {
return true;
}
//判断并显示头部视图,或者容器
_isShowHeaderView(index) {
if (index == 0 && widget.headerBuilder != null) {
return Offstage(
offstage: false,
child: widget.headerBuilder(context, index),
);
}
return Container();
}
//根据定位判断是否显示好友列表头
bool _shouldShowHeader(int position) {
if (position <= 0) {
return false;
}
if (position != 0 &&
widget.items[position].seationKey !=
widget.items[position - 1].seationKey) {
return false;
}
return true;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
//列表加载更多
NotificationListener(
onNotification: _onNotification,
child: ListView.builder(
//滚动控制器
controller: _scrollController,
//列表里的内容不足一屏幕时也可以滑动
physics: const AlwaysScrollableScrollPhysics(),
//列表长度
itemCount: widget.items.length,
itemBuilder: (BuildContext context, int index) {
//列表项容器
return Container(
alignment: Alignment.centerLeft,
child: Column(
children: <Widget>[
//显示列表列头
_isShowHeaderView(index),
//用offstage 组件控制是否显示英文字母
Offstage(
offstage: _shouldShowHeader(index),
child: widget.sectionBuilder(context, index),
),
//显示列表项
Column(
children: <Widget>[widget.itemBuilder(context, index)],
)
],
),
);
},
),
)
],
),
);
}
}