-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_page.dart
113 lines (104 loc) · 3.83 KB
/
test_page.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
import 'package:cascade_picker/cascade_picker.dart';
import 'package:flutter/material.dart';
class TestPage extends StatefulWidget {
const TestPage({Key? key}) : super(key: key);
@override
_TestPageState createState() => _TestPageState();
}
class _TestPageState extends State<TestPage> {
final _cascadeController = CascadeController();
late List<Item> items;
@override
void initState() {
super.initState();
items = [];
for (int i = 0; i < 5; i++) {
Item item0 = Item();
item0.name = "name_$i";
item0.code = "code_$i";
List<Item> children1 = [];
for (int j = 0; j < 3; j++) {
Item item1 = Item();
item1.name = "name_${i}_$j";
item1.code = "code_${i}_$j";
List<Item> children2 = [];
for (int k = 0; k < 7; k++) {
Item item2 = Item();
item2.name = "name_${i}_${j}_$k";
item2.code = "code_${i}_${j}_$k";
// 第3页没有子数据列表
item2.children = [];
children2.add(item2);
}
// 第2页的子数据列表
item1.children = children2;
children1.add(item1);
}
// 第1页的子数据列表
item0.children = children1;
items.add(item0);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
SizedBox(height: 50,),
TextButton(
onPressed: () {
if (_cascadeController.isCompleted()) {
// 已选中的titles
List<String> selectedTitles = _cascadeController.selectedTitles;
print("已选中的titles: $selectedTitles");
// 已选中的序号
List<int> selectedIndexes = _cascadeController.selectedIndexes;
print("已选中的序号:$selectedIndexes");
Item item = items[selectedIndexes[0]].children![selectedIndexes[1]].children![selectedIndexes[2]];
print("已选择item( ${item.name} )");
}
},
child: Text("选定")
),
SizedBox(height: 10,),
Container(
height: 400,
child: CascadePicker(
initialPageData: items.map((e) => e.name!).toList(),
nextPageData: (pageCallback, currentPage, selectIndex) async {
print("当前选择: 第$currentPage页, 第$selectIndex项");
if (currentPage == 1) {
// 在第一页选中,返回第二页列表数据
List<String>? nextPageData = items[selectIndex]
.children?.map((e) => e.name!).toList();
if (nextPageData != null) pageCallback(nextPageData);
} else if (currentPage == 2) {
// 在第二页选中,返回第二页列表数据
// 先获取已选中的序号
List<int> selectedIndexes = _cascadeController.selectedIndexes;
// 根据已选中的序号在items中获取下一级页面的列表数据
List<String>? nextPageData = items[selectedIndexes[0]]
.children?[selectIndex]
.children?.map((e) => e.name!).toList();
if (nextPageData != null) pageCallback(nextPageData);
}
},
controller: _cascadeController,
maxPageNum: 3,
// selectedIcon: Image.asset("images/ic_select_mark.png", width: 10, height: 10, color: Colors.redAccent,),
),
),
],
),
),
);
}
}
class Item {
String? name;
String? code;
String? fatherCode;
String? remark;
List<Item>? children;
}