import 'package:flutter/material.dart';
class PullToRefreshList extends StatefulWidget {
@override
State<PullToRefreshList> createState() => _PullToRefreshListState();
}
class _PullToRefreshListState extends State<PullToRefreshList> {
List<String> items = List.generate(5, (index) => 'Item ${index + 1}');
Future<void> _refresh() async {
await Future.delayed(Duration(seconds: 2));
setState(() {
items = items.map((item) => '$item (refreshed)').toList();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('PullToRefreshList')),
body: RefreshIndicator(
onRefresh: _refresh,
child: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(title: Text(items[index]));
},
),
),
);
}
}
This Flutter app shows a list of 5 items. The RefreshIndicator widget wraps the list and detects the pull-to-refresh gesture. When the user pulls down, the _refresh method runs, simulating a 2-second delay to mimic loading data. Then it updates the list by adding ' (refreshed)' to each item and calls setState to refresh the UI. The loading spinner appears automatically during the refresh.