flutter 我的ConstrainedBox不保留最小尺寸标注

ryoqjall  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(102)

我得到一个有卡片和居中文本的框,但是当我减小窗口的大小时,我的卡片消失了,我的文本溢出了。我得到一个溢出错误时,框变得比文本小。
我希望最小的盒子尺寸为300x300,最大的盒子尺寸为600x600,而不是无限缩小
也许添加一个Singlechildscrollview是我能得到的最好的。我仍然认为有一种方法可以创建一个缩小卡达到一定的最小尺寸

class TestConstrainedBox extends StatefulWidget {
  TestConstrainedBox({Key? key}) : super(key: key);

  @override
  State<TestConstrainedBox> createState() => _TestConstrainedBoxState();
}

class _TestConstrainedBoxState extends State<TestConstrainedBox> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(title:Text("Test Constrained Box")),body: 
  SingleChildScrollView(child:
  Container(
      margin: const EdgeInsets.only(top:20.0, left: 20.0, right: 20.0, bottom:10.0),
      child:
      SizedBox.fromSize(size: const Size(450,450),
      //OverflowBox(minHeight:300,minWidth:300,
      //maxWidth:300, maxHeight: 300,
  child:ConstrainedBox(constraints: BoxConstraints(
    minWidth:300,
    minHeight:300,
    maxWidth:350,
    maxHeight:350,
    ),
    child:
    Card(child: 
    Column(mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.center,
    children:[
      //SizedBox(width:350, height:350, child:
           Text('Hello World!')
           //)
           ]))
  
))
     )
    )
    );
  }
}
yk9xbfzb

yk9xbfzb1#

官方SizedBox documentation

如果给定一个子对象,此小部件会强制它具有特定的宽度和/或高度。如果此小部件的父级不允许这些值,则将忽略它们。

例如,如果父对象是屏幕(强制子对象与父对象大小相同)或另一个SizedBox(强制其子对象具有特定宽度和/或高度),则会发生这种情况。这可以通过将子SizedBox Package 在一个小部件中来解决,该小部件允许它的大小不超过父级的大小,例如Center或Align。
因此必须删除sizedBox或将ConstrainedBox居中。

import 'package:flutter/material.dart';
import 'dart:math' as math;

void main() => runApp(MaterialApp(
      home: TestConstrainedBox(),
    ));

class TestConstrainedBox extends StatefulWidget {
  TestConstrainedBox({Key? key}) : super(key: key);

  @override
  State<TestConstrainedBox> createState() => _TestConstrainedBoxState();
}

class _TestConstrainedBoxState extends State<TestConstrainedBox> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Test Constrained Box")),
      body: SingleChildScrollView(
        child: Container(
          margin: const EdgeInsets.only(
              top: 20.0, left: 20.0, right: 20.0, bottom: 10.0),
          child: SizedBox.fromSize(
            size: const Size(450, 450),
            //OverflowBox(minHeight:300,minWidth:300,
            //maxWidth:300, maxHeight: 300,
            child: Center(
              child: ConstrainedBox(
                constraints: BoxConstraints(
                  minWidth: 300,
                  minHeight: 300,
                  maxWidth: 350,
                  maxHeight: 350,
                ),
                child: Card(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      //SizedBox(width:350, height:350, child:
                      Text('Hello World!')
                      //)
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

相关问题