ready_view
在使用 Flutter 開發時,通常需要在從網路或資料庫取得資訊時使用非同步操作。在這種情況下,我們需要在頁面上包含載入元件,追蹤載入完成狀態,並在載入完成後顯示原始檢視。包含這一系列程式碼會使檢視程式碼變得相當複雜且混亂。因此,ready_view 庫提供了一種簡單而優雅的解決方案,用於處理非同步動作。
melodysdreamj/ready_view open-source repository details.
以下為英文專案原文快照,最新內容請造訪 GitHub。
When developing with Flutter, it is often necessary to use asynchronous operations when fetching information from networks or databases. In such cases, we need to include loading widgets on our pages, track the completion of loading, and display the original view once the loading is complete. Including this series of code can make the view code quite complex and messy.
Therefore, the ready_view library offers a simple and elegant solution for handling asynchronous actions.
flutter pub add ready_view
// replace
StatelessWidget => ReadyStatelessWidget
build(BuildContext context) => buildWhenReady(BuildContext context)
// replace
extends State<.. => extends ReadyState<..
build(BuildContext context) => buildWhenReady(BuildContext context)
When asynchronous actions are required, you can override the readyState() to perform the necessary tasks.
// handle async operation here if needed
@override
Function? readyState(BuildContext context) {
return () async {
// your async code here
await Future.delayed(const Duration(seconds: 2));
};
}
You can customize the loading widget by overriding the loadingWidget() if you want.
// custom loading widget if needed
@override
Widget loadingWidget() {
return Scaffold(
body: Center(
child: Container(
width: 100,
height: 100,
color: Colors.red,
)));
}
import 'package:flutter/material.dart';
import 'package:ready_view/ready_view.dart';
class StatelessExample extends ReadyStatelessWidget {
const StatelessExample({super.key});
// handle async operation here if needed
@override
Function? readyState(BuildContext context) {
return () async {
await Future.delayed(const Duration(seconds: 2));
};
}
// custom loading widget if needed
// @override
// Widget loadingWidget() {
// return Scaffold(
// body: Center(
// child: Container(
// width: 100,
// height: 100,
// color: Colors.red,
// )));
// }
@override
Widget buildWhenReady(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('StatelessExample'),
),
body: const Center(
child: Text('Content is ready!'),
),
);
}
}
void main() {
runApp(const MaterialApp(
home: StatelessExample(),
));
}
import 'package:flutter/material.dart';
import 'package:ready_view/ready_view.dart';
class StatefulExample extends StatefulWidget {
const StatefulExample({super.key});
@override
ReadyState<StatefulExample> createState() => _StatefulExampleState();
}
class _StatefulExampleState extends ReadyState<StatefulExample> {
// handle async operation here if needed
@override
Function? readyState(BuildContext context) {
return () async {
await Future.delayed(const Duration(seconds: 2));
};
}
// custom loading widget if needed
// @override
// Widget loadingWidget() {
// return Scaffold(
// body: Center(
// child: Container(
// width: 100,
// height: 100,
// color: Colors.red,
// )));
// }
@override
Widget buildWhenReady(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('StatefulExample'),
),
body: const Center(
child: Text('Content is ready!'),
),
);
}
}
void main() {
runApp(const MaterialApp(
home: StatefulExample(),
));
}
Although similar to FutureBuilder, using FutureBuilder asynchronously when the view is first created requires setting up related state, including calling asynchronous functions in initState, and placing the FutureBuilder in the build function along with a separate loading widget. In contrast, ready_view allows you to keep your existing code as it is, while providing a cleaner and more intuitive way to handle asynchronous actions by separately calling the readyState and loadingWidget functions when needed.
先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