Flutter SizedOverflowBox


简介

一个特定大小的窗口小部件,将其原始约束传递给其子节点,可能会溢出。

基本用法

alignment:对齐

size: 设置部件大小

  • ex:为方便看效果,现设置幕布大小为(Container)200*50。图一

  • ex:图一,基础上添加一个不设置size属性的SizeOverflowBox。图二

  • ex:图二,添加size属性,100*20,图三

  • ex:图三,添加 alignment: Alignment.center,

实例演示

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class SizeOverflowBoxDefault extends StatelessWidget {
  final double curSizeWidth;
  final double curSizeHeight;
  final String text;

  SizeOverflowBoxDefault(
      {Key key, this.curSizeWidth, this.curSizeHeight, this.text})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedOverflowBox(
      size: Size(curSizeWidth, curSizeHeight),
      alignment: Alignment.centerRight,
      //size属性设置了大小,container 里面的大小失效,
      child: Container(
        color: Color(0xfff06292),
        width: 50.0,
        height: 50.0,
        child:  Text(
          text,
          style: TextStyle(color: Colors.white),
        ),
      ),
    );
  }
}

class SizeBoxDefault extends StatelessWidget {
  final double curWidth;
  final double curHeight;

  SizeBoxDefault({Key key, this.curHeight, this.curWidth}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: curWidth,
      height: curHeight,
      child: const Card(
        child: Text(
          'SizedBox',
          style: TextStyle(color: Colors.white),
          textDirection: TextDirection.rtl,
        ),
        color: Color(0xfff06292),
      ),
    );
  }
}