Flutter 3.38で追加されたSliverGrid.listで冗長なコードとサヨナラする
この記事は 株式会社ガラパゴス(有志) Advent Calendar 2025 の25日目(最終日)の記事です。
はじめに
2025年11月にリリースされたFlutter 3.38では、Widget PreviewsやDot Shorthandsといった注目度の高い新機能が追加されましたが、今回は細かな変更点について注目したいと思います。
それが、今回紹介するSliverGrid.listコンストラクタです。
CustomScrollView内でグリッド表示を実装する際、「なぜこんなに冗長な記述が必要なのか」と感じたことはありませんか?この新しいコンストラクタは、そんな開発者の小さなストレスを解消してくれます。
本記事では、SliverGrid.listの使い方と、従来の書き方との違いを解説します。
従来の課題:なぜSliverChildListDelegateを書かなければならないのか
CustomScrollViewで固定数のウィジェットをグリッド表示したい場合、これまでは以下のような記述が必要でした。
// Flutter 3.37以前の書き方
SliverGrid(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
),
// 子のリストを渡すだけなのに、Delegateの記述が必須
delegate: SliverChildListDelegate(
[
Container(color: Colors.red),
Container(color: Colors.green),
Container(color: Colors.blue),
// ...
],
),
)
ListViewやColumnではchildrenに直接リストを渡せるのに、SliverGridではSliverChildListDelegateという長いクラス名を書き、ネストを一段深くする必要がありました。
SliverGrid.countやSliverGrid.extentを使えばこの記述を省略できますが、gridDelegateを細かくカスタマイズしたい場合には使えません。この「Delegateの壁」が小さなストレスになっていました。
解決策:SliverGrid.listの登場
Flutter 3.38で追加されたSliverGrid.listコンストラクタを使えば、この問題は解決します。
- // Flutter 3.37以前の書き方
- SliverGrid(
+ // Flutter 3.38以降の書き方
+ SliverGrid.list(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
),
- // 以前:子のリストを渡すだけなのに、Delegateの記述が必須
- delegate: SliverChildListDelegate(
- [
- Container(color: Colors.red),
- Container(color: Colors.green),
- Container(color: Colors.blue),
- ],
- ),
+ // 以後:ListViewと同じ感覚で書ける!
+ children: [
+ Container(color: Colors.red),
+ Container(color: Colors.green),
+ Container(color: Colors.blue),
+ ],
)
childrenプロパティに直接リストを渡すだけで動作します。SliverChildListDelegateの記述が不要になり、ネストが一段浅くなったことで、コードの見通しが改善されました。
技術的なメリット
1. パラメータ指定がフラットに
内部的にはSliverChildListDelegateを生成してラップしていますが、これまでDelegateの引数として渡していたプロパティが、SliverGrid.listの直接の引数として指定できるようになりました。
addAutomaticKeepAlivesaddRepaintBoundariesaddSemanticIndexessemanticIndexOffset
これにより、オプション指定時の記述量も削減されます。
// パラメータをフラットに指定できる
SliverGrid.list(
gridDelegate: /* ... */,
children: /* ... */,
addAutomaticKeepAlives: false,
semanticIndexOffset: 10,
)
2. アクセシビリティ対応が容易に
semanticIndexOffsetを簡単に指定できるようになったことで、複数のSliverGridを縦に連結する際のアクセシビリティ対応がスムーズになります。
例えば、スクリーンリーダー(TalkBack/VoiceOver)で読み上げる際、複数のグリッドをまたいでも連続したインデックスを保持できるため、「見た目は分かれているが、意味的には1つの連続したリスト」として扱えます。
3. パフォーマンスは従来と同等
新しいコンストラクタは既存の仕組みをラップしているだけなので、描画結果もパフォーマンス特性も従来のSliverGridと全く同じです。安心して移行できます。
いつ使うべきか
SliverGrid.listが適しているのは、以下のようなケースです。
適している場面
- 設定画面やメニュー画面など、要素数が固定のグリッド
- アイコンやカテゴリなど、少数の項目を表示する場合
-
gridDelegateを細かくカスタマイズしたい場合
適していない場面
- 要素数が動的に変わる、または大量のデータを扱う場合 →
SliverGrid.builderを使用 - クロス軸の数だけ指定すれば十分な場合 →
SliverGrid.countで十分
実践:サンプルコードで動作確認
実際に動作するサンプルコードを用意しました。従来の書き方と新しい書き方を並べて比較できます。
検証環境
$ fvm flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.38.5, on macOS 26.1 25B78 darwin-arm64, locale ja-JP)
[✓] Xcode - develop for iOS and macOS (Xcode 26.0.1)
サンプルコード全体
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: const SliverGridPage(),
);
}
}
class SliverGridPage extends StatelessWidget {
const SliverGridPage({super.key});
@override
Widget build(BuildContext context) {
// 表示するダミーウィジェットのリスト
final List<Widget> demoChildren = List.generate(
6,
(index) => Card(
color: Colors.primaries[index % Colors.primaries.length].shade200,
child: Center(
child: Text(
'Item $index',
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
),
);
return Scaffold(
appBar: AppBar(
title: const Text('SliverGrid.list 検証'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: CustomScrollView(
slivers: [
// ---------------------------------------------------------
// 1. ヘッダー (Before)
// ---------------------------------------------------------
const SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Text(
'Before: 従来の書き方\n(delegate: SliverChildListDelegateが必要)',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
),
),
),
// 従来の書き方
SliverGrid(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 8.0,
crossAxisSpacing: 8.0,
childAspectRatio: 1.5,
),
// ここが冗長だった部分
delegate: SliverChildListDelegate(
demoChildren.map((w) => _wrapWithLabel(w, "Old")).toList(),
),
),
// ---------------------------------------------------------
// 2. ヘッダー (After)
// ---------------------------------------------------------
const SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.fromLTRB(16.0, 32.0, 16.0, 16.0),
child: Text(
'After: Flutter 3.38\n(SliverGrid.listを使用)',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16,
color: Colors.blue,
),
),
),
),
// 新しい書き方 (Flutter 3.38+)
// children プロパティに直接リストを渡せる
SliverGrid.list(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 8.0,
crossAxisSpacing: 8.0,
childAspectRatio: 1.5,
),
children: demoChildren
.map((w) => _wrapWithLabel(w, "New"))
.toList(),
),
// 余白用
const SliverToBoxAdapter(child: SizedBox(height: 50)),
],
),
);
}
// 識別用のラベルを付けるヘルパー関数
Widget _wrapWithLabel(Widget child, String label) {
return Stack(
children: [
child,
Positioned(
right: 4,
bottom: 4,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
color: Colors.black54,
child: Text(
label,
style: const TextStyle(color: Colors.white, fontSize: 10),
),
),
),
],
);
}
}
実行結果

従来の書き方(Before)と新しい書き方(After)で、全く同じ描画結果が得られることが確認できます。コードの見た目はスッキリしましたが、動作は完全に一致しています。
まとめ
-
SliverGrid.listを使えば、固定リストのグリッド表示がよりシンプルに書ける -
SliverChildListDelegateを明示的に書くボイラープレートが不要に - パラメータ指定がフラットになり、可読性とメンテナンス性が向上
- 内部実装は従来と同じため、パフォーマンスや動作に影響なし
目立つ機能ではありませんが、繰り返し書くコードをシンプルにできる点で価値のある追加です。
Flutter 3.38にアップデートしたら、ぜひ既存のコードをリファクタリングしてみてください。きっと、コードがすっきりとした印象を受けるはずです。
Discussion