常见设计模式 例如
工厂模式: 简单工厂模式, 例如编辑器里面有多种模板,H5、名片、海报, 可以通过模板工厂来创建不同的产品
策略模式: 要写一个动画库,让某个控件可以支持多个控件,有一个抽象策略, 一个具体策略,一个执行环境, 具体策略就是要实例化
main(){
CommonAnimation _commonAnimation;
switch (widget._animationType) {
case AnimationType.OpacityAndTransform:
_commonAnimation = OpacityAndTransform(widget._dsmView, _animationValue);
break;
case AnimationType.OpacityAndTransform:
_commonAnimation = OpacityAndTransform(widget._dsmView, _animationValue);
break;
}
return _commonAnimation.build();
}
///通用的动画
abstract class CommonAnimation {
Widget child;
double animationValue = 0.0;
CommonAnimation(this.child, this.animationValue);
///通过build 创建视图
Widget build();
}
///透明并且移动
class OpacityIn extends CommonAnimation {
OpacityIn(child, animationValue) : super(child, animationValue);
@override
Widget build() {
return Opacity(
opacity: animationValue > 1 ? 1 : animationValue,
child: child,
);
}
}
///透明并且移动
class OpacityAndTransform extends CommonAnimation {
OpacityAndTransform(child, animationValue) : super(child, animationValue);
@override
Widget build() {
return Opacity(
opacity: animationValue > 1 ? 1 : animationValue,
child: Transform.translate(
offset: Offset((1 - animationValue) * 60, 0),
child: child,
),
);
}
}
网友评论