前言
我们通常都知道程序设计要依赖抽象,提高复用性,做到对扩展开放,对修改关闭。贯彻SOLID
五大原则的最重要法宝就是抽象
和继承
。多态
是一种手段,下面,通过简单 demo 介绍 flutter 开发中常用的最佳实践。
举栗子🌰
/// 不推荐,避免把逻辑放在公共底层处理
class TestWidget extends StatefulWidget {
const TestWidget({Key? key}) : super(key: key);
@override
TestWidgetState createState() => TestWidgetState();
}
class TestWidgetState extends State<TestWidget> {
dynamic param;
Widget childWidget = Container();
///提供给外部调用
void update(dynamic value) {
setState(() {
param = value;
_updateBody();
});
}
void _updateBody(){
if(param == null){
childWidget = Container();
return;
}
switch(param.runtimeType){
case A:
childWidget = AWidget(a: param as A,);
break;
case B:
childWidget = BWidget(b: param as B,);
break;
default:
childWidget = Container();
break;
}
}
@override
Widget build(BuildContext context) {
return Container(
child: childWidget,
);
}
}
如上的写法是不推荐的,应该进行依赖倒置
,将可变的部分放上层处理,保证底层干净,如下:
-
方式一:通过 传参构造器 进行控制反转
typedef ChildBuilder = Widget Function(dynamic param);
class TestWidget extends StatefulWidget {
final ChildBuilder builder;
const TestWidget({Key? key, required this.builder}) : super(key: key);
@override
TestWidgetState createState() => TestWidgetState();
}
class TestWidgetState extends State<TestWidget> {
dynamic param;
void update(dynamic value) {
setState(() {
param = value;
});
}
@override
Widget build(BuildContext context) {
return Container(
child: widget.builder(param),
);
}
}
///外部使用 TestWidget
Widget _builder(dynamic param){
if (param == null) {
return Container();
}
switch (param.runtimeType) {
case A:
return AWidget(
a: param as A,
);
case B:
return BWidget(
b: param as B,
);
default:
return Container();
}
}
return TestWidget(builder: _builder);
-
方式二:通过 继承 + 泛型 进行解耦
/// 将 TestWidget 改成抽象类并指定泛型
abstract class TestWidget<T> extends StatefulWidget {
const TestWidget({Key? key}) : super(key: key);
Widget childBuilder(T param);
@override
TestWidgetState<T> createState() => TestWidgetState<T>();
}
class TestWidgetState<T> extends State<TestWidget> {
T? param;
Widget childWidget = Container();
void update(T value) {
setState(() {
param = value;
});
}
@override
Widget build(BuildContext context) {
return Container(
child: widget.childBuilder(param),
);
}
}
/// 实例A
class ATestWidget extends TestWidget<A> {
const ATestWidget({Key? key}) : super(key: key);
@override
Widget childBuilder(A param) {
return AWidget(a: param);
}
}
/// 实例B
class BTestWidget extends TestWidget<B> {
const BTestWidget({Key? key}) : super(key: key);
@override
Widget childBuilder(B param) {
return BWidget(b: param);
}
}
网友评论