我的问题的背景:如果需要,小部件将其子小Expanded部件缩小到零高度并忽略其子小部件约束。
目标:防止扩展widget缩小到小于其子级大小,以便其小部件不会被破坏。
此外,我需要小部件占用所有可用空间并扩展其子项。
旁注:
Container用于分配高度空间以expanded对空间变化做出反应
这是代码:
import 'package:flutter/material.dart';
class MainScreen extends StatelessWidget {
const MainScreen({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
child: Card(
color: Colors.blueAccent,
child: LimitedBox(
maxHeight: 200,
child: Text(
'Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text Test dummy text ',
style: TextStyle(fontSize: 60, color: Colors.black),
),
),
),
),
Container(
color: Colors.amberAccent,
height: 105,
width: 200,
)
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是不需要的电流输出:
Expanded 正常行为:
使用Flexible小部件并添加Container最小高度,蓝色容器的大小会随着文本的增加而增加。
Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: Container(
constraints: BoxConstraints(
minHeight: 200, minWidth: double.infinity),
child: Card(
color: Colors.blueAccent,
child: Text(
'Sample Text',
style: TextStyle(fontSize: 60, color: Colors.black),
),
),
),
),
Container(
color: Colors.amberAccent,
height: 105,
width: 200,
)
],
),
)
),
Run Code Online (Sandbox Code Playgroud)
输出:
| 归档时间: |
|
| 查看次数: |
3405 次 |
| 最近记录: |