Skip to content

Commit ebd911a

Browse files
authored
Merge pull request #218 from letsar/feature/aligned_grid
Adding AlignedGrid
2 parents c2d61c1 + 1e7eddf commit ebd911a

27 files changed

+1465
-164
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.6.0
2+
### Added
3+
* SliverAlignedGrid and AlignedGridView widgets.
4+
### Changed
5+
* Renamed SliverMasonryGridDelegate to SliverSimpleGridDelegate.
6+
17
## 0.5.1
28
### Added
39
* StaggeredTile.fit constructor.

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ GridView.custom(
210210
);
211211
```
212212

213-
214213
### **Staired**
215214
![Staired Grid Layout][staired_preview]
216215

@@ -253,6 +252,41 @@ GridView.custom(
253252
);
254253
```
255254

255+
### **Aligned**
256+
![Aligned Grid Layout][aligned_preview]
257+
258+
This layout is also called CSS Grid. This is a common grid layout on the web, where each item within a track has the maximum cross axis extent of its siblings.
259+
260+
#### **Grid properties**
261+
- Evenly divided in *n* columns
262+
- The rows can have differents heights
263+
264+
#### **Tile properties**
265+
- Must occupy 1 column only
266+
- Each tile has the same height as the tallest one of the row.
267+
268+
#### **Placement algorithm**
269+
- Top-most and then left-most
270+
271+
#### **Example**
272+
Below you'll find the code to create this grid layout:
273+
274+
![Aligned example][aligned_example]
275+
276+
```dart
277+
AlignedGridView.count(
278+
crossAxisCount: 4,
279+
mainAxisSpacing: 4,
280+
crossAxisSpacing: 4,
281+
itemBuilder: (context, index) {
282+
return Tile(
283+
index: index,
284+
extent: (index % 7 + 1) * 30,
285+
);
286+
},
287+
);
288+
```
289+
256290
## Sponsoring
257291

258292
I'm working on my packages on my free-time, but I don't have as much time as I would. If this package or any other package I created is helping you, please consider to sponsor me so that I can take time to read the issues, fix bugs, merge pull requests and add features to these packages.
@@ -292,8 +326,10 @@ If you fixed a bug or implemented a feature, please send a [pull request][pr].
292326
[quilted_preview]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/quilted.png
293327
[woven_preview]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/woven.png
294328
[staired_preview]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/staired.png
329+
[aligned_preview]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/aligned.png
295330
[staggered_example]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/staggered_example.png
296331
[masonry_example]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/masonry_example.png
297332
[quilted_example]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/quilted_example.png
298333
[woven_example]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/woven_example.png
299-
[staired_example]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/staired_example.png
334+
[staired_example]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/staired_example.png
335+
[aligned_example]: https://raw.githubusercontent.com/letsar/flutter_staggered_grid_view/master/docs/images/aligned_example.png

docs/images/aligned.png

767 Bytes
Loading

docs/images/aligned_example.png

30.1 KB
Loading

examples/assets/aligned.png

767 Bytes
Loading

examples/lib/common.dart

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:flutter/material.dart';
22

