Flutter -- 5.布局相关

2021-11-04  本文已影响0人  MissStitch丶

1.Container

class ContainerDemo extends StatelessWidget {
  const ContainerDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return Container(
      color: Colors.grey[400],
      child: Container(
        width: 200,
        height: 200,
        color: Colors.red,
      ),
      alignment: Alignment.center, //设置后,不受child影响
    );
  }
}
layout_container.png layout_container_alignment.png

2.Flex

class FlexDemo extends StatelessWidget {
  const FlexDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey[400],
      child: Flex(
        direction: Axis.horizontal,
        children: [
          Container(
            width: 60,
            color: Colors.red,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Container(
            width: 80,
            color: Colors.blue,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Container(
            width: 100,
            color: Colors.yellow,
            child: const AspectRatio(aspectRatio: 1,),
          ),
        ],
      ),
    );
  }
}
laout_flex_horizontal laout_flex_vertical

3.主轴

class FlexDemo extends StatelessWidget {
  const FlexDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey[400],
      alignment: Alignment.center,
      child: Flex(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        direction: Axis.horizontal,
        children: [
          Container(
            width: 60,
            color: Colors.red,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Container(
            width: 80,
            color: Colors.blue,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Container(
            width: 100,
            color: Colors.yellow,
            child: const AspectRatio(aspectRatio: 1,),
          ),
        ],
      ),
    );
  }
}
flex_mainAxisAlignment_start.png flex_mainAxisAlignment_center.png flex_mainAxisAlignment_end.png
flex_mainAxisAlignment_spaceBetween.png flex_mainAxisAlignment_spaceAround.png flex_mainAxisAlignments_paceEvenly.png

4.交叉轴

class FlexBaselineDemo extends StatelessWidget {
  const FlexBaselineDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey[400],
      alignment: Alignment.center,
      child: Flex(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.baseline,
        /*
        * TextBaseLine
        * alphabetic,英文
        * ideographic,其他语言
        * 测试完感觉2者在中文/英文来用任意一个都差不多
        * */
        textBaseline: TextBaseline.ideographic, 
        direction: Axis.horizontal,
        children: [
          Container(
            width: 60,
            color: Colors.red,
            child: const AspectRatio(
              aspectRatio: 1,
              child: Center(
                child: Text("红色", style: TextStyle(fontSize: 15, color: Colors.white),),
              ),
            ),
          ),
          Container(
            width: 80,
            color: Colors.blue,
            child: const AspectRatio(
              aspectRatio: 1,
              child: Center(
                child: Text("蓝色", style: TextStyle(fontSize: 20, color: Colors.white),),
              ),
            ),
          ),
          Container(
            width: 100,
            color: Colors.yellow,
            child: const AspectRatio(
              aspectRatio: 1,
              child: Center(
                child: Text("黄色", style: TextStyle(fontSize: 30, color: Colors.white),),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
flex_crossAxisAlignment_start.png flex_crossAxisAlignment_center.png flex_crossAxisAlignment_end.png
flex_crossAxisAlignment_stretch.png flex_crossAxisAlignment_baseline_ideographic.png flex_crossAxisAlignment_baseline_alphabetic.png

5.Row、Column

class RowOrColumnDemo extends StatelessWidget {

  final bool isRow;
  
  const RowOrColumnDemo(this.isRow);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey[400],
      alignment: Alignment.center,
      child: isRow ? Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: 60,
            color: Colors.red,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Container(
            width: 80,
            color: Colors.blue,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Container(
            width: 100,
            color: Colors.yellow,
            child: const AspectRatio(aspectRatio: 1,),
          ),
        ],
      ) :
      Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: 60,
            color: Colors.red,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Container(
            width: 80,
            color: Colors.blue,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Container(
            width: 100,
            color: Colors.yellow,
            child: const AspectRatio(aspectRatio: 1,),
          ),
        ],
      ) ,
    );
  }
}

6.Stack

class StackDemo extends StatelessWidget {

  final bool isShowPositioned;

  const StackDemo(this.isShowPositioned);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey[400],
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height - kToolbarHeight - _statusBarHeight,
      child: Stack(
        alignment: Alignment.center,
        children: [
          Container(
            width: 200,
            height: 200,
            color: Colors.red,
          ),
          Container(
            width: 150,
            height: 150,
            color: Colors.blue,
          ),
          Container(
            width: 100,
            height: 100,
            color: Colors.yellow,
          ),
          if (isShowPositioned) Positioned(
            right: 0,
            width: 50,
            height: 150,
            top: 150,
            child: Container(
              color: Colors.purple,
            )),
        ],
      ),
    );
  }
}
stack_normal.png stack_positioned.png

7. AspectRatio

Container(
  width: 200,
  height: 150,
  color: Colors.red,
  child: const AspectRatio(aspectRatio: 1,), //失效
)

8.Expanded

class ExpandedDemo extends StatelessWidget {

  final bool isRow;

  const ExpandedDemo({required this.isRow});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey[400],
      alignment: Alignment.center,
      child: isRow ? Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: 60,
            color: Colors.red,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Expanded( //蓝色在主轴上填满
            child: Container(
              color: Colors.blue,
              child: const AspectRatio(aspectRatio: 1,),
            ),),
          Container(
            width: 100,
            color: Colors.yellow,
            child: const AspectRatio(aspectRatio: 1,),
          ),
        ],
      ) :
      Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: 60,
            color: Colors.red,
            child: const AspectRatio(aspectRatio: 1,),
          ),
          Expanded( //蓝色在主轴上填满
            child: Container(
              width: 80,
              color: Colors.blue,
              child: const AspectRatio(aspectRatio: 1,),
            ),
          ),
          Container(
            width: 100,
            color: Colors.yellow,
            child: const AspectRatio(aspectRatio: 1,),
          ),
        ],
      ) ,
    );
  }
}
row_expanded.png column_expanded.png

9.margin&padding

上一篇下一篇

猜你喜欢

热点阅读