image.png

    GridView.builder 实现网格布局

    image.png

    代码

    1. class ContentView extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return GridView.builder(
    5. itemCount: listData.length,
    6. gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
    7. crossAxisCount: 2,
    8. mainAxisSpacing: 10,
    9. crossAxisSpacing: 10,
    10. ),
    11. itemBuilder: _getData,
    12. );
    13. }
    14. Widget _getData(context, index) {
    15. return Container(
    16. child: Column(
    17. children: [
    18. Image.network(listData[index]["imageUrl"]),
    19. SizedBox(
    20. child: Text(
    21. listData[index]["title"],
    22. textAlign: TextAlign.center,
    23. style: const TextStyle(
    24. fontSize: 16,
    25. ),
    26. ),
    27. )
    28. ],
    29. ),
    30. );
    31. }
    32. }

    效果图

    FCA81A184B67CD77B52B748F75FE258D.jpg