Skip to content

Instantly share code, notes, and snippets.

@hyochan
Last active November 12, 2022 09:37
Show Gist options
  • Save hyochan/af18f4dbab21a3297348410278063b8b to your computer and use it in GitHub Desktop.
Save hyochan/af18f4dbab21a3297348410278063b8b to your computer and use it in GitHub Desktop.
flutter flat list code snippet
import 'package:flat_list/flat_list.dart';
import 'package:flat_list_example/common_views.dart';
import 'package:flat_list_example/data.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
class FlatListEx1 extends HookWidget {
static const name = '/flat-list-ex1';
const FlatListEx1({super.key});
@override
Widget build(BuildContext context) {
var items = useState(data);
var loading = useState(false);
return Scaffold(
appBar: AppBar(
title: const Text('Simple Example'),
),
body: SafeArea(
child: FlatList(
onEndReached: () async {
loading.value = true;
await Future.delayed(const Duration(seconds: 2));
if (context.mounted) {
items.value += getMoreData();
loading.value = false;
}
},
onRefresh: () async {
await Future.delayed(const Duration(seconds: 2));
if (context.mounted) {
items.value = data;
}
},
loading: loading.value,
listHeaderWidget: const Header(),
listFooterWidget: const Footer(),
listEmptyWidget: Container(
alignment: Alignment.center,
padding: const EdgeInsets.all(12),
child: const Text('List is empty!'),
),
data: items.value,
buildItem: (item, index) {
var person = items.value[index];
return ListItemView(person: person);
},
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment