最近朋友想让我实现一个仿苹果相册长按下拉选中的效果,奈何度娘不给力,找不到任何的有关文章,索性就自己来实现吧。希望以后有此需求的朋友能借鉴借鉴自己的文章,这样也不枉自己花些笔墨为大家来介绍实现的思路和方法。
实现效果:(长按下拉选中后续选项)
首先,我们先来实现最基本的效果,单击选中,再单击,取消选中
import 'package:flutter/material.dart';
class AlbumDemo extends StatefulWidget {
const AlbumDemo({Key? key}) : super(key: key);
@override
State<AlbumDemo> createState() => _AlbumDemoState();
}
class _AlbumDemoState extends State<AlbumDemo> {
List selects = [];
@override
void initState() {
super.initState();
for(int i = 0; i < 400 ; i++){
selects.add(0);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('AlbumDemo'),),
body: ListView.builder(itemBuilder: (BuildContext context,int index){
return GestureDetector(
onTap: (){
selects[index] = ((selects[index]??0)==0)?1:0;
setState(() {
});
},
child: Container(
color: Colors.white,
child: Column(
children: [
Container(
height: 80,
child: Row(
children: [
Icon( ((selects[index]??0) ==1)?Icons.check_box:Icons.check_box_outline_blank,),
SizedBox(width: 50,),
Text('$index'),
],
)
),
Container(
height: 0.5,
color: Colors.lightBlue,
)
],
),
)
);
},itemCount: 400,),
);
}
}
接下来利用手势来判别滑动轨迹,并且为listview添加controller控制滑动
import 'package:flutter/material.dart';
enum PullDirection {
down,
up,
}
class AlbumDemo extends StatefulWidget {
const AlbumDemo({Key? key}) : super(key: key);
@override
State<AlbumDemo> createState() => _AlbumDemoState();
}
class _AlbumDemoState extends State<AlbumDemo> {
List selects = [];//选中的暂存数组
ScrollController _controller = ScrollController();
double origin_offset = 0;//原始的标记位置,用来判别是上拉还是下滑
bool endDrap = false;//结束滑动的标志
bool isPullingUp = false;//是否正在上滑中
bool isPullingDown = false;//是否正在下滑中
bool isAnimationing = false;//是否处于自动滚动中
final GlobalKey _globalKey = GlobalKey();//用来获取当前的listview
Offset saveOffset = Offset(0, 0);
PullDirection direction = PullDirection.down;//滑动方向
@override
void initState() {
super.initState();
for(int i = 0; i < 400 ; i++){
selects.add(0);
}
}
runloopScroll(bool down){
print("isPullingDown = $isPullingDown");
if(down){
origin_offset+=80;
}else{
origin_offset-=80;
if(origin_offset<0){
origin_offset = 0;
}
}
Future.delayed(Duration(milliseconds: 200),(){
if(down){
_controller.animateTo(origin_offset, duration: Duration(milliseconds: 200),curve:Curves.linear );
}else{
_controller.animateTo(origin_offset, duration: Duration(milliseconds: 200),curve:Curves.linear );
}
}).then((value){
RenderBox box = _globalKey.currentContext?.findRenderObject() as RenderBox;
var innerLocation = box.globalToLocal(saveOffset);
updateSelectionStatus(Offset(innerLocation.dx, innerLocation.dy+ (down?80:-80)),isSelected: direction==PullDirection.down);
if(endDrap == true){
}else{
if(down){//下滑中
if(isPullingDown) {
runloopScroll(true);
}
}else{
if(isPullingUp){
runloopScroll(false);
}
}
}
});
}
updateSelectionStatus(Offset offset,{bool isSelected = true}){
int index = (_controller.offset.toInt() + offset.dy.toInt()) ~/ 80 ;
// print('滑动经过的index=$index');
selects[index] = isSelected?1:0;
setState(() {
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('AlbumDemo'),),
body: GestureDetector(
onLongPress: (){
print("长按开始");
endDrap = false;
origin_offset = _controller.offset;
},
onLongPressEnd: (LongPressEndDetails details){
print("长按结束");
endDrap = true;
isAnimationing = false;
},
onLongPressMoveUpdate:(LongPressMoveUpdateDetails details){
if(saveOffset.dy < details.globalPosition.dy){
//下滑
direction = PullDirection.down;
}else{
//上滑
direction = PullDirection.up;
}
saveOffset = details.globalPosition;
RenderBox box = _globalKey.currentContext?.findRenderObject() as RenderBox;
var innerLocation = box.globalToLocal(saveOffset);
updateSelectionStatus(innerLocation,isSelected: direction==PullDirection.down);
if(details.globalPosition.dy>=810){//下面的边缘临界点,目前写死(这里应该是导航栏与定点的距离)
if(isPullingUp){
isPullingUp = false;
}
isPullingDown = true;
if(isAnimationing){
}else{
runloopScroll(true);
isAnimationing = true;
}
}else if(details.globalPosition.dy<=120){//上面的临界点,这里应该是底部离定点的距离
if(isPullingDown){
isPullingDown = false;
isAnimationing = false;
}
isPullingUp = true;
if(isAnimationing){
}else{
runloopScroll(false);
isAnimationing = true;
}
}else{
if(isPullingDown){
isPullingDown = false;
isAnimationing = false;
}
if(isPullingUp){
isPullingUp = false;
}
}
},
child: ListView.builder(key: _globalKey,controller: _controller,itemBuilder: (BuildContext context,int index){
return GestureDetector(
onTap: (){
selects[index] = ((selects[index]??0)==0)?1:0;
setState(() {
});
},
child: Container(
color: Colors.white,
child: Column(
children: [
Container(
height: 80,
child: Row(
children: [
Icon( ((selects[index]??0) ==1)?Icons.check_box:Icons.check_box_outline_blank,),
SizedBox(width: 50,),
Text('$index'),
],
)
),
Container(
height: 0.5,
color: Colors.lightBlue,
)
],
),
)
);
},itemCount: 400,),
)
);
}
}
大功告成、楼主省略了讲解步骤,请见下回分解 哈哈