Flutter核心功能扩展插件flexus_core的使用
Flutter核心功能扩展插件flexus_core的使用
本项目是Flutter应用程序的起点。
这是一个开源框架,用于使用Flutter创建应用程序。此框架可用于为Android和iOS平台创建应用程序。
特性
此包包含用于创建应用程序的通用功能。
使用的库
- get
- logger
- lifecycle
- sizer
- loading overlay
- device preview
许可证 (MIT)
版权所有 © 2021 Chatura Dilan Perera
在此授权下,任何获得软件副本的人可以免费获得该软件及其相关文档文件(“软件”)的副本,不受限制地处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件的副本,并允许被提供软件的人这样做,但须符合以下条件:
上述版权声明和本许可声明必须包含在所有副本或软件的重要部分中。
软件按“原样”提供,不附带任何形式的明示或暗示保证,包括但不限于适销性、适用于特定用途和非侵权的保证。在任何情况下,作者或版权持有人均不对因软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任负责。
支持
如需支持和示例应用,请联系 dilan@dilan.me
完整示例Demo
以下是一个完整的示例,展示如何使用flexus_core
插件。
import 'package:flutter/material.dart';
import 'package:get/get.dart'; // 使用Get库
import 'package:logger/logger.dart'; // 使用Logger库
import 'package:lifecycle/lifecycle.dart'; // 使用Lifecycle库
import 'package:sizer/sizer.dart'; // 使用Sizer库
import 'package:loading_overlay/loading_overlay.dart'; // 使用LoadingOverlay库
import 'package:device_preview/device_preview.dart'; // 使用DevicePreview库
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flexus Core Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
builder: DevicePreview.appBuilder, // 使用DevicePreview
);
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver { // 使用WidgetsBindingObserver
final logger = Logger(); // 初始化Logger
bool isLoading = false; // 控制加载状态
[@override](/user/override)
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this); // 添加观察者
}
[@override](/user/override)
void dispose() {
WidgetsBinding.instance.removeObserver(this); // 移除观察者
super.dispose();
}
[@override](/user/override)
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
logger.i("App resumed"); // 打印日志
break;
case AppLifecycleState.inactive:
logger.i("App inactive"); // 打印日志
break;
case AppLifecycleState.paused:
logger.i("App paused"); // 打印日志
break;
case AppLifecycleState.detached:
logger.i("App detached"); // 打印日志
break;
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flexus Core Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
LoadingOverlay(
isLoading: isLoading,
progressIndicator: CircularProgressIndicator(), // 加载指示器
child: ElevatedButton(
onPressed: () {
setState(() {
isLoading = true;
});
Future.delayed(Duration(seconds: 2), () {
setState(() {
isLoading = false;
});
});
},
child: Text('Press Me!'),
),
),
],
),
),
);
}
}
更多关于Flutter核心功能扩展插件flexus_core的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter核心功能扩展插件flexus_core的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flexus_core
是一个用于扩展 Flutter 核心功能的插件,提供了许多有用的工具和功能,帮助开发者更高效地构建 Flutter 应用。以下是关于 flexus_core
插件的基本使用方法和一些核心功能的介绍。
1. 安装 flexus_core
插件
首先,你需要在 pubspec.yaml
文件中添加 flexus_core
依赖:
dependencies:
flutter:
sdk: flutter
flexus_core: ^1.0.0 # 请使用最新版本
然后,运行 flutter pub get
来安装依赖。
2. 导入 flexus_core
在你的 Dart 文件中导入 flexus_core
:
import 'package:flexus_core/flexus_core.dart';
3. 使用 flexus_core
提供的功能
flexus_core
提供了多种功能,以下是一些常见的用法:
3.1 扩展的 Widgets
flexus_core
提供了一些扩展的 Widget,可以帮助你更快速地构建 UI。
FlexusContainer(
child: Text('This is a Flexus Container'),
padding: EdgeInsets.all(16.0),
margin: EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8.0),
),
);
3.2 工具类
flexus_core
提供了一些工具类,例如 FlexusUtils
,可以帮助你处理常见的任务。
String formattedDate = FlexusUtils.formatDate(DateTime.now());
print('Formatted Date: $formattedDate');
3.3 状态管理
flexus_core
提供了一些状态管理的工具,例如 FlexusState
,可以帮助你更轻松地管理应用的状态。
class MyAppState extends FlexusState<MyApp> {
int _counter = 0;
void incrementCounter() {
setState(() {
_counter++;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flexus Core Example'),
),
body: Center(
child: Text('Counter: $_counter'),
),
floatingActionButton: FloatingActionButton(
onPressed: incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
3.4 网络请求
flexus_core
提供了一个简单的网络请求工具 FlexusHttp
,可以帮助你执行 HTTP 请求。
Future<void> fetchData() async {
var response = await FlexusHttp.get('https://jsonplaceholder.typicode.com/posts');
if (response.statusCode == 200) {
print('Response data: ${response.body}');
} else {
print('Request failed with status: ${response.statusCode}');
}
}
4. 其他功能
flexus_core
还提供了其他一些功能,例如主题管理、本地存储、路由管理等。你可以根据项目需求选择使用这些功能。
5. 示例代码
以下是一个简单的示例,展示了如何使用 flexus_core
的一些核心功能:
import 'package:flutter/material.dart';
import 'package:flexus_core/flexus_core.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flexus Core Example',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends FlexusState<MyHomePage> {
int _counter = 0;
void incrementCounter() {
setState(() {
_counter++;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flexus Core Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
FlexusContainer(
child: Text('This is a Flexus Container'),
padding: EdgeInsets.all(16.0),
margin: EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8.0),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}