Git Product home page Git Product logo

qs_blocs's Introduction

qs_blocs

QS Blocs

Getting Started

Fetch cubit example

class _ExampleFetchCubit extends FetchCubit<String, AppError> {
  @override
  Future<void> fetch({
    required OnFetchSuccessCallback<String> onSuccess,
    required OnFetchErrorCallback<AppError> onError,
  }) {
    return Future.delayed(
      const Duration(seconds: 3),
          () => onSuccess('Success'),
    );
  }

  @override
  bool isConnectionError(AppError? error) {
    return false;
  }

  @override
  AppError obtainUnknownError() {
    return AppError('Unknown error');
  }
}

Fetch widget usage example

class FetchPage extends StatelessWidget {
  const FetchPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: QsFetchWidget<String, AppError>(
        params: QsFetchWidgetParams<String, AppError>(
          cubitResolver: (context) => _ExampleFetchCubit(),
          loadBuilder: (context, state) =>
          const Center(
            child: CircularProgressIndicator.adaptive(),
          ),
          errorBuilder: (context, state, error) =>
              Center(
                child: Text(error.message),
              ),
          contentBuilder: (context, state) =>
              CustomScrollView(
                slivers: [
                  SliverFillRemaining(
                    child: Center(
                      child: Text('Fetch result: ${state.state.data}'),
                    ),
                  ),
                ],
              ),
        ),
      ),
    );
  }
}

List cubit example

class _ExampleListCubit extends ListCubit<String, AppError> {
  static final _tempData = List.generate(100, (index) => 'Item #$index');

  @override
  Future<void> fetch({
    required OnListSuccessCallback<String> onSuccess,
    required OnListErrorCallback onError,
    String? nextPageToken,
    int limit = kDefaultLimit,
  }) {
    int offset = nextPageToken != null ? int.parse(nextPageToken) : 0;
    final result = _tempData.sublist(offset, offset + kDefaultLimit);
    final hasMore = offset + limit < _tempData.length;
    return Future.delayed(
        const Duration(seconds: 3),
            () =>
            onSuccess(ListCubitDataState(
              data: result,
              hasMore: hasMore,
              nextPageToken: (offset + limit).toString(),
            )));
  }

  @override
  bool isConnectionError(AppError? error) {
    return false;
  }

  @override
  AppError obtainUnknownError() {
    return AppError('Unknown error');
  }
}

List widget usage example

class ListPage extends StatelessWidget {
  const ListPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: QsListWidget<String, AppError>(
        params: QsListWidgetParams<String, AppError>(
          cubitResolver: (context) => _ExampleListCubit(),
          loadBuilder: (context, state) =>
          const SliverFillRemaining(
            child: Center(
              child: CircularProgressIndicator.adaptive(),
            ),
          ),
          errorBuilder: (context, state, error) =>
              SliverFillRemaining(
                child: Center(
                  child: Text(error.message),
                ),
              ),
          contentBuilder: _buildContent,
          wrapperBuilder: (context, state, child) {
            return RefreshIndicator(
              onRefresh: state.requestRefresh,
              child: CustomScrollView(
                slivers: [
                  child,
                ],
              ),
            );
          },
        ),
      ),
    );
  }

  Widget _buildContent(BuildContext context,
      QsListState<String, AppError> state,) {
    return SliverList(
      delegate: SliverChildBuilderDelegate(
            (context, index) {
          if (state.state.hasMore && index == state.state.count) {
            state.requestNextPage();
            return const Center(
              child: CircularProgressIndicator.adaptive(),
            );
          }
          return Container(
            padding: const EdgeInsets.symmetric(
              horizontal: 16,
              vertical: 8,
            ),
            child: Text(state.state.itemAt(index)!),
          );
        },
        childCount: state.state.count + (state.state.hasMore ? 1 : 0),
      ),
    );
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.