loading_switch 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

loading_switch #

Pub support
Flutter加载切换库

预览 #

如何使用 #

添加依赖 #

dependencies:
  loading_switch: ^0.1.0

如何使用 #

///控制器
final controller = LoadingSwitchController();

LoadingSwitch(
      controller: controller,
      child: child,
      loading: ...,
      error: ...,
      empty: ...,
      option: ...
    );

///显示加载中
controller.showLoading();
    

有如下三种方式设置加载页面

  • 全局设置,在应用初始化时设置
LoadingSwitch.defaultOption
  • 初始化LoadingSwitch时配置option属性
  • 初始化LoadingSwitch时配置loading error等属性
    优先级:
    loading error等 > option > defaultOption

具体设置请参考example

Example #

cd ./example
flutter create .
flutter run

License #

Apache License 2.0

Copyright (c) 2018-2019 codingfd

[0.0.1] - package create #

[0.1.0] - no change,only version update #

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:loading_switch/loading_switch.dart';
import 'page/default_loading_page.dart';
import 'page/default_error_page.dart';
import 'page/default_empty_page.dart';
import 'page/test_page.dart';
import 'page/text_loading_option.dart';

void main() {
  LoadingSwitch.defaultOption = LoadingOption(
      loadingBuilder: (_) => DefaultLoadingPage(),
      errorBuilder: (onWidgetTap) => DefaultErrorPage(
            onWidgetTap: onWidgetTap,
          ),
      emptyBuilder: (_) => DefaultEmptyPage());

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: HomePage());
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("LoadingSwitch Example"),
      ),
      body: ListView(
        children: <Widget>[
          _buildItem(context, "全局配置", 1),
          _buildItem(context, "约定配置", 2),
          _buildItem(context, "单独配置", 3),
          _buildItem(context, "未定义配置", 4),
        ],
      ),
    );
  }

  Widget _buildItem(BuildContext context, String text, int index) => SizedBox(
      width: double.infinity,
      child: FlatButton(
        child: Text(
          text,
        ),
        onPressed: () => {
          Navigator.push(context,
              MaterialPageRoute(builder: (context) => ExamplePage(text, index)))
        },
      ));
}

class ExamplePage extends StatelessWidget {
  final title;
  final index;

  ExamplePage(this.title, this.index);

  @override
  Widget build(BuildContext context) {
    return _buildPage();
  }

  Widget _buildPage() {
    final controller = LoadingSwitchController();
    return Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Column(
          children: <Widget>[
            Expanded(child: _buildLoadingSwitch(controller)),
            Row(
              children: <Widget>[
                _buildButton("加载页面", () => controller.showLoading()),
                _buildButton("错误页面", () => controller.showError()),
                _buildButton("空页面", () => controller.showEmpty()),
                _buildButton("内容页面", () => controller.showContent()),
              ],
            )
          ],
        ));
  }

  LoadingSwitch _buildLoadingSwitch(LoadingSwitchController controller) {
    var child = Container(
        alignment: Alignment.center,
        child: Text(
          "content",
          style: TextStyle(fontSize: 24),
        ));

    switch (index) {
      case 1:

        ///根据全局配置初始化
        return LoadingSwitch(
          controller: controller,
          onWidgetTap: (tag) {
            print("重新加载$tag");
            controller.showLoading();
          },
          child: child,
        );

      case 2:

        ///根据option初始化
        return LoadingSwitch(
          controller: controller,
          onWidgetTap: (tag) {
            print("重新加载$tag");
            controller.showLoading();
          },
          child: child,
          option: TextLoadingOption(),
        );
      case 3:

        ///单独页面配置
        return LoadingSwitch(
          controller: controller,
          onWidgetTap: (tag) {
            print("重新加载$tag");
            controller.showLoading();
          },
          child: child,
          option: TextLoadingOption(),
          loading: DefaultLoadingPage(),
        );
      case 4:

        ///未定义枚举值的页面
        controller.switchStatus("test");
        return LoadingSwitch(
          controller: controller,
          onWidgetTap: (tag) {
            print("重新加载$tag");
            controller.showLoading();
          },
          child: child,
          others: {"test": TestPage()},
        );
    }

    return null;
  }

  Widget _buildButton(String text, VoidCallback onPressed) => Expanded(
          child: FlatButton(
        child: Text(text),
        onPressed: onPressed,
      ));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  loading_switch: ^0.1.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:loading_switch/loading_switch.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [loading_switch] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test