flutter - 如何设置包装中最后两个元素之间的空间
问题描述
我是新来的。我想构建一个如下所示的布局。对于第 1 部分和第 2 部分是正确的和我想要的。
但是对于第 3 部分,我想在 bbbb 和 cccc 元素之间设置空间。但我尝试 flex / row / wrap 最后两个元素仍然不是我想要的。
有人对目标有任何想法吗?谢谢
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
SizedBox(
width: double.infinity,
child: Wrap(
alignment: WrapAlignment.spaceBetween,
runAlignment: WrapAlignment.spaceBetween,
children: [
Container(
color: Colors.red,
child: const Text('Part 1'),
),
Wrap(
alignment: WrapAlignment.spaceBetween,
runAlignment: WrapAlignment.spaceBetween,
children: [
Container(
color: Colors.blue,
child: const Text('bbbbbbbb'),
),
Container(color: Colors.green, child: const Text('cccccccc')),
],
),
],
),
),
SizedBox(
width: double.infinity,
child: Wrap(
alignment: WrapAlignment.spaceBetween,
runAlignment: WrapAlignment.spaceBetween,
children: [
Container(
color: Colors.red,
child: const Text('Part 2'),
),
Flex(
// alignment: WrapAlignment.spaceBetween,
// runAlignment: WrapAlignment.spaceBetween,
direction: Axis.horizontal,
children: [
Flexible(
child: Container(
color: Colors.blue,
child: const Text(
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'),
),
),
Container(color: Colors.green, child: const Text('cccccccc')),
],
),
],
),
),
SizedBox(
width: double.infinity,
child: Wrap(
alignment: WrapAlignment.spaceBetween,
runAlignment: WrapAlignment.spaceBetween,
children: [
Container(
color: Colors.red,
child: const Text('Part 3'),
),
Flex(
// alignment: WrapAlignment.spaceBetween,
// runAlignment: WrapAlignment.spaceBetween,
direction: Axis.horizontal,
children: [
Flexible(
child: Container(
color: Colors.blue,
child: const Text('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'),
),
),
Container(color: Colors.green, child: const Text('cccccccc')),
],
),
],
),
),
],
),
),
);
}
}
解决方案
可以考虑SizedBox
在它们之间添加。
Wrap(
alignment: WrapAlignment.spaceBetween,
runAlignment: WrapAlignment.spaceBetween,
children: [
Container(
color: Colors.blue,
child: const Text('bbbbbbbb'),
),
// add some space using SizedBox
SizedBox(width: 8.0),
Container(color: Colors.green, child: const Text('cccccccc')),
],
),
推荐阅读
- .net - WinSCP NuGet 包 .NET 标准错误
- sharepoint - 如何检查具有特定ID的用户是否存在?
- oracle - 无法从数组中删除元素
- java - Drools 6.3.0:升级后旧的 ZiPKieModules 保留在堆中(在 kiescanner JMX bean 中执行 scanNow)
- mysql - 选择不在另一个选择中的位置
- android - 发布 Android 应用程序后,如何向 SQLite 数据库添加新列?
- r - 在 R 中处理形状文件时如何保留数据框?
- java - 在准备好的语句的子句中查询构建器
- postgresql - 如何在 PostgreSQL 中执行多条语句
- azure - 将 Azure 函数转换为 WebAPI 调用 Azure 存储