Skip to content

Instantly share code, notes, and snippets.

@RyouMon
Created December 26, 2022 12:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save RyouMon/488c32baa3763408a2513fe932d5f5ee to your computer and use it in GitHub Desktop.
Flutter Demo: Infinite GridView
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class InfiniteGridView extends StatefulWidget {
const InfiniteGridView({super.key});
@override
State<InfiniteGridView> createState() => _InfiniteGridViewState();
}
class _InfiniteGridViewState extends State<InfiniteGridView> {
List<IconData> icons = [];
@override
void initState() {
super.initState();
retrieveIcons();
}
@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemCount: icons.length,
itemBuilder: ((context, index) {
if (index == icons.length - 1 && icons.length < 200) {
retrieveIcons();
}
return Icon(icons[index]);
}));
}
void retrieveIcons() {
Future.delayed(const Duration(milliseconds: 200)).then((value) {
setState(() {
icons.addAll([
Icons.ac_unit,
Icons.airport_shuttle,
Icons.all_inclusive,
Icons.beach_access,
Icons.cake,
Icons.free_breakfast,
]);
});
});
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: const InfiniteGridView(),
);
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(title: 'Infinite GridView'),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment