-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChecklistView.dart
166 lines (153 loc) · 5.99 KB
/
ChecklistView.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import 'package:checklist/ChecklistListView.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'ChecklistItemView.dart';
export 'ChecklistItemView.dart';
typedef void OnDropChecklist(int oldListIndex, int listIndex, ChecklistViewState state);
typedef void OnTapChecklist(int listIndex, ChecklistViewState state);
typedef void OnStartDragChecklist(
int listIndex, ChecklistViewState state);
class ChecklistView extends StatefulWidget {
Widget title;
List<ChecklistItemView> items;
ChecklistListViewState checklistState;
int index;
bool isOpen;
bool canDrag;
Widget footer;
OnDropChecklist onDropChecklist;
OnTapChecklist onTapChecklist;
OnStartDragChecklist onStartDragChecklist;
Color backgroundColor;
ChecklistView(
{Key key, this.items, this.canDrag,this.footer,this.title, this.checklistState, this.index, this.isOpen, this.onDropChecklist, this.onTapChecklist, this.onStartDragChecklist, this.backgroundColor})
: super(key: key);
@override
State<StatefulWidget> createState() {
return ChecklistViewState();
}
}
class ChecklistViewState extends State<ChecklistView> with AutomaticKeepAliveClientMixin {
double height;
double width;
double headerHeight;
double footerHeight;
List<ChecklistItemViewState> itemStates = List<ChecklistItemViewState>();
void onDropList(int listIndex) {
widget.checklistState.setState(() {
if (widget.onDropChecklist != null) {
widget.onDropChecklist(widget.checklistState.oldListIndex,widget.checklistState.draggedListIndex, this);
}
widget.checklistState.draggedListIndex = null;
});
}
void _startDrag(Widget item, BuildContext context) {
if (widget.checklistState != null) {
widget.checklistState.setState(() {
widget.checklistState.height = context.size.height;
widget.checklistState.draggedListIndex = widget.index;
widget.checklistState.draggedItemIndex = null;
widget.checklistState.draggedItem = item;
widget.checklistState.onDropList = onDropList;
if (widget.onStartDragChecklist != null) {
widget.onStartDragChecklist(widget.index, this);
}
widget.checklistState.oldListIndex = widget.index;
widget.checklistState.run();
});
}
}
@override
bool get wantKeepAlive => true;
void afterFirstLayout(BuildContext context) {
height = context.size.height;
width = context.size.width;
if(_headerKey.currentContext != null) {
headerHeight = _headerKey.currentContext.size.height;
}else{
headerHeight = 0;
}
if(_footerKey.currentContext != null) {
footerHeight = _footerKey.currentContext.size.height;
}else{
footerHeight = 0;
}
}
GlobalKey _headerKey = GlobalKey();
GlobalKey _footerKey = GlobalKey();
@override
Widget build(BuildContext context) {
WidgetsBinding.instance
.addPostFrameCallback((_) => afterFirstLayout(context));
List<Widget> widgets = new List();
Widget header = GestureDetector(
key: _headerKey,
onTapDown: (otd) {
if(widget.checklistState != null) {
RenderBox object = context.findRenderObject();
Offset pos = object.localToGlobal(Offset.zero);
widget.checklistState.initialX = pos.dx;
widget.checklistState.initialY = pos.dy;
widget.checklistState.topListY = pos.dy;
widget.checklistState.bottomListY = pos.dy + object.size.height;
}
},
onTap: (){
if (widget.onTapChecklist != null) {
widget.onTapChecklist(widget.index, this);
}
},
onTapCancel: () {},
onLongPress: () {
if(widget.canDrag == null || widget.canDrag == true) {
_startDrag(widget, context);
}
},
child: Container(
color: (widget.backgroundColor != null)?widget.backgroundColor:Colors.white,
child: Row(
children: <Widget>[Expanded(child: widget.title??Container())],
)));
widgets.add(header);
if(widget.items != null){
for (int i = 0; i < widget.items.length; i++) {
if (widget.items[i].checklist == null || widget.items[i].index != i || widget.items[i].checklist.widget.index != widget.index || widget.items[i].checklist == this) {
widget.items[i] = ChecklistItemView(
onTapItem: widget.items[i].onTapItem,
onStartDragItem: widget.items[i].onStartDragItem,
onDropItem: widget.items[i].onDropItem,
onChanged: widget.items[i].onChanged,
canDrag: widget.items[i].canDrag,
index: i,
title: widget.items[i].title,
value: widget.items[i].value,
backgroundColor: widget.items[i].backgroundColor,
checklist: this,
);
}
if (widget.checklistState.draggedItemIndex == i &&
widget.checklistState.draggedListIndex == widget.index) {
widgets.add(Opacity(
opacity: 0.0,
child: widget.items[i],
));
} else {
widgets.add(AnimatedCrossFade(duration: Duration(milliseconds: 140),crossFadeState: (widget.isOpen == null || widget.isOpen)?CrossFadeState.showFirst:CrossFadeState.showSecond,firstChild: widget.items[i], secondChild: Container(),));
}
}
}
if(widget.checklistState != null && widget.index != null) {
if (widget.checklistState.checklistStates.length > widget.index) {
widget.checklistState.checklistStates.removeAt(widget.index);
}
widget.checklistState.checklistStates.insert(widget.index, this);
}
if(widget.footer != null){
widgets.add(AnimatedCrossFade(duration: Duration(milliseconds: 140),crossFadeState: (widget.isOpen == null || widget.isOpen)?CrossFadeState.showFirst:CrossFadeState.showSecond,firstChild:Material(key: _footerKey,color:widget.backgroundColor,child: widget.footer), secondChild: Container(),));
}
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: widgets??[],
);
}
}