image.png

    GridView.count 实现网格布局

    image.png

    实现代码

    1. class ContentView extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return GridView.count(
    5. crossAxisCount: 2, // 控制一行能有多少个组件
    6. crossAxisSpacing: 10,
    7. // padding: EdgeInsets.all(10),
    8. children: _getData(),
    9. );
    10. }
    11. List<Widget> _getData() {
    12. var list = listData.map((value) {
    13. return Container(
    14. decoration: BoxDecoration(
    15. border: Border.all(
    16. color: Colors.black,
    17. )),
    18. child: Column(
    19. children: [
    20. Container(
    21. alignment: Alignment.center,
    22. child: Image.network(value["imageUrl"]),
    23. ),
    24. SizedBox(
    25. child: Text(
    26. value["author"],
    27. textAlign: TextAlign.center,
    28. style: const TextStyle(
    29. // height: 18,
    30. fontSize: 10,
    31. ),
    32. ),
    33. )
    34. ],
    35. ),
    36. );
    37. });
    38. return list.toList();
    39. }
    40. }

    效果图
    FCA81A184B67CD77B52B748F75FE258D.jpg