android-layout - BoxConstraints 在拉伸容器时强制使用无限高
问题描述
我是 Android 开发的新手。我正在尝试使用行和列小部件垂直拉伸 3 个容器和水平拉伸 3 个容器。每次我crossAxisAlignment: CrossAxisAlignment.stretch,
在我的 Row 小部件中添加时,我都会得到一个BoxConstraints 强制无限高度错误。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Multichild Container Test'),
),
body: SafeArea(
child: Container(
child: Column(
children: <Widget>[
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.pink.shade900,
),
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.green.shade900,
),
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.deepPurple.shade900,
),
],
),
),
Container(
child: Row(
//crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.pink.shade900,
),
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.green.shade900,
),
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.deepPurple.shade900,
),
],
),
)
],
)
),
),
),
);
}
}
解决方案
您可以在下面使用复制粘贴运行完整代码
您可以使用Expanded
andflex
为Container
代码片段提供高度
Expanded(
flex: 1,
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Multichild Container Test'),
),
body: SafeArea(
child: Container(
child: Column(
children: <Widget>[
Expanded(
flex: 1,
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.pink.shade900,
),
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.green.shade900,
),
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.deepPurple.shade900,
),
],
),
),
),
Expanded(
flex: 1,
child: Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.pink.shade900,
),
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.green.shade900,
),
Container(
width: 60.0,
height: 100.0,
child: Text('Container 1'),
color: Colors.deepPurple.shade900,
),
],
),
),
)
],
)),
),
),
);
}
}
推荐阅读
- jenkins - Jenkins 升级到 2.212 后无法启动
- algorithm - 异步无向树中的领导者选举
- xamarin - 如何确保 Xamarin.ios 扩展项目与主项目一起部署
- oracle - 尝试访问 oracle 数据库,但触发器阻止我连接
- php - 来自自定义作曲家包的 UserProvider [Symfony 4]
- python - 如何创建代表一副纸牌的元组列表?
- gradle - 如何在报告门户上查看来自 gradle 的 TestNG 测试结果 xml 结果?
- mysql - Docker 教程不适用于 springboot+mysql+react 应用程序
- google-apps-script - 如何验证 Web App Apps 脚本 url 以查看 Directory API 更改
- c - 我的 uvlib 默认循环意外退出 - 为什么?