Flutter核心功能扩展插件zam_core的使用
Flutter核心功能扩展插件zam_core的使用
Zam Core库
Zam Core库是由zamstation开发的所有包的核心库。
包含的组件
详细了解这些组件请查看这里。
如何使用
ParameterizedBuilder
ParameterizedBuilder
可以用于构建具有特定类型的对象。
final ParameterizedBuilder<Type, Car> carBuilder;
// ...
final car = carBuilder(HondaCivic);
ParameterizedCallback
ParameterizedCallback
是一个可以处理不同类型参数的回调函数。
final ParameterizedCallback<double, int> roundOffStrategy;
// ...
final price = roundOffStrategy(8.458);
NamedException
NamedException
是对内置 Exception
类的包装,提供了更详细的异常信息,如严重程度和解决方案。
构造简单问题:
final exception = NamedException.create('Provided value is -26 which is negative.');
或者构建更详细的异常信息:
final exception = NamedException.create(
'Provided value is -26 which is negative.',
solution: 'Please provide a positive value.',
severity: ExceptionSeverity.critical,
);
Cloneable
Cloneable
接口允许对象克隆自身。
[@immutable](/user/immutable)
class Triangle implements Cloneable<Triangle> {
final double base;
final double height;
const Triangle(this.base, this.height);
[@override](/user/override)
Triangle clone() {
return Triangle(this.base, this.height);
}
}
Model
Model
类是一个不可变的数据模型,通常用于表示应用中的数据实体。
[@immutable](/user/immutable)
class BmiModel extends Model {
final double weight;
final double height;
final double value;
[@override](/user/override)
get props => [weight, height];
const BmiModel(this.weight, this.height) : value = weight / (height * height);
}
ViewModel
ViewModel
类是UI逻辑的抽象,通常与视图层绑定。
[@immutable](/user/immutable)
class HeightViewModel extends ViewModel {
final double value;
[@override](/user/override)
get props => [value];
const HeightViewModel(this.value);
}
Entity
Entity
类用于持久化和表示应用中的数据实体。
[@immutable](/user/immutable)
class BmiEntity extends Entity<BmiModel> {
[@override](/user/override)
final String key = '';
final double weight;
final double height;
[@override](/user/override)
get props => [weight, height];
const BmiEntity({
required this.weight,
required this.height,
});
BmiEntity.fromJson(Json json)
: this(
weight: json['weight'] as double,
height: json['height'] as double,
);
BmiEntity.fromModel(BmiModel model)
: this(
weight: model.weight,
height: model.height,
);
[@override](/user/override)
Json toJson() {
return {
'key': this.key,
'weight': this.weight,
'height': this.height,
};
}
[@override](/user/override)
BmiModel toModel() {
return BmiModel(this.weight, this.height);
}
}
要了解更多内容,请移步到示例部分,或者查看GitHub上的示例。
状态
贡献者
许可证
完整示例Demo
import 'package:zam_core/zam_core.dart';
void main() {
//
// Cloneable
//
final triangle = Triangle(6, 4);
final clonedTriangle = triangle.clone();
assert(triangle != clonedTriangle);
//
// Exceptions
//
final emptyException = NamedException.empty();
print(emptyException);
// EmptyException has occured.
// > Problem: <none>
// > Solution: <none>
final unnamedException = NamedException.create(
'Provided value is -26 which is negative.',
);
print(unnamedException);
// UnnamedException has occured.
// > Problem: Provided value is -26 which is negative.
// > Solution: <none>
final heightException = NamedException.create(
'Provided value is -26 which is negative.',
solution: 'Provide a positive value.',
name: 'Height Exception',
);
print(heightException);
// Height Exception has occured.
// > Problem: Provided value is -26 which is negative.
// > Solution: Provide a positive value.
}
[@immutable](/user/immutable)
class Triangle implements Cloneable<Triangle> {
final double base;
final double height;
const Triangle(this.base, this.height);
[@override](/user/override)
Triangle clone() {
return Triangle(base, height);
}
}
[@immutable](/user/immutable)
class BmiModel extends Model {
final double weight;
final double height;
final double value;
[@override](/user/override)
get props => [weight, height];
const BmiModel(this.weight, this.height) : value = weight / (height * height);
}
[@immutable](/user/immutable)
class HeightViewModel extends ViewModel {
final double value;
[@override](/user/override)
get props => [value];
const HeightViewModel(this.value);
}
[@immutable](/user/immutable)
class BmiEntity extends Entity<BmiModel> {
[@override](/user/override)
final String key = '';
final double weight;
final double height;
[@override](/user/override)
get props => [weight, height];
const BmiEntity({
required this.weight,
required this.height,
});
BmiEntity.fromJson(Json json)
: this(
weight: json['weight'] as double,
height: json['height'] as double,
);
BmiEntity.fromModel(BmiModel model)
: this(
weight: model.weight,
height: model.height,
);
[@override](/user/override)
Json toJson() {
return {
'key': key,
'weight': weight,
'height': height,
};
}
[@override](/user/override)
BmiModel toModel() {
return BmiModel(weight, height);
}
}
更多关于Flutter核心功能扩展插件zam_core的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter核心功能扩展插件zam_core的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
zam_core
是 Flutter 中的一个核心功能扩展插件,旨在为开发者提供一些常用的工具和功能,以简化开发流程并提高代码的可维护性。以下是一些常见的功能和使用方法。
1. 安装 zam_core
首先,你需要在 pubspec.yaml
文件中添加 zam_core
依赖:
dependencies:
flutter:
sdk: flutter
zam_core: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 常用功能
2.1 状态管理
zam_core
提供了一种轻量级的状态管理工具,可以帮助你简化状态管理逻辑。
import 'package:zam_core/zam_core.dart';
class CounterState with StateMixin {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
}
class CounterPage extends StatelessWidget {
final CounterState _state = CounterState();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Counter')),
body: Center(
child: StateBuilder<CounterState>(
state: _state,
builder: (context, state) {
return Text('Count: ${state.count}');
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _state.increment,
child: Icon(Icons.add),
),
);
}
}
2.2 路由管理
zam_core
提供了简单的路由管理工具,可以帮助你更方便地导航到不同的页面。
import 'package:zam_core/zam_core.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: NavigationService.navigatorKey,
onGenerateRoute: NavigationService.generateRoute,
initialRoute: '/',
routes: {
'/': (context) => HomePage(),
'/details': (context) => DetailsPage(),
},
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () {
NavigationService.navigateTo('/details');
},
child: Text('Go to Details'),
),
),
);
}
}
class DetailsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Details')),
body: Center(
child: ElevatedButton(
onPressed: () {
NavigationService.goBack();
},
child: Text('Go Back'),
),
),
);
}
}
2.3 网络请求
zam_core
提供了一个简单的网络请求工具,可以帮助你快速发起 HTTP 请求。
import 'package:zam_core/zam_core.dart';
void fetchData() async {
final response = await HttpService.get('https://jsonplaceholder.typicode.com/posts');
if (response.statusCode == 200) {
print('Data: ${response.body}');
} else {
print('Failed to load data');
}
}
2.4 日志记录
zam_core
提供了一个日志记录工具,可以帮助你更方便地记录日志。
import 'package:zam_core/zam_core.dart';
void logMessage() {
Logger.info('This is an info message');
Logger.error('This is an error message');
Logger.debug('This is a debug message');
}