Skip to content

Commit d010c5f

Browse files
committed
update readme
1 parent 70b9568 commit d010c5f

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

README.md

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ insult him.
5757
13. dimension ratio
5858
14. relative id
5959
15. wrapper constraints
60-
16. grid、list(list is a special grid)
60+
16. staggered grid、grid、list(list is a special staggered grid, grid is also a special staggered
61+
grid)
6162

6263
Coming soon:
6364

@@ -545,6 +546,113 @@ class GridExample extends StatelessWidget {
545546

546547
![grid.webp](https://github.com/hackware1993/flutter-constraintlayout/blob/master/grid.webp?raw=true)
547548

549+
5. staggered grid
550+
551+
```dart
552+
class StaggeredGridExample extends StatelessWidget {
553+
const StaggeredGridExample({Key? key}) : super(key: key);
554+
555+
@override
556+
Widget build(BuildContext context) {
557+
List<Color> colors = [
558+
Colors.redAccent,
559+
Colors.greenAccent,
560+
Colors.blueAccent,
561+
Colors.orangeAccent,
562+
Colors.yellow,
563+
Colors.pink,
564+
Colors.lightBlueAccent
565+
];
566+
const double smallestSize = 40;
567+
const int columnCount = 8;
568+
Random random = Random();
569+
return Scaffold(
570+
body: ConstraintLayout(
571+
children: [
572+
TextButton(
573+
onPressed: () {
574+
(context as Element).markNeedsBuild();
575+
},
576+
child: const Text(
577+
'Upset',
578+
style: TextStyle(
579+
fontSize: 32,
580+
height: 1.5,
581+
),
582+
),
583+
).applyConstraint(
584+
left: ConstraintId('horizontalList').right,
585+
top: ConstraintId('horizontalList').top,
586+
),
587+
...constraintGrid(
588+
id: ConstraintId('horizontalList'),
589+
left: parent.left,
590+
top: parent.top,
591+
margin: const EdgeInsets.only(
592+
left: 100,
593+
),
594+
itemCount: 50,
595+
columnCount: columnCount,
596+
itemBuilder: (index) {
597+
return Container(
598+
color: colors[index % colors.length],
599+
alignment: Alignment.center,
600+
child: Text('$index'),
601+
);
602+
},
603+
itemSizeBuilder: (index) {
604+
if (index == 0) {
605+
return const Size(
606+
smallestSize * columnCount + 35, smallestSize);
607+
}
608+
if (index == 6) {
609+
return const Size(smallestSize * 2 + 5, smallestSize);
610+
}
611+
if (index == 7) {
612+
return const Size(smallestSize * 6 + 25, smallestSize);
613+
}
614+
if (index == 19) {
615+
return const Size(smallestSize * 2 + 5, smallestSize);
616+
}
617+
if (index == 29) {
618+
return const Size(smallestSize * 3 + 10, smallestSize);
619+
}
620+
return Size(
621+
smallestSize, (2 + random.nextInt(4)) * smallestSize);
622+
},
623+
itemSpanBuilder: (index) {
624+
if (index == 0) {
625+
return columnCount;
626+
}
627+
if (index == 6) {
628+
return 2;
629+
}
630+
if (index == 7) {
631+
return 6;
632+
}
633+
if (index == 19) {
634+
return 2;
635+
}
636+
if (index == 29) {
637+
return 3;
638+
}
639+
return 1;
640+
},
641+
itemMarginBuilder: (index) {
642+
return const EdgeInsets.only(
643+
left: 5,
644+
top: 5,
645+
);
646+
})
647+
],
648+
),
649+
);
650+
}
651+
}
652+
```
653+
654+
![staggered_grid.gif](https://github.com/hackware1993/flutter-constraintlayout/blob/master/staggered_grid.gif?raw=true)
655+
548656
# Performance optimization
549657

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

staggered_grid.gif

111 KB
Loading

0 commit comments

Comments
 (0)