Flutter 在行中对齐单个 child
Flutter align a single child in row
我有一个包含行数组的列,每行内部都有一些容器。这是我的代码:
import 'package:flutter/material.dart';
Widget row() {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
color: Colors.amber,
width: 50,
height: 100,
),
Container(
color: Colors.amber,
width: 50,
height: 150,
),
Container(
color: Colors.blue,
width: 50,
height: 80,
),
],
);
}
class TestPage extends StatelessWidget {
const TestPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
row(),
Divider(),
row(),
Divider(),
row(),
],
);
}
}
输出:
我想将蓝色容器与底部对齐,而不触及其他容器位置。我怎样才能做到这一点?
这是我的预期结果:
请试试这个
import 'package:flutter/material.dart';
Widget row() {
return Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
color: Colors.amber,
width: 50,
height: 100,
),
Container(
color: Colors.amber,
width: 50,
height: 150,
),
],
),
Container(
color: Colors.blue,
width: 50,
height: 80,
),
],
);
}
class TestPage extends StatelessWidget {
const TestPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
row(),
Divider(),
row(),
Divider(),
row(),
],
);
}
}
使用 CrossAxisAlignment end
获取主行并将 Amber 包裹在另一行中并使用 CrossAxisAlignment start
.
进行声明
这是如何工作的?
因为你的蓝色容器现在将在主行内并且现在用 CrossAxisAlignment.end 声明,因此它将与父对齐。
Widget row() {
return Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
Container(
color: Colors.amber,
width: 50,
height: 100,
),
Container(
color: Colors.amber,
width: 50,
height: 150,
)
]),
Container(
alignment: Alignment.bottomCenter,
color: Colors.blue,
width: 50,
height: 80,
),
],
);
}
我有一个包含行数组的列,每行内部都有一些容器。这是我的代码:
import 'package:flutter/material.dart';
Widget row() {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
color: Colors.amber,
width: 50,
height: 100,
),
Container(
color: Colors.amber,
width: 50,
height: 150,
),
Container(
color: Colors.blue,
width: 50,
height: 80,
),
],
);
}
class TestPage extends StatelessWidget {
const TestPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
row(),
Divider(),
row(),
Divider(),
row(),
],
);
}
}
输出:
我想将蓝色容器与底部对齐,而不触及其他容器位置。我怎样才能做到这一点? 这是我的预期结果:
请试试这个
import 'package:flutter/material.dart';
Widget row() {
return Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
color: Colors.amber,
width: 50,
height: 100,
),
Container(
color: Colors.amber,
width: 50,
height: 150,
),
],
),
Container(
color: Colors.blue,
width: 50,
height: 80,
),
],
);
}
class TestPage extends StatelessWidget {
const TestPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
children: [
row(),
Divider(),
row(),
Divider(),
row(),
],
);
}
}
使用 CrossAxisAlignment end
获取主行并将 Amber 包裹在另一行中并使用 CrossAxisAlignment start
.
这是如何工作的?
因为你的蓝色容器现在将在主行内并且现在用 CrossAxisAlignment.end 声明,因此它将与父对齐。
Widget row() {
return Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
Container(
color: Colors.amber,
width: 50,
height: 100,
),
Container(
color: Colors.amber,
width: 50,
height: 150,
)
]),
Container(
alignment: Alignment.bottomCenter,
color: Colors.blue,
width: 50,
height: 80,
),
],
);
}