-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzengqiang_main.dart
172 lines (155 loc) · 4.98 KB
/
zengqiang_main.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
167
168
169
170
171
172
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(
debugShowCheckedModeBanner: false,
title: '活动网格',
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 acpplication
// is not restarted.
primaryColor: Colors.red,
primaryColorBrightness: Brightness.dark,
primaryColorDark: Colors.grey,
primaryColorLight: Colors.white,
appBarTheme: AppBarTheme(
color: Colors.blue,
// centerTitle: false,
// shadowColor: Colors.blue,
brightness: Brightness.dark),
),
home: HomePage(),
);
}
}
//实体
class ItemEntity {
String name;
IconData icon;
ItemEntity(this.name, this.icon);
}
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return GridItemPageState();
}
}
class GridItemPageState extends State<HomePage> {
var data = <ItemEntity>[];
//页面高度
var _pageHeight;
//页面数
var pages = -1;
//每行item数量
var rowCount = 5;
//行数
var columnCount = 2;
//item size
var _itemSize;
var isFirst = true;
@override
void initState() {
super.initState();
for (int i = 0; i < 13; i++) {
data.add(ItemEntity('测试:${i + 1}', Icons.android));
}
//计算页数
pages = data.length ~/ (rowCount * columnCount) +
(data.length % (rowCount * columnCount) == 0 ? 0 : 1);
}
List<Container> _generateData() {
//组件list
var list = <Container>[];
//生成每页数据
for (int i = 0; i < pages; i++) {
list.add(
//每页的父布局
Container(
color: Colors.white,
width: double.infinity,
alignment: Alignment.topLeft,
child: Wrap(
direction: Axis.horizontal,
// crossAxisAlignment: WrapCrossAlignment.center,
children: data
.sublist(
rowCount * columnCount * i,
(i + 1) * rowCount * columnCount > data.length
? data.length
: (i + 1) * rowCount * columnCount)
.map((e) => GestureDetector(
onTap: () {
print('点击${e.name}');
},
child: Container(
width: _itemSize,
height: _itemSize,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[Icon(e.icon), Text(e.name)],
)),
))
.toList(),
),
));
}
return list;
}
@override
Widget build(BuildContext context) {
_itemSize = MediaQuery.of(context).size.width / rowCount;
if (isFirst) {
isFirst = false;
if (pages > 1) {
_pageHeight = columnCount * _itemSize;
} else {
//不够一页的item数量
var moreItemCount = data.length % (rowCount * columnCount);
var lineCount =
moreItemCount ~/ rowCount + moreItemCount % rowCount == 0 ? 0 : 1;
_pageHeight = lineCount * _itemSize;
}
}
return Scaffold(
backgroundColor: Colors.green,
appBar: AppBar(
title: Text('自定义Grid分页Item'),
),
body: Container(
width: double.infinity,
height: _pageHeight,
child: PageView(
scrollDirection: Axis.horizontal,
pageSnapping: true,
onPageChanged: (index) {
setState(() {
if ((pages - 1) == index) {
//不够一页的item数量
var moreItemCount = data.length % (rowCount * columnCount);
var lineCount =
moreItemCount ~/ rowCount + moreItemCount % rowCount == 0
? 0
: 1;
_pageHeight = lineCount * _itemSize;
print("if:$_pageHeight");
} else {
_pageHeight = columnCount * _itemSize;
}
});
},
children: _generateData(),
),
),
);
}
}