GridView
適合顯示水平清單,且自動根據目前寬度換行。
Version
Flutter 3.24.5
Flutter
- Android 與 iOS 都成功使用
GridView
實現水平清單
GridView
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
const Home({super.key});
State<Home> createState() => _Home();
}
class _Home extends State<Home> {
final List<String> data = List.generate(
8,
(index) => 'Item ${index + 1}',
);
Widget build(BuildContext context) {
var appBar = AppBar(
title: const Text(
'GridView.builder',
),
);
var gridView = GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 8,
mainAxisSpacing: 8,
childAspectRatio: 2,
),
itemCount: data.length,
itemBuilder: (
BuildContext context,
int index,
) {
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8),
),
child: Text(
data[index],
style: const TextStyle(
color: Colors.white,
),
),
);
},
);
var body = Padding(
padding: const EdgeInsets.all(8.0),
child: gridView,
);
return Scaffold(
appBar: appBar,
body: body,
);
}
}
Line 11
final List<String> data = List.generate(
8,
(index) => 'Item ${index + 1}',
);
List.generate()
:產生8
筆 List 假資料
Line 25
var gridView = GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 8,
mainAxisSpacing: 8,
childAspectRatio: 2,
),
itemCount: data.length,
)
GridView.builder()
:建立 GridViewgridDelegate:使用
SliverGridDelegateWithFixedCrossAxisCount
指定網格佈局crossAxisCount
:設定橫軸顯示個數crossAxisSpacing
:設定橫軸間距mainAxisSpacing
:設定縱軸間距childAspectRatio
:設定項目的長寬比例
itemCount
: data.length,
Line 32
var gridView = GridView.builder(
itemBuilder: (
BuildContext context,
int index,
) {
return Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8),
),
child: Text(
data[index],
style: const TextStyle(
color: Colors.white,
),
),
);
},
)
- 在 ItemBuilder 內實現每一筆 GridView 的 UI
Conclusion
GridView.count()
與GridView.build()
的功能大致相同GridView.count()
語法較精簡,適合少量資料GridView.build()
語法稍複雜,適合大量資料,並支援 Lazy Loading