-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
dynamic_grid.dart
191 lines (180 loc) · 6 KB
/
dynamic_grid.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/widgets.dart';
import 'render_dynamic_grid.dart';
import 'staggered_layout.dart';
import 'wrap_layout.dart';
/// A scrollable, 2D array of widgets.
///
// TODO(all): Add more documentation & sample code
class DynamicGridView extends GridView {
/// Creates a scrollable, 2D array of widgets with a custom
/// [SliverGridDelegate].
DynamicGridView({
super.key,
super.scrollDirection,
super.reverse,
required super.gridDelegate,
// This creates a SliverChildListDelegate in the super class.
super.children = const <Widget>[],
});
/// Creates a scrollable, 2D array of widgets that are created on demand.
DynamicGridView.builder({
super.key,
super.scrollDirection,
super.reverse,
required super.gridDelegate,
// This creates a SliverChildBuilderDelegate in the super class.
required super.itemBuilder,
super.itemCount,
}) : super.builder();
/// Creates a scrollable, 2D array of widgets with tiles where each tile can
/// have its own size.
///
/// Uses a [SliverGridDelegateWithWrapping] as the [gridDelegate].
///
/// The following example shows how to use the DynamicGridView.wrap constructor.
///
/// ```dart
/// DynamicGridView.wrap(
/// mainAxisSpacing: 10,
/// crossAxisSpacing: 20,
/// children: [
/// Container(
/// height: 100,
/// width: 200,
/// color: Colors.amberAccent[100],
/// child: const Center(child: Text('Item 1')
/// ),
/// ),
/// Container(
/// height: 50,
/// width: 70,
/// color: Colors.blue[100],
/// child: const Center(child: Text('Item 2'),
/// ),
/// ),
/// Container(
/// height: 82,
/// width: 300,
/// color: Colors.pink[100],
/// child: const Center(child: Text('Item 3'),
/// ),
/// ),
/// Container(
/// color: Colors.green[100],
/// child: const Center(child: Text('Item 3'),
/// ),
/// ),
/// ],
/// ),
/// ```
///
/// See also:
///
/// * [SliverGridDelegateWithWrapping] to see a more detailed explanation of
/// how the wrapping works.
DynamicGridView.wrap({
super.key,
super.scrollDirection,
super.reverse,
double mainAxisSpacing = 0.0,
double crossAxisSpacing = 0.0,
double childCrossAxisExtent = double.infinity,
double childMainAxisExtent = double.infinity,
super.children = const <Widget>[],
}) : super(
gridDelegate: SliverGridDelegateWithWrapping(
mainAxisSpacing: mainAxisSpacing,
crossAxisSpacing: crossAxisSpacing,
childCrossAxisExtent: childCrossAxisExtent,
childMainAxisExtent: childMainAxisExtent,
),
);
/// Creates a scrollable, 2D array of widgets where each tile's main axis
/// extent will be determined by the child's corresponding finite size and
/// the cross axis extent will be fixed, generating a staggered layout.
///
/// Either a [crossAxisCount] or a [maxCrossAxisExtent] must be provided.
/// The constructor will then use a
/// [DynamicSliverGridDelegateWithFixedCrossAxisCount] or a
/// [DynamicSliverGridDelegateWithMaxCrossAxisExtent] as its [gridDelegate],
/// respectively.
///
/// This sample code shows how to use the constructor with a
/// [maxCrossAxisExtent] and a simple layout:
///
/// ```dart
/// DynamicGridView.staggered(
/// maxCrossAxisExtent: 100,
/// crossAxisSpacing: 2,
/// mainAxisSpacing: 2,
/// children: List.generate(
/// 50,
/// (int index) => Container(
/// height: index % 3 * 50 + 20,
/// color: Colors.amber[index % 9 * 100],
/// child: Center(child: Text("Index $index")),
/// ),
/// ),
/// );
/// ```
DynamicGridView.staggered({
super.key,
super.scrollDirection,
super.reverse,
int? crossAxisCount,
double? maxCrossAxisExtent,
double mainAxisSpacing = 0.0,
double crossAxisSpacing = 0.0,
super.children = const <Widget>[],
}) : assert(crossAxisCount != null || maxCrossAxisExtent != null),
assert(crossAxisCount == null || maxCrossAxisExtent == null),
super(
gridDelegate: crossAxisCount != null
? DynamicSliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
mainAxisSpacing: mainAxisSpacing,
crossAxisSpacing: crossAxisSpacing,
)
: DynamicSliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: maxCrossAxisExtent!,
mainAxisSpacing: mainAxisSpacing,
crossAxisSpacing: crossAxisSpacing,
),
);
@override
Widget buildChildLayout(BuildContext context) {
return DynamicSliverGrid(
delegate: childrenDelegate,
gridDelegate: gridDelegate,
);
}
}
/// A sliver that places multiple box children in a two dimensional arrangement.
class DynamicSliverGrid extends SliverMultiBoxAdaptorWidget {
/// Creates a sliver that places multiple box children in a two dimensional
/// arrangement.
const DynamicSliverGrid({
super.key,
required super.delegate,
required this.gridDelegate,
});
/// The delegate that manages the size and position of the children.
final SliverGridDelegate gridDelegate;
@override
RenderDynamicSliverGrid createRenderObject(BuildContext context) {
final SliverMultiBoxAdaptorElement element =
context as SliverMultiBoxAdaptorElement;
return RenderDynamicSliverGrid(
childManager: element, gridDelegate: gridDelegate);
}
@override
void updateRenderObject(
BuildContext context,
RenderDynamicSliverGrid renderObject,
) {
renderObject.gridDelegate = gridDelegate;
}
}