一统天下 flutter - widget 容器类(只能有一个子): FractionallySizedBox - 按比值占用可用空间

源码 https://github.com/webabcd/flutter_demo
作者 webabcd

一统天下 flutter - widget 容器类(只能有一个子): FractionallySizedBox - 按比值占用可用空间

示例如下:

lib\widget\container\fractionally_sized_box.dart

/*
 * FractionallySizedBox - 按比值占用可用空间
 */

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Colors.red,
      child: FractionallySizedBox(
        /// 实际宽度:可用宽度 * 0.5
        widthFactor: 0.5,
        /// 实际高度:可用高度 * 0.5
        heightFactor: 0.5,
        /// 子的对齐方式
        alignment: FractionalOffset.center,
        child: Container(
          color: Colors.green,
        ),
      ),
    );
  }
}

源码 https://github.com/webabcd/flutter_demo
作者 webabcd

posted @ 2023-04-26 17:03  webabcd  阅读(29)  评论(0编辑  收藏  举报