riverpod_infinite_scroll_page 0.0.1 riverpod_infinite_scroll_page: ^0.0.1 copied to clipboard
This project is a sample Flutter application demonstrating infinite scrolling pagination using Riverpod for state management. It builds on the infinite_scroll_pagination package and includes optimizat [...]
Here’s the README with the additional explanation:
Riverpod Infinite Scroll Page
This project is a sample Flutter application demonstrating infinite scrolling pagination using Riverpod for state management. It builds on the infinite_scroll_pagination package and includes optimizations to avoid unnecessary rebuilds. Specifically, it prevents the entire list from being rebuilt when only specific items need updating, resulting in improved performance for large lists.
Features
• Infinite scroll pagination
• State management with Riverpod
• Optimized to avoid rebuilding the entire list when list state changes
• Supports updating individual list items without triggering a full list rebuild
• Error handling and retry mechanisms
Requirements
This project uses the following dependencies:
• Flutter: 3.0.0 or higher
• Riverpod: ^2.0.0
Installation
1. Clone the project to your local machine:
git clone https://github.com/lhjandroid/riverpod_infinite_scroll_page.git cd riverpod_infinite_scroll_page
2. Install dependencies:
flutter pub get
3. Run the project:
flutter run
Project Structure
• lib/
• main.dart: The application entry point.
• models/: Defines data models.
• providers/: Defines Riverpod providers, including the pagination state manager.
• widgets/: Defines reusable UI components.
Key Functionality
Pagination with Riverpod
This example uses Riverpod to manage pagination state and data fetching. The paging_provider.dart file in the providers/ directory handles the pagination logic and state management.
Main states:
• isLoading: Indicates if data is currently loading.
• hasError: Indicates if an error occurred during loading.
• hasMoreData: Indicates if there is more data to load.
Infinite Scrolling Implementation
In the main screen under screens/, a ListView.builder creates the scrollable list. By listening to the scroll position, the application loads more data as the user reaches the bottom of the list.
class TestPage extends ConsumerStatefulWidget {
const TestPage({super.key});
@override
ConsumerState<ConsumerStatefulWidget> createState() => _TestPageState();
}
class _TestPageState extends ConsumerState<TestPage> {
final String pageKey = '0';
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PagedListView<String, PagingItem>(
pagingControllerProvider: pagingControllerProvider(pageKey),
builderDelegate: itemRegister(),
pagingDataController: TestPageNetWorkController(),
statusBuilderDelegate: statusBuilder(),
),
floatingActionButton: FloatingActionButton(onPressed: () {
final pagingController =
ref.read(pagingControllerProvider(pageKey).notifier);
pagingController.updateItemAt(8, TestItem('11111'));
}),
);
}
PagedChildStatusBuilderDelegate<PagingItem> statusBuilder() {
return PagedChildStatusBuilderDelegate(
newPageErrorIndicatorBuilder: (context, retryCallback) {
return GestureDetector(
onTap: retryCallback,
child: const SizedBox(
height: 40,
child: Center(child: Text('error')),
),
);
},
);
}
MultiItemChildBuilderDelegate itemRegister() {
return MultiItemChildBuilderDelegate(
pageKey,
pagingItemRegister: PagingItemRegister(
itemRegister: {
TestItem: buildTestItem,
},
),
);
}
Widget buildTestItem<T extends PagingItem>(
BuildContext context, T data, int index) {
print('buildTestItem $index');
final testItem = data as TestItem; // 假设 TestItem 是具体的类型
return Text('item $index name${testItem.name}');
}
}
Optimized Rebuilds
This implementation minimizes rebuilds to improve list performance. When the list state changes, only affected items are rebuilt instead of the entire list. This optimization is particularly beneficial for large lists, enhancing both efficiency and smoothness.
Error Handling
If an error occurs while loading data, an error message is displayed along with a “Retry” button.
How to Use
1. Replace the API in paging_provider.dart with your own data source.
2. Customize the data model in the models/ directory to fit your data structure.
3. Adjust the UI in the main screen under screens/ as needed.
Example Demo
Run the project on an emulator or device to see a scrollable list with infinite scrolling functionality. When you reach the end of the list, additional data loads automatically.
Contributing
We welcome PRs and issues to help improve this project.
License
This project is licensed under the MIT License. For details, see the LICENSE file.
This README should give a clear understanding of the project’s purpose, features, and optimizations.