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

A flutter package to handle listview with refresh, and lazy scrolling smartly

example/lib/main.dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:smart_listview/lazy_refreshing_listview.dart';

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'SmartListview Demo',
      home: LazyPullListviewExample(),
    );
  }
}

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

  @override
  State<LazyPullListviewExample> createState() =>
      _LazyPullListviewExampleState();
}

class _LazyPullListviewExampleState extends State<LazyPullListviewExample> {
  final List<dynamic> items = [];
  int _page = 0;
  final int _limit = 10;
  bool _hasMore = true;

  Future<void> _fetchData({bool isRefresh = false}) async {
    if (isRefresh) {
      _page = 0;
      _hasMore = true;
    } else if (!_hasMore) {
      return;
    }

    final start = _page * _limit;
    final response = await http.get(
      Uri.parse(
        'https://jsonplaceholder.typicode.com/posts?_start=$start&_limit=$_limit',
      ),
    );

    if (response.statusCode == 200) {
      final List<dynamic> newItems = json.decode(response.body);
      setState(() {
        if (isRefresh) {
          items.clear();
        }
        items.addAll(newItems);
        _hasMore = newItems.length == _limit;
        _page++;
      });
    }
  }

  Future<void> _onRefresh() async => _fetchData(isRefresh: true);

  Future<void> _onLazyLoad() async => _fetchData();

  @override
  void initState() {
    super.initState();
    _fetchData(); // initial load
  }

  String limitWords(String text, int wordLimit) {
    final words = text.split(' ');
    String shortened = words.length <= wordLimit
        ? text
        : '${words.take(wordLimit).join(' ')}...';

    return shortened.isNotEmpty
        ? '${shortened[0].toUpperCase()}${shortened.substring(1)}'
        : shortened;
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Lazy Pull ListView Example'), backgroundColor: Colors.grey[200],),
      body: Padding(
        padding: const EdgeInsets.only(left: 10.0, right: 10.0, top: 10.0, bottom: 40.0),
        child: LazyRefreshingListView(
          onRefresh: _onRefresh,
          onLazyLoad: _onLazyLoad,
          listView: ListView.separated(
            itemCount: items.length,
            separatorBuilder: (_, __) => const SizedBox(height: 10.0),
            itemBuilder: (_, index) {
              final item = items[index];
              return ListTile(
                tileColor: Colors.grey[200],
                title: Text(
                  limitWords(item['title'], 5),
                  style: const TextStyle(fontWeight: FontWeight.bold),
                ),
                subtitle: Text(limitWords(item['body'], 10)),
              );
            },
          ),
        ),
      ),
    );
  }
}
0
likes
0
points
28
downloads

Publisher

unverified uploader

Weekly Downloads

A flutter package to handle listview with refresh, and lazy scrolling smartly

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, pull_to_refresh_flutter3

More

Packages that depend on lazy_refreshing_listview