当前位置:网站首页>flutter 按字母 排序 通讯录并点击滑动
flutter 按字母 排序 通讯录并点击滑动
2022-06-09 09:50:00 【小红花~】
使用插件:scrollable_positioned_list: ^0.2.3 #列表滚动
import 'package:flutter/material.dart';
import "package:collection/collection.dart"; //要引进这个 才可以使用 groups
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
class GroudByCustomer extends StatefulWidget {
const GroudByCustomer({Key? key}) : super(key: key);
@override
State<GroudByCustomer> createState() => _GroudByCustomerState();
}
class _GroudByCustomerState extends State<GroudByCustomer> {
//模拟数据
List list = [
{
"title": "小明",
"sort": "x",
},
{
"title": "花花",
"sort": "h",
},
{
"title": "花花",
"sort": "h",
},
{
"title": "花花",
"sort": "h",
},
{
"title": "花花",
"sort": "h",
},
{
"title": "花花",
"sort": "h",
},
{
"title": "花花",
"sort": "h",
},
{
"title": "花花",
"sort": "h",
},
{
"title": "花花",
"sort": "h",
},
{
"title": "上阳",
"sort": "s",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "小红",
"sort": "x",
},
{
"title": "丽丽",
"sort": "l",
},
{
"title": "a明康",
"sort": "a",
},
{
"title": "小明",
"sort": "x",
},
{
"title": "丽丽",
"sort": "l",
},
{
"title": "丽丽",
"sort": "l",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "a小花",
"sort": "a",
},
{
"title": "上阳",
"sort": "s",
},
{
"title": "上阳",
"sort": "s",
},
{
"title": "上阳",
"sort": "s",
},
{
"title": "小明",
"sort": "x",
},
{
"title": "小明",
"sort": "x",
},
];
//组装好的数据
List arr = [];
//当前被点击都项
int activeIndex = 0;
ItemScrollController itemScrollController = ItemScrollController();
ItemPositionsListener itemPositionsListener = ItemPositionsListener.create();
//滚动控制
ScrollController scrollController = ScrollController();
@override
void initState() {
super.initState();
//对数据进行按字母分组
customerDate();
// 滚动监听
itemPositionsListener.itemPositions.addListener(() {});
}
customerDate() {
// 1.先对数组进行排序
// 2.对数组进行分组
// dart 对 List快速排序 (这是按照升序)
list.sort((left, right) {
return left['sort'].compareTo(right['sort']);
});
//转map类型 (这里需要类型转换 不然使用 groudBy 会报错)
List<Map> groupList = list.map<Map>((e) => e).toList();
// // 进行分组
Map groups = groupBy<Map, String>(groupList, (e) => e["sort"]);
//组成新的数组
groups.forEach((key, value) {
var item = {};
item["title"] = key;
item["children"] = value;
arr.add(item);
});
}
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size.width;
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text("按照字母进行排序----通讯录的格式"),
),
body: Stack(
children: [
// //滚动列表
ScrollablePositionedList.builder(
shrinkWrap: true,
itemCount: arr.length,
itemBuilder: (context, index) => Container(
margin: const EdgeInsets.only(bottom: 20),
child: Column(
children: [
Container(
height: 20,
width: size,
color: Colors.white,
child: Text(arr[index]["title"]),
),
Column(
children: arr[index]["children"]
.asMap()
.entries
.map<Widget>((e) {
// print()
return Container(
height: 50,
width: size,
color: Colors.white,
child: Text("${e.value["title"]}"),
);
}).toList(),
),
],
)),
itemScrollController: itemScrollController,
itemPositionsListener: itemPositionsListener,
),
Positioned(
right: 0,
bottom: 0,
top: 0,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: arr.asMap().entries.map<Widget>((e) {
return InkWell(
onTap: () {
setState(() {
activeIndex = e.key;
});
//跳到哪个index 并以什么动画执行
itemScrollController.scrollTo(
index: e.key,
duration: const Duration(seconds: 1),
curve: Curves.easeIn);
},
child: Container(
width: 20,
margin: const EdgeInsets.only(bottom: 10),
child: Text(
"${e.value["title"]}",
style: TextStyle(
color: activeIndex == e.key
? Colors.green
: Colors.black),
),
),
);
}).toList(),
),
)
],
));
}
}
边栏推荐
- Configuration based permission control
- Go strconv package
- 剑指 Offer 19. 正则表达式匹配-递归
- 978. longest turbulent subarray
- CSRF跨站请求伪造
- 【genius_platform软件平台开发】第一万零一讲:电力项目dz产品windows环境vs2017编译遇到的报错汇总
- golang_3_结构体
- Notes on the development of raspberry pie (15): Raspberry pie 4b+ compile and install MySQL database from the source code
- 从数据库查询权限信息
- Openstack explanation (18) -- Nova service startup and service creation
猜你喜欢

Machine learning notes - explore the keras dataset

1340. jumping game v-dynamic planning plus DFS

Openstack explanation (18) -- Nova service startup and service creation

不加班的测试开发工程师不是好程序员?可能不是一只笨鸟,但一直在先飞......

Mingdao cloud on the list of office software in China's information and innovation industry in 2022
![Cyclic neural network theory -- [torch learning notes]](/img/42/4f9a70b707ad87d1f483129fea6e7e.png)
Cyclic neural network theory -- [torch learning notes]
![[genius_platform software platform development] lesson 36: definition of maximum value macro of built-in data type](/img/f1/ca57934507bb0758b8bb0a52606a10.jpg)
[genius_platform software platform development] lesson 36: definition of maximum value macro of built-in data type

Application of ebpf in cloud native environment

用户目录一站式指南

【genius_platform软件平台开发】第三十七讲:网卡混杂模式和原始套接字
随机推荐
Digital image processing graphic image restoration task
Construction of vocabulary and sampling -- [torch learning notes]
剑指 Offer 18. 删除链表的节点
【genius_platform软件平台开发】第三十六讲:内置数据类型的最大值宏定义
Authentication failure processor
Custom failure handling
31. next spread
IBM announced its withdrawal from Russia after a loss of $300million!
明道云上榜2022年中国信创行业办公软件排行榜
1019. 链表中的下一个更大节点
Webrtc series -- calculate frame rate and frame interval
1019. the next larger node in the linked list
Sequence model - 【 torch learning notes 】
视频融合云服务EasyCVR平台部署在云服务器中的配置关键点
如何管理云计算平台用户?
1108. IP 地址无效化
1324. 竖直打印单词-力扣双百代码
剑指 Offer II 061. 和最小的 k 个数对-大顶堆法
2220. minimum number of bit flips to convert numbers
全球 77.5% 的网站,都在使用“世界上最好的语言” PHP!