gpt4 book ai didi

flutter - 如何制作适合 Flutter 剩余空间的 4 网格

转载 作者:IT王子 更新时间:2023-10-29 06:43:20 30 4
gpt4 key购买 nike

我正在尝试制作一个看起来像以下设计的小部件。我似乎无法弄清楚如何让我的元素增长。在 android 中我会使用 match_parent。但这是我无法弄清楚它需要如何实现的事情?

My design

这是我得到的:

enter image description here

我的自定义小部件

import 'package:flutter/material.dart';

class HomeTiles extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Column(mainAxisSize: MainAxisSize.max, children: [
Container(
child: Row(children: [
Expanded(
child: HomeItem(
title: Localization.of(context).homeExercises,
icon: Icons.fitness_center,
color: ThemeColors.blueColor)),
Padding(padding: EdgeInsets.only(left: ThemeDimens.padding)),
Expanded(
child: HomeItem(
title: Localization.of(context).homeDiet,
icon: Icons.restaurant_menu,
color: ThemeColors.greenColor))
])),
Padding(padding: EdgeInsets.only(top: ThemeDimens.padding)),
Container(
child: Row(
children: [
Expanded(
child: HomeItem(
title: Localization.of(context).homeTimer,
icon: Icons.timer,
color: ThemeColors.redColor)),
Padding(padding: EdgeInsets.only(left: ThemeDimens.padding)),
Expanded(
child: HomeItem(
title: Localization.of(context).homeMyClub,
icon: Icons.home,
color: ThemeColors.orangeColor)),
],
))
]));
}
}

这是 HomeTiles 小部件的父级:

import 'package:balance/page/history_page.dart';
import 'package:balance/page/users_page.dart';
import 'package:balance/widget/general/background.dart';
import 'package:balance/widget/general/bottom_navigation.dart';
import 'package:balance/widget/home/home_tiles.dart';
import 'package:flutter/material.dart';

class HomeScreen extends StatefulWidget {
@override
HomeScreenState createState() {
return new HomeScreenState();
}
}

class HomeScreenState extends State<HomeScreen> {
int _page = 0;

@override
Widget build(BuildContext context) {
return Scaffold(
body: Background(child: _getCorrectPage()),
bottomNavigationBar: BottomNavigation(onPageSelected: onPageSelected));
}

void onPageSelected(int index) {
setState(() {
_page = index;
});
}

Widget _getCorrectPage() {
switch (_page) {
case 0:
return HomeTiles();
case 1:
return HistoryPage();
case 2:
return UsersPage();
}
return Container();
}
}

给定的代码是我现在拥有的。我拍了第一张照片。使用此代码(我对高度值进行了硬编码)所以我知道我的其他小部件 HomeItem 工作得很好:

import 'package:flutter/material.dart';

class HomeTiles extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: 798,
child: Column(mainAxisSize: MainAxisSize.max, children: [
Container(
height: (798 / 2) - 8,
child: Row(children: [
Expanded(
child: HomeItem(
title: Localization.of(context).homeExercises,
icon: Icons.fitness_center,
color: ThemeColors.blueColor)),
Padding(padding: EdgeInsets.only(left: ThemeDimens.padding)),
Expanded(
child: HomeItem(
title: Localization.of(context).homeDiet,
icon: Icons.restaurant_menu,
color: ThemeColors.greenColor))
])),
Padding(padding: EdgeInsets.only(top: ThemeDimens.padding)),
Container(
height: (798 / 2) - 8,
child: Row(
children: [
Expanded(
child: HomeItem(
title: Localization.of(context).homeTimer,
icon: Icons.timer,
color: ThemeColors.redColor)),
Padding(padding: EdgeInsets.only(left: ThemeDimens.padding)),
Expanded(
child: HomeItem(
title: Localization.of(context).homeMyClub,
icon: Icons.home,
color: ThemeColors.orangeColor)),
],
))
]));
}
}

我整天都在网上搜索。而且我没有得到我真正想要的结果。你会怎么做?有解决办法吗?

最佳答案

尝试将元素置于 Column 的中心:

Container(
color: Colors.white,
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,

并使用 Expanded

展开 Column 中的每个元素
         children: [
Expanded(
child: Container(
child: Row(children: [
Expanded(
child: HomeItem(
...

关于flutter - 如何制作适合 Flutter 剩余空间的 4 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54171947/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com