当前位置:   article > 正文

Flutter之Flex组件布局

Flutter之Flex组件布局

目录

Flex属性值

轴向:direction:Axis.horizontal

主轴方向:mainAxisAlignment:MainAxisAlignment.center

交叉轴方向:crossAxisAlignment:CrossAxisAlignment

主轴尺寸:mainAxisSize

文字方向:textDirection:TextDirection

竖直方向排序:verticalDirection:VerticalDirection

基线对齐方式:textBaseline:TextBaseline

使用方法

第一种写法

第二种写法 


Flex属性

 

轴向:direction:Axis.horizontal

  1. enum Axis {
  2. horizontal,//水平
  3. vertical,//竖直
  4. }

主轴方向:mainAxisAlignment:MainAxisAlignment.center

  1. enum MainAxisAlignment {
  2. start,//顶头
  3. end,//接尾
  4. center,//居中
  5. spaceBetween,//顶头接尾,其他均分
  6. spaceAround,//中间的孩子均分,两头的孩子空一半
  7. spaceEvenly,//均匀平分
  8. }

交叉轴方向:crossAxisAlignment:CrossAxisAlignment

  1. enum CrossAxisAlignment {
  2. start,//顶头
  3. end,//接尾
  4. center,//居中
  5. stretch,//伸展
  6. baseline,//基线
  7. }

主轴尺寸:mainAxisSize

  1. enum MainAxisSize {
  2. min,
  3. max,
  4. }

文字方向:textDirection:TextDirection

  1. enum TextDirection {
  2. ltr,//从左到右
  3. rtl,//从右到左
  4. }

竖直方向排序:verticalDirection:VerticalDirection

  1. enum VerticalDirection{
  2. up,
  3. down,
  4. }

基线对齐方式:textBaseline:TextBaseline

  1. enum TextBaseline {
  2. alphabetic,
  3. ideographic,
  4. }

 

使用方法

第一种写法

  1. Column(
  2. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  3. crossAxisAlignment: CrossAxisAlignment.stretch,
  4. children: [
  5. Flexible(
  6. flex: 1,
  7. child: Container(
  8. color: Colors.red,
  9. width: 100, // 宽度固定
  10. height: double.infinity, // 纵向填满
  11. ),
  12. ),
  13. Flexible(
  14. flex: 2,
  15. child: Container(
  16. color: Colors.blue,
  17. width: 100,
  18. height: double.infinity,
  19. ),
  20. ),
  21. ],
  22. )

第二种写法 

  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_diancan/pages/layout/login_page.dart';
  4. import 'package:flutter_diancan/pages/home/home_page.dart';
  5. import 'package:flutter_diancan/stores/user_store.dart';
  6. import 'package:provider/provider.dart';
  7. class Startpage extends StatelessWidget {
  8. const Startpage({super.key});
  9. @override
  10. Widget build(BuildContext context) {
  11. return Scaffold(
  12. appBar: AppBar(
  13. centerTitle: true,
  14. title: Text("首页"),
  15. ),
  16. body: Center(child: testFlex()));
  17. }
  18. testFlex() {
  19. var redBox = Container(
  20. color: Colors.red,
  21. height: 80,
  22. width: double.infinity,
  23. child: const Text("红"),
  24. );
  25. var blueBox = Container(
  26. color: Colors.blue,
  27. height: 80,
  28. width: double.infinity,
  29. child: const Text("蓝"),
  30. );
  31. var yellowBox = Container(
  32. color: Colors.yellow,
  33. height: 80,
  34. width: double.infinity,
  35. child: const Text("黄"),
  36. );
  37. var greenBox = Container(
  38. color: Colors.green,
  39. height: 80,
  40. width: double.infinity,
  41. child: const Text("绿"),
  42. );
  43. var show = Flex(
  44. direction: Axis.vertical,
  45. crossAxisAlignment: CrossAxisAlignment.start,
  46. textBaseline: TextBaseline.alphabetic,
  47. mainAxisAlignment: MainAxisAlignment.center,
  48. children: <Widget>[redBox, blueBox, yellowBox, greenBox],
  49. mainAxisSize: MainAxisSize.max,
  50. );
  51. return show;
  52. }
  53. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/412156
推荐阅读
相关标签
  

闽ICP备14008679号