async_list_view 0.0.1-dev.7 copy "async_list_view: ^0.0.1-dev.7" to clipboard
async_list_view: ^0.0.1-dev.7 copied to clipboard

outdated

A wrapper around `StreamSummaryBuilder` and `ListView` that displays a scrollable list of items lazily fetched from an asynchronous data source.

example/lib/main.dart

import 'package:async_list_view/async_list_view.dart';
import 'package:example/mock_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // Number of fruits loaded so far
  int _loadedFruits = 0;

  // Total number of fruits meeting the search criteria
  int _totalFruits = MockDatabase.countMatchingFruits('');
  String _searchString = '';
  Stream<String> _fruitStream = MockDatabase.getFruits('');

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(title: Text('AsyncListView demo!')),
          body: Column(
            children: [
              TextField(
                onChanged: _onTextChanged,
                decoration: InputDecoration(
                  hintText: 'Search Fruits...',
                ),
              ),
              Expanded(
                child: AsyncListView<String>(
                    // If the same stream is passed repeatedly into AsyncListView
                    // AsyncListView will maintain its state and not erroneously
                    // listen to the same stream twice.
                    stream: _fruitStream,
                    itemBuilder: _buildFruitTile,
                    // Display 'loading...' text if the user scrolls past the
                    // currently loaded fruits to let them know they need to wait
                    // for more results.
                    loadingWidget: Text('  loading...',
                        style: TextStyle(
                            fontSize: 20.0,
                            fontWeight: FontWeight.bold,
                            color: Colors.black38)),
                    noResultsWidgetBuilder: (context) {
                      // The `ListView` builder will trample our loaded fruit count.
                      // Reset the count here.
                      Future.delayed(Duration(seconds: 0))
                          .then((value) => setState(() {
                                _loadedFruits = 0;
                              }));
                      return Text(
                          'No fruits found for search term \'$_searchString\'. '
                          'If you feel a fruit has excluded in error, please file a bug report:'
                          '\n\nhttps://github.com/caseycrogers/async_list_view/issues/new?assignees=caseycrogers&labels=high-priority&template=fruit-request-template.md&title=%5BFruit%5D+Add+%3Cinsert-fruit-name-here%3E+to+the+Fruit+List',
                          style: TextStyle(
                              fontSize: 20.0,
                              fontWeight: FontWeight.bold,
                              color: Colors.black38));
                    }),
              ),
            ],
          ),
          // Illustrate that AsyncListView is lazily loading fruits.
          floatingActionButton: FloatingActionButton.extended(
            label: Text('Fruits Loaded: $_loadedFruits/$_totalFruits'),
            onPressed: () {},
          ),
        ),
      ),
    );
  }

  // Take the snapshot of fruits loaded so far and the ListView index to build
  // for and build and return the desired widget corresponding to that index.
  Widget _buildFruitTile(
      BuildContext context, AsyncSnapshot<List<String>> snapshot, int index) {
    Future.delayed(Duration(seconds: 0)).then((value) => setState(() {
          _loadedFruits = snapshot.data?.length ?? 0;
        }));
    return ListTile(
      title: Text(
        snapshot.data?[index] ?? 'Something went wrong!!!',
        style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
      ),
    );
  }

  void _onTextChanged(String newSearchString) {
    // Only update the stream if the search text has changed to avoid expensive
    // duplicate database queries.
    if (_searchString == newSearchString) return;
    _updateWithNewSearchString(newSearchString);
  }

  void _updateWithNewSearchString(String newSearchString) {
    setState(() {
      _searchString = newSearchString;
      _fruitStream = MockDatabase.getFruits(_searchString);
      _totalFruits = MockDatabase.countMatchingFruits(_searchString);
    });
  }
}
5
likes
0
pub points
43%
popularity

Publisher

verified publishercaseycrogers.dev

A wrapper around `StreamSummaryBuilder` and `ListView` that displays a scrollable list of items lazily fetched from an asynchronous data source.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, stream_summary_builder

More

Packages that depend on async_list_view