index_bar 1.0.0 index_bar: ^1.0.0 copied to clipboard
A new Flutter package.A new Flutter package.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: FriendsPage(),
);
}
}
final Color WechatThemeColor = Color.fromRGBO(220, 220, 220, 1.0);
double ScreenWidth(BuildContext context) => MediaQuery.of(context).size.width;
double ScreenHeight(BuildContext context) => MediaQuery.of(context).size.height;
class FriendsPage extends StatefulWidget {
@override
_FriendsPageState createState() => _FriendsPageState();
}
class _FriendsPageState extends State<FriendsPage>
with AutomaticKeepAliveClientMixin {
final Map _groupOffsetMap = {
INDEX_WORDS[0]: 0.0,
INDEX_WORDS[1]: 0.0,
};
ScrollController _scrollController;
final List<Friends> _listDatas = [];
@override
void initState() {
// TODO: implement initState
super.initState();
// _listDatas.addAll(datas);
// _listDatas.addAll(datas);
_listDatas..addAll(datas)..addAll(datas);
_listDatas.sort((Friends a, Friends b) {
return a.indexLetter.compareTo(b.indexLetter);
});
var _groupOffset = 54.5 * 4;
for (int i = 0; i < _listDatas.length; i++) {
if (i < 1) {
_groupOffsetMap.addAll({_listDatas[i].indexLetter: _groupOffset});
_groupOffset += 84.5;
} else if (_listDatas[i].indexLetter == _listDatas[i - 1].indexLetter) {
_groupOffset += 54.5;
} else {
_groupOffsetMap.addAll({_listDatas[i].indexLetter: _groupOffset});
_groupOffset += 84.5;
}
}
_scrollController = ScrollController();
}
final List<Friends> _headerData = [
Friends(
imageUrl:
'https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E5%9B%BE%E6%A0%87&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=©right=&cs=2148695688,3762678660&os=3586054903,2101618507&simid=3362084702,202088874&pn=2&rn=1&di=206140&ln=1922&fr=&fmq=1606395405487_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=http%3A%2F%2Fku.90sjimg.com%2Felement_origin_min_pic%2F00%2F91%2F34%2F3756f166f1d20f6.jpg&rpstart=0&rpnum=0&adpicid=0&force=undefined',
name: '新的朋友'),
Friends(
imageUrl:
'https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E5%9B%BE%E6%A0%87&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=©right=&cs=32822217,3902783693&os=2880966643,978385393&simid=5526804,625381400&pn=3&rn=1&di=96910&ln=1922&fr=&fmq=1606395405487_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=http%3A%2F%2Fimages.669pic.com%2Felement_pic%2F43%2F57%2F83%2F1%2F3ed3001c3e5cdcc467b8d4f0dfe7d8df.jpg%2521w700wb&rpstart=0&rpnum=0&adpicid=0&force=undefined',
name: '群聊'),
Friends(
imageUrl:
'https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E5%9B%BE%E6%A0%87&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=©right=&cs=2013143159,3482378231&os=2555638536,4192324156&simid=4219453221,631365214&pn=13&rn=1&di=15840&ln=1922&fr=&fmq=1606395405487_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=http%3A%2F%2Fpic.51yuansu.com%2Fpic3%2Fcover%2F03%2F80%2F30%2F5c07c4d026ca5_610.jpg&rpstart=0&rpnum=0&adpicid=0&force=undefined',
name: '标签'),
Friends(
imageUrl:
'https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E5%9B%BE%E6%A0%87&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=©right=&cs=1556032908,1806893532&os=574822850,2957051506&simid=3438712011,324232377&pn=51&rn=1&di=131010&ln=1922&fr=&fmq=1606395405487_R&ic=&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01511f57492d286ac72525ae491af2.jpg%40900w_1l_2o_100sh.jpg&rpstart=0&rpnum=0&adpicid=0&force=undefined',
name: '公众号'),
];
Widget _itemForRow(BuildContext context, int index) {
if (index < _headerData.length) {
return _FriendCell(
imageUrl: _headerData[index].imageUrl,
name: _headerData[index].name,
);
}
bool _hideIndexLetter = (index - 4 > 0 &&
_listDatas[index - 4].indexLetter == _listDatas[index - 5].indexLetter);
return _FriendCell(
imageUrl: _listDatas[index - 4].imageUrl,
name: _listDatas[index - 4].name,
groupTitle: _hideIndexLetter ? null : _listDatas[index - 4].indexLetter,
);
}
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
appBar: AppBar(
backgroundColor: WechatThemeColor,
title: Text('通讯录页面'),
actions: <Widget>[
GestureDetector(
child: Container(
margin: EdgeInsets.only(right: 10),
child: Image(
image: AssetImage('images/icon_friends_add.png'),
width: 25,
),
),
// onTap: () {
// Navigator.of(context).push(MaterialPageRoute(
// builder: (BuildContext context) => DiscoverChildPage(
// title: '添加朋友',
// )));
// },
)
],
),
body: Stack(
children: <Widget>[
Container(
color: WechatThemeColor,
child: ListView.builder(
controller: _scrollController,
itemCount: _listDatas.length + _headerData.length,
itemBuilder: _itemForRow,
),
),
IndexBar(
indexBarCallBack: (String str) {
if (_groupOffsetMap[str] != null) {
_scrollController.animateTo(_groupOffsetMap[str],
duration: Duration(milliseconds: 1), curve: Curves.easeIn);
}
},
),
],
),
);
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}
class _FriendCell extends StatelessWidget {
final String imageUrl;
final String name;
final String groupTitle;
final String imageAssets;
const _FriendCell(
{this.imageUrl, this.name, this.groupTitle, this.imageAssets});
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Container(
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(left: 10),
height: groupTitle != null ? 30 : 0,
color: Color.fromRGBO(1, 1, 1, 0.0),
child: groupTitle != null
? Text(
groupTitle,
style: TextStyle(color: Colors.grey),
)
: null,
),
Container(
color: Colors.white,
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.all(10),
width: 34,
height: 34,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6.0),
image: DecorationImage(
image: imageUrl != null
? NetworkImage(imageUrl)
: AssetImage(imageAssets)),
),
),
Container(
child: Text(
name,
style: TextStyle(
fontSize: 17,
),
),
),
],
),
),
Container(
height: 0.5,
color: WechatThemeColor,
child: Row(
children: <Widget>[
Container(
width: 50,
color: Colors.white,
)
],
),
),
],
);
}
}
class Friends {
final String imageUrl;
final String name;
final String indexLetter;
Friends({this.imageUrl, this.name, this.indexLetter});
}
List<Friends> datas = [
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/27.jpg',
name: 'Lina',
indexLetter: 'L'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/17.jpg',
name: '菲儿',
indexLetter: 'F'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/16.jpg',
name: '安莉',
indexLetter: 'A'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/men/31.jpg',
name: '阿贵',
indexLetter: 'A'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/22.jpg',
name: '贝拉',
indexLetter: 'B'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/37.jpg',
name: 'Lina',
indexLetter: 'L'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/18.jpg',
name: 'Nancy',
indexLetter: 'N'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/men/47.jpg',
name: '扣扣',
indexLetter: 'K'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/men/3.jpg',
name: 'Jack',
indexLetter: 'J'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/5.jpg',
name: 'Emma',
indexLetter: 'E'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/24.jpg',
name: 'Abby',
indexLetter: 'A'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/men/15.jpg',
name: 'Betty',
indexLetter: 'B'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/men/13.jpg',
name: 'Tony',
indexLetter: 'T'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/men/26.jpg',
name: 'Jerry',
indexLetter: 'J'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/men/36.jpg',
name: 'Colin',
indexLetter: 'C'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/12.jpg',
name: 'Haha',
indexLetter: 'H'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/11.jpg',
name: 'Ketty',
indexLetter: 'K'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/13.jpg',
name: 'Lina',
indexLetter: 'L'),
Friends(
imageUrl: 'https://randomuser.me/api/portraits/women/23.jpg',
name: 'Lina',
indexLetter: 'L'),
];