flutter - Row() 中的容器在 Flutter 中不起作用
问题描述
我是 Flutter 的新手,我正在制作一些随机块来练习我的已知知识。我试图制作三列,其中最后一列将包含两行,它们都在一列中,并且它们在一个卡片小部件中。我的列工作正常,但我的行容器不工作。作为一个新手,我无法理解我在哪里以及我错过了什么或我做错了什么。
我试图制作这个视图 截图
这是我的代码-->
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyStatelessWidget(),
),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
alignment: Alignment.topCenter,
color: Colors.red,
height: 100,
),
Container(
alignment: Alignment.topCenter,
color: Colors.green,
height: 100,
),
],
),
Row(
//mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
alignment: Alignment.topCenter,
color: Colors.yellow,
height: 100,
),
Container(
alignment: Alignment.topCenter,
color: Colors.black,
height: 100,
),
TextButton(
child: const Text('Button'),
onPressed: () {},
),
const SizedBox(width: 8),
TextButton(
child: const Text('Button'),
onPressed: () {},
), //Checking if Text button is working or Not
const SizedBox(width: 8),
],
),
],
),
),
);
}
}
解决方案
您应该将文本“Button”作为子项提供给 Container,而不是像 TextButton 这样的单独的 Widget。此外,您还没有为 Container 提供任何宽度,这就是为什么您什么都看不到的原因。您可以执行以下操作:
Row(
children: <Widget>[
Expanded(
child: Container(
alignment: Alignment.topCenter,
color: Colors.yellow,
child: Center(child: const Text('Button')),
height: 100,
),
),
Expanded(
child: Container(
alignment: Alignment.topCenter,
color: Colors.black,
height: 100,
child: Center(
child: const Text('Button',
style:TextStyle(color:Colors.white)),
),
),
),
],
),
推荐阅读
- c# - 在哪里以及如何将 T 实体映射到多个不同的实体?
- c# - 组合框中的鼠标检测
- ios - 在推送的 ViewController 中隐藏 TabBar 的问题,TabBar 在一些延迟后出现
- python - 为什么“尝试相对导入超出顶级包”ValueError,而不是 ImportError?
- amazon-web-services - 如何解决堆栈跟踪错误,AWS apigateway 中的 keyerror?
- windows - 将整个文件夹(不仅仅是内容)复制到 cmd 终端中的另一个文件夹中
- npm - 忽略 NPM 中的语义版本控制
- mysql - 论坛类别/主题的Mysql查询
- c# - 为什么这个 Task.Factory.FromAsync 调用不起作用?
- c# - 在 ASP.NET 中将类绑定到下拉列表