[英]How to align widgets in a GridView in Flutter?
I want to align the contents of a GridView item to the center ( both vertically and horizontally ) without changing the size of the widget .我想在不改变小部件大小的情况下将GridView 项目的内容与中心(垂直和水平)对齐。
This code creates a GridView
with an item in it, it has a default top-center alignment and the size is a default square.此代码创建一个GridView
,其中包含一个项目,它有一个默认的顶部中心 alignment 并且大小是默认正方形。
GridView.count(
crossAxisCount: 3,
children: [
Container(
// alignment: Alignment.center,
child: RaisedButton(
child: Column(
children: [
Text("Top text"),
Text("Bottom text"),
],
),
onPressed: (){}
),
),
],
);
And it looks like this:它看起来像这样:
When I uncomment the alignment, it doesn't align the items correctly and the size of the item changes, I don't want neither of them.当我取消注释 alignment 时,它没有正确对齐项目并且项目的大小发生变化,我不想要它们。
Note that I need multiple Widget
s in that GridView
item, so I can't remove the Column
(but maybe I can replace it?).请注意,我在该GridView
项目中需要多个Widget
,因此我无法删除Column
(但也许我可以替换它?)。
I don't see any size change after adding mainAxisAlignment to the Column.将 mainAxisAlignment 添加到列后,我看不到任何大小变化。 Please run the code below:请运行以下代码:
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GridView.count(
crossAxisCount: 3,
children: [
Container(
// alignment: Alignment.center,
child: RaisedButton(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Top text"),
Text("Bottom text"),
],
),
onPressed: () {}),
),
],
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.