1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Flutter】Gridviewを使ってみる

Last updated at Posted at 2024-02-08

こんにちは。
今回は、GridViewの使用方法を紹介します。

方法

スクリーンショット 2023-08-13 20.18.51.png

itemCountでアイテム数を指定して、itemBuilderでは、表示するウィジェットを返します。
SliverGridDelegateWithFixedCrossAxisCountは固定列数で表示することができます。
crossAxisCountで列数を指定できます。
mainAxisSpacingとcrossAxisSpacingでグリッド間のスペースのサイズを指定することができます。
childAspectRatioでグリッドの高さを指定しています。

使用例


class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> numberList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: GridView.builder(
          itemCount: numberList.length,
          itemBuilder: (context, index) {
            return Container(
              color: Colors.pink,
              child: Center(child: Text(numberList[index])),
            );
          },
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            mainAxisSpacing: 5,
            crossAxisSpacing: 5,
            childAspectRatio: 2.0,
          ),
        ));
  }
}


実行例

スクリーンショット 2024-02-07 18.41.27.png

最後に

ここまで読んでいただき、ありがとうございました!
いいねしてくれたら、スキップして喜びます:heartpulse:

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?