3+
const _defaultColor = Color(0xFF34568B);
4+
35
class AppScaffold extends StatelessWidget {
46
const AppScaffold({
57
Key? key,
@@ -31,17 +33,19 @@ class Tile extends StatelessWidget {
3133
Key? key,
3234
required this.index,
3335
this.extent,
36+
this.backgroundColor,
3437
this.bottomSpace,
3538
}) : super(key: key);
3639

3740
final int index;
3841
final double? extent;
3942
final double? bottomSpace;
43+
final Color? backgroundColor;
4044

4145
@override
4246
Widget build(BuildContext context) {
4347
final child = Container(
44-
color: const Color(0xFF34568B),
48+
color: backgroundColor ?? _defaultColor,
4549
height: extent,
4650
child: Center(
4751
child: CircleAvatar(
@@ -92,3 +96,44 @@ class ImageTile extends StatelessWidget {
9296
);
9397
}
9498
}
99+
100+
class InteractiveTile extends StatefulWidget {
101+
const InteractiveTile({
102+
Key? key,
103+
required this.index,
104+
this.extent,
105+
this.bottomSpace,
106+
}) : super(key: key);
107+
108+
final int index;
109+
final double? extent;
110+
final double? bottomSpace;
111+
112+
@override
113+
_InteractiveTileState createState() => _InteractiveTileState();
114+
}
115+
116+
class _InteractiveTileState extends State<InteractiveTile> {
117+
Color color = _defaultColor;
118+
119+
@override
120+
Widget build(BuildContext context) {
121+
return GestureDetector(
122+
onTap: () {
123+
setState(() {
124+
if (color == _defaultColor) {
125+
color = Colors.red;
126+
} else {
127+
color = _defaultColor;
128+
}
129+
});
130+
},
131+
child: Tile(
132+
index: widget.index,
133+
extent: widget.extent,
134+
backgroundColor: color,
135+
bottomSpace: widget.bottomSpace,
136+
),
137+
);
138+
}
139+
}

examples/lib/examples/aligned.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'package:examples/common.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
4+
5+
class AlignedPage extends StatelessWidget {
6+
const AlignedPage({
7+
Key? key,
8+
}) : super(key: key);
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
return AppScaffold(
13+
title: 'Aligned',
14+
child: AlignedGridView.count(
15+
crossAxisCount: 4,
16+
mainAxisSpacing: 4,
17+
crossAxisSpacing: 4,
18+
itemBuilder: (context, index) {
19+
return Tile(
20+
index: index,
21+
extent: (index % 7 + 1) * 30,
22+
);
23+
},
24+
),
25+
);
26+
}
27+
}

examples/lib/main.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:examples/common.dart';
2+
import 'package:examples/pages/aligned.dart';
23
import 'package:examples/pages/masonry.dart';
34
import 'package:examples/pages/quilted.dart';
45
import 'package:examples/pages/staggered.dart';
@@ -66,6 +67,11 @@ class HomePage extends StatelessWidget {
6667
imageName: 'staired',
6768
destination: StairedPage(),
6869
),
70+
MenuEntry(
71+
title: 'Aligned',
72+
imageName: 'aligned',
73+
destination: AlignedPage(),
74+
),
6975
],
7076
),
7177
);

examples/lib/main_examples.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:examples/common.dart';
2+
import 'package:examples/examples/aligned.dart';
23
import 'package:examples/examples/masonry.dart';
34
import 'package:examples/examples/quilted.dart';
45
import 'package:examples/examples/staggered.dart';
@@ -66,6 +67,11 @@ class HomePage extends StatelessWidget {
6667
imageName: 'staired',
6768
destination: StairedPage(),
6869
),
70+
MenuEntry(
71+
title: 'Aligned',
72+
imageName: 'aligned',
73+
destination: AlignedPage(),
74+
),
6975
],
7076
),
7177
);

examples/lib/pages/aligned.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'dart:math';
2+
3+
import 'package:examples/common.dart';
4+
import 'package:flutter/material.dart';
5+
import 'package:flutter/widgets.dart';
6+
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
7+
8+
class AlignedPage extends StatefulWidget {
9+
const AlignedPage({
10+
Key? key,
11+
}) : super(key: key);
12+
13+
@override
14+
State<AlignedPage> createState() => _AlignedPageState();
15+
}
16+
17+
class _AlignedPageState extends State<AlignedPage> {
18+
final rnd = Random();
19+
late List<int> extents;
20+
int crossAxisCount = 4;
21+
22+
@override
23+
void initState() {
24+
super.initState();
25+
extents = List<int>.generate(10000, (int index) => rnd.nextInt(7) + 1);
26+
}
27+
28+
@override
29+
Widget build(BuildContext context) {
30+
return AppScaffold(
31+
title: 'Aligned',
32+
child: AlignedGridView.count(
33+
crossAxisCount: crossAxisCount,
34+
mainAxisSpacing: 4,
35+
crossAxisSpacing: 4,
36+
itemBuilder: (context, index) {
37+
final height = extents[index] * 40;
38+
return ImageTile(
39+
index: index,
40+
width: 100,
41+
height: height,
42+
);
43+
},
44+
itemCount: extents.length,
45+
),
46+
);
47+
}
48+
}

0 commit comments

Comments
 (0)