pagist 0.0.1 copy "pagist: ^0.0.1" to clipboard
pagist: ^0.0.1 copied to clipboard

A modern Flutter pagination package that provides efficient data loading with customizable UI components for lists and grids.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:pagist/pagist.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Pagist Example',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: UserListPage(),
    );
  }
}

// Model class for our example
class User {
  final int id;
  final String name;
  final String email;

  User({required this.id, required this.name, required this.email});
}

// Custom PagingSource implementation
class UserPagingSource extends PagingSource<int, User> {
  @override
  Future<LoadResult<int, User>> load(LoadParams<int> params) async {
    try {
      // Simulate API delay
      await Future.delayed(Duration(milliseconds: 500));

      final page = params.key ?? 1;
      final pageSize = params.loadSize;

      // Simulate fetching data from an API
      final users = await _fetchUsers(page, pageSize);

      // Simulate end of data after page 10
      final nextKey = page < 10 ? page + 1 : null;

      return LoadResult<int, User>(data: users, nextKey: nextKey);
    } catch (e) {
      throw Exception('Failed to load users: $e');
    }
  }

  // Mock API call
  Future<List<User>> _fetchUsers(int page, int pageSize) async {
    final startIndex = (page - 1) * pageSize;

    return List.generate(pageSize, (index) {
      final id = startIndex + index + 1;
      return User(id: id, name: 'User $id', email: 'user$id@example.com');
    });
  }
}

class UserListPage extends StatefulWidget {
  const UserListPage({super.key});

  @override
  UserListPageState createState() => UserListPageState();
}

class UserListPageState extends State<UserListPage> {
  late PaginationController<int, User> _controller;

  @override
  void initState() {
    super.initState();

    // Initialize the pagination controller
    _controller = PaginationController<int, User>(
      pagingSource: UserPagingSource(),
      pageSize: 10,
      initialKey: 1,
    );

    // Load initial data
    _controller.loadMore();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Pagist Example')),
      body: PaginatedListView<User>(
        controller: _controller,
        enableRefresh: true,
        itemBuilder: (context, user, index) {
          return ListTile(
            leading: CircleAvatar(child: Text(user.id.toString())),
            title: Text(user.name),
            subtitle: Text(user.email),
          );
        },
        loadingWidget: Center(child: CircularProgressIndicator()),
        errorWidget: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(Icons.error, size: 64, color: Colors.red),
              SizedBox(height: 16),
              Text('Failed to load users'),
              SizedBox(height: 16),
              ElevatedButton(
                onPressed: () => _controller.refresh(),
                child: Text('Retry'),
              ),
            ],
          ),
        ),
        emptyWidget: Center(child: Text('No users found')),
        loadingMoreWidget: Padding(
          padding: EdgeInsets.all(16),
          child: Center(child: CircularProgressIndicator()),
        ),
      ),
    );
  }
}
1
likes
140
points
7
downloads

Publisher

unverified uploader

Weekly Downloads

A modern Flutter pagination package that provides efficient data loading with customizable UI components for lists and grids.

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on pagist