Giter Club home page Giter Club logo

flutter_state_widget's Introduction

pub package

StateWidget

Chinese Document: README

The StateWidget component is a generic state layout component that can display different layouts based on various states (such as loading, load failure, empty data, etc.).

Usage

To use the StateWidget component, the following parameters need to be provided:

  • stateType: The type of state layout, enumerated as StateType, including the following values:
    • loading: Loading state
    • empty: Empty data state
    • error: Load failure state
    • success: Content display state
  • message: When the state layout type is empty or error, a text message can be passed to describe the specific state information.
  • onRetry: When the state layout type is error, a callback function can be passed to retry loading the data.
  • child: When the state layout type is success, a child component needs to be passed to display the content.

Example code:

//1. Direct usage
    StateWidget(
      stateType: stateType,
      loadingWidgetBuilder: (_, __) => const Text('Loading'),
      emptyWidgetBuilder: (message, __) => const Text('Empty'),
      errorWidgetBuilder: (message, onRetry) => TextButton(
        onPressed: onRetry,
        child: const Text("Click to Retry"),
      ),
      onRetry: () {
        //retry
        setState(() {
          stateType = StateType.loading;
        });
      },
      message: "Prompt message",
      child: const Text('Hello World'),
    );

//2. Using the extension function .state to build
     Text('Hello World').state(
        stateType: stateType,
        loadingWidgetBuilder: (_, __) => const Text('Loading'),
        emptyWidgetBuilder: (message, __) => const Text('Empty'),
        errorWidgetBuilder: (message, onRetry) => TextButton(
              onPressed: onRetry,
              child: const Text("Click to Retry"),
            ),
        onRetry: () {
          //retry
          setState(() {
            stateType = StateType.loading;
          });
        },
        message: "Prompt message");

Global Configuration

The global configuration of the StateWidget component can be set as follows:

// Global initialization of state layout
    StateWidget.config(
        errorWidgetBuilder: (String? message, VoidCallback? onRetry) => Column(
              children: [
                Text(message ?? "Error"),
                TextButton(onPressed: onRetry, child: const Text("Click to Retry"))
              ],
            ),
        emptyWidgetBuilder: (String? message, VoidCallback? onRetry) =>
            Text(message ?? "Empty"),
        loadingWidgetBuilder: (String? message, VoidCallback? onRetry) =>
            Text(message ?? "Loading"));

The StateWidgetConfig is a configuration class that includes the following parameters:

  • emptyWidgetBuilder: Custom component for building the empty data state.
  • errorWidgetBuilder: Custom component for building the load failure state.
  • loadingWidgetBuilder: Custom component for building the loading state.

Configuration Priority

Local configuration > Global configuration

flutter_state_widget's People

Stargazers

Kale avatar

Watchers

Kale avatar

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.