如何在Flutter中将GridView项目居中放置在Column中?

huangapple go评论58阅读模式
英文:

How to center GridView items inside a Column in Flutter?

问题

在一个SingleChildScrollView中的Column里,我有一个GridView.builder,但似乎无法使GridView中的项目在Column中水平居中。

我尝试在小部件树的许多部分添加Center,并在每个小部件的树中包含了mainAxisAlignmentcrossAxisAlignment以及更多参数。我还尝试将小部件包装在宽度为无穷大的容器中。Column中放置的其他小部件都是居中的,所以我认为问题出现在GridView的delegate中。

最小可重现示例(MRE):

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Center(
            child: GridView.builder(
              gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                  maxCrossAxisExtent: 40, crossAxisSpacing: 2.5),
              scrollDirection: Axis.vertical,
              itemCount: 9,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                return const Text(
                  '_',
                );
              },
            ),
          ),
        ],
      ),
    ),
  );
}

我找到的唯一相关答案是这个,但是对于如此微不足道的问题,复杂性和代码开销似乎是不必要的,而且很难正确缩放Text

英文:

I have a GridView.builder inside of a Column in a SingleChildScrollView. I cannot seem to get the items of the GridView to center horizontally inside of the Column.

I have tried adding Center to many parts of the widget tree and including mainAxisAlignment crossAxisAlignment and more parameters to each widget of the tree. I have also tried wrapping the widget in a container with width infinity. Other widgets placed in the column are centered, so I believe the problem lies within the GridView delegate.

MRE:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: &lt;Widget&gt;[
            Center(
              child: GridView.builder(
                gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                    maxCrossAxisExtent: 40, crossAxisSpacing: 2.5),
                scrollDirection: Axis.vertical,
                itemCount: 9,
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemBuilder: (context, index) {
                  return const Text(
                    &#39;_&#39;,
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

The only relevant answer I found was this, however the complexity and code overhead seems unneccessary for such a trivial issue, and it is hard to scale the Text correctly.

答案1

得分: 1

你需要使用 Center 小部件包裹 Column,并使用 SideBox 包裹 GridView,并根据需要设置大小。

[输出链接][1]

```dart
SingleChildScrollView(
  child: Center( // 将 Center 包裹在 Column 外部
    child: Column(
      children: <Widget>[
        SizedBox(
          width: 400, // 关键在于这里
          child: GridView.builder(
            gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
              maxCrossAxisExtent: 40, 
              crossAxisSpacing: 2.5,
              mainAxisSpacing: 2.5,
            ),
            itemCount: 9,
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              return Container(
                color: Colors.yellow,
                child: const Text(
                  '_',
                  textAlign: TextAlign.center,
                ),
              );
            },
          ),
        ),
      ],
    ),
  ),
),

<details>
<summary>英文:</summary>

You have to wrap Column with Center widget and GridView with SideBox and set the size based on your need.

[1] ```dart SingleChildScrollView( child: Center( // Wrap center outside of column child: Column( children: &lt;Widget&gt;[ SizedBox( width: 400, // The trick is here child: GridView.builder( gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent( maxCrossAxisExtent: 40, crossAxisSpacing: 2.5, mainAxisSpacing: 2.5, ), itemCount: 9, shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), itemBuilder: (context, index) { return Container( color: Colors.yellow, child: const Text( &#39;_&#39;, textAlign: TextAlign.center, ), ); }, ), ), ], ), ), ),

huangapple
  • 本文由 发表于 2023年5月28日 09:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定