flutter_status_widget 0.0.7
flutter_status_widget: ^0.0.7 copied to clipboard
状态页管理库(Flutter Widget),支持 Loading/Success/Error/Empty 四种状态切换,提供全局默认页面配置、独立页面覆盖与自定义内容回调功能。
example/lib/main.dart
import 'package:example/src/simple_holder.dart';
import 'package:flutter/material.dart';
import 'package:flutter_status_widget/status_widget.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
scInstance
..addBuilder(
Status.loading, ({data}) => const SimpleHolder(text: "加载中..."))
..addBuilder(Status.error, ({data}) => SimpleHolder(text: data))
..addBuilder(Status.empty, ({data}) => SimpleHolder(text: data));
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: '状态页切换示例'),
);
}
}
enum Status {
loading,
error,
empty,
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final StatusController controller = StatusController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Column(
children: [
Expanded(
child: StatusLayout(
controller: controller,
child: ListView.builder(
itemBuilder: (context, index) => Center(
child: Text("$index"),
),
itemCount: 100,
),
),
),
],
),
floatingActionButton: FloatingActionButton.small(
onPressed: changeStatus,
child: const Icon(Icons.change_circle_outlined),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
changeStatus() {
switch (controller.currentStatus) {
case success:
controller.show(Status.loading);
break;
case Status.loading:
controller.show(Status.empty, data: "没有数据");
break;
case Status.empty:
controller.show(Status.error, data: "请求异常");
break;
case Status.error:
controller.showSuccess();
break;
default:
controller.showSuccess();
break;
}
}
}