Skip to content

Commit b001406

Browse files
committed
support grid
1 parent 790bcf0 commit b001406

File tree

6 files changed

+146
-1
lines changed

6 files changed

+146
-1
lines changed

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ insult him.
5757
13. dimension ratio
5858
14. relative id
5959
15. wrapper constraints
60+
16. grid
6061

6162
Coming soon:
6263

@@ -495,6 +496,54 @@ class BadgeExample extends StatelessWidget {
495496

496497
![badge.webp](https://github.com/hackware1993/flutter-constraintlayout/blob/master/badge.webp?raw=true)
497498

499+
4. grid
500+
501+
```dart
502+
class GridExample extends StatelessWidget {
503+
const GridExample({Key? key}) : super(key: key);
504+
505+
@override
506+
Widget build(BuildContext context) {
507+
List<Color> colors = [
508+
Colors.redAccent,
509+
Colors.greenAccent,
510+
Colors.blueAccent,
511+
Colors.orangeAccent,
512+
Colors.yellow,
513+
Colors.pink,
514+
Colors.lightBlueAccent
515+
];
516+
return Scaffold(
517+
body: ConstraintLayout(
518+
children: [
519+
...constraintGrid(
520+
id: ConstraintId('grid'),
521+
left: parent.left,
522+
top: parent.top,
523+
itemCount: 50,
524+
columnCount: 8,
525+
itemWidth: 50,
526+
itemHeight: 50,
527+
itemBuilder: (index) {
528+
return Container(
529+
color: colors[index % colors.length],
530+
);
531+
},
532+
itemMarginBuilder: (index) {
533+
return const EdgeInsets.only(
534+
left: 10,
535+
top: 10,
536+
);
537+
})
538+
],
539+
),
540+
);
541+
}
542+
}
543+
```
544+
545+
![grid.webp](https://github.com/hackware1993/flutter-constraintlayout/blob/master/grid.webp?raw=true)
546+
498547
# Performance optimization
499548

500549
1. When the layout is complex, if the child elements need to be repainted frequently, it is

example/grid.dart

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_constraintlayout/src/constraint_layout.dart';
3+
4+
import 'custom_app_bar.dart';
5+
6+
class GridExample extends StatelessWidget {
7+
const GridExample({Key? key}) : super(key: key);
8+
9+
@override
10+
Widget build(BuildContext context) {
11+
List<Color> colors = [
12+
Colors.redAccent,
13+
Colors.greenAccent,
14+
Colors.blueAccent,
15+
Colors.orangeAccent,
16+
Colors.yellow,
17+
Colors.pink,
18+
Colors.lightBlueAccent
19+
];
20+
return Scaffold(
21+
appBar: const CustomAppBar(
22+
title: 'Grid',
23+
codePath: 'example/grid.dart',
24+
),
25+
body: ConstraintLayout(
26+
children: [
27+
...constraintGrid(
28+
id: ConstraintId('grid'),
29+
left: parent.left,
30+
top: parent.top,
31+
itemCount: 50,
32+
columnCount: 8,
33+
itemWidth: 100,
34+
itemHeight: 100,
35+
itemBuilder: (index) {
36+
return Container(
37+
color: colors[index % colors.length],
38+
);
39+
},
40+
itemMarginBuilder: (index) {
41+
return const EdgeInsets.only(
42+
left: 10,
43+
top: 10,
44+
);
45+
})
46+
],
47+
),
48+
);
49+
}
50+
}

example/home.dart

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'badge.dart';
44
import 'barrier.dart';
55
import 'complex_list.dart';
66
import 'dimension_ratio.dart';
7+
import 'grid.dart';
78
import 'guideline.dart';
89
import 'percentage_layout.dart';
910
import 'relative_id.dart';
@@ -56,6 +57,9 @@ class ExampleHome extends StatelessWidget {
5657
button('Wrapper Constraints', () {
5758
push(context, const WrapperConstraintsExample());
5859
}),
60+
button('Grid', () {
61+
push(context, const GridExample());
62+
}),
5963
button('Chain (Coming soon)', null),
6064
const Spacer(),
6165
const Text(

grid.webp

4.51 KB
Binary file not shown.

lib/src/constraint_layout.dart

+41
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,47 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
7474
}
7575
}
7676

77+
List<Widget> constraintGrid({
78+
required ConstraintId id,
79+
required _Align left,
80+
required _Align top,
81+
required int itemCount,
82+
required int columnCount,
83+
required double itemWidth,
84+
required double itemHeight,
85+
required Widget Function(int index) itemBuilder,
86+
EdgeInsets Function(int index)? itemMarginBuilder,
87+
}) {
88+
assert(itemCount > 0);
89+
assert(columnCount > 0);
90+
assert(itemWidth > 0);
91+
assert(itemHeight > 0);
92+
List<Widget> widgets = [];
93+
_Align leftAnchor = left;
94+
_Align topAnchor = top;
95+
for (int i = 0; i < itemCount; i++) {
96+
ConstraintId itemId = ConstraintId(id.id + '_grid_item_$i');
97+
Widget widget = itemBuilder(i);
98+
widgets.add(Constrained(
99+
child: widget,
100+
constraint: Constraint(
101+
id: itemId,
102+
width: itemWidth,
103+
height: itemHeight,
104+
left: leftAnchor,
105+
top: topAnchor,
106+
margin: itemMarginBuilder?.call(i) ?? EdgeInsets.zero,
107+
),
108+
));
109+
leftAnchor = itemId.right;
110+
if (i % columnCount == columnCount - 1) {
111+
leftAnchor = left;
112+
topAnchor = itemId.bottom;
113+
}
114+
}
115+
return widgets;
116+
}
117+
77118
/// Not completed
78119
List<Widget> horizontalChain({
79120
required _Align left,

pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ flutter:
3030
- example/percentage_layout.dart
3131
- example/dimension_ratio.dart
3232
- example/relative_id.dart
33-
- example/wrapper_constraints.dart
33+
- example/wrapper_constraints.dart
34+
- example/grid.dart

0 commit comments

Comments
 (0)