类似于html中的flex伸缩盒子

    Expanded组件的child还可以是Expanded
    所以可以嵌套使用,从而实现多种布局
    image.png

    1. class ContentView extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return Row(
    5. mainAxisAlignment: MainAxisAlignment.center,
    6. crossAxisAlignment: CrossAxisAlignment.center,
    7. children: [
    8. Expanded(
    9. child: IconContainer(Icons.search,Colors.yellow,color: Colors.red),
    10. flex: 1,
    11. ),
    12. Expanded(
    13. child: Row(
    14. children: [
    15. Expanded(
    16. child: IconContainer(Icons.home,Colors.blue,color:Colors.orange),
    17. flex: 1,
    18. ),
    19. Expanded(
    20. child: IconContainer(Icons.home,Colors.blue,color:Colors.orange),
    21. flex: 1,
    22. )
    23. ],
    24. ),
    25. flex: 2
    26. ),
    27. Expanded(
    28. flex: 3,
    29. child: Row(
    30. children: [
    31. Expanded(
    32. child: IconContainer(Icons.web, Colors.purple),
    33. flex: 1,
    34. ),
    35. Expanded(
    36. child: IconContainer(Icons.web, Colors.purple),
    37. flex: 1,
    38. ),
    39. Expanded(
    40. child: IconContainer(Icons.web, Colors.purple),
    41. flex: 1,
    42. )
    43. ],
    44. )
    45. )
    46. ],
    47. );
    48. }
    49. }

    IconContainer 组件的代码

    1. class IconContainer extends StatelessWidget {
    2. double size;
    3. Color color;
    4. Color bgColor;
    5. IconData icon;
    6. IconContainer(this.icon, this.bgColor,{this.color = Colors.white, this.size = 30});
    7. @override
    8. Widget build(BuildContext context) {
    9. return Container(
    10. height: 100,
    11. width: 100,
    12. color: this.bgColor,
    13. child: Center(
    14. child: Icon(
    15. this.icon,
    16. size: this.size,
    17. color: this.color,
    18. )
    19. ),
    20. );
    21. }
    22. }

    效果图:
    E6E724604240166B9A78E839A948AC46.jpg