-
Notifications
You must be signed in to change notification settings - Fork 6
/
image_hero.dart
108 lines (99 loc) · 3.57 KB
/
image_hero.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
///
/// [Author] Alex (https://github.com/AlexV525)
/// [Date] 2021-05-07 20:11
///
import 'package:flutter/material.dart';
import 'package:extended_image/extended_image.dart';
class ImageHero extends StatefulWidget {
const ImageHero({
Key key,
@required this.child,
@required this.tag,
@required this.slidePageKey,
this.slideType = SlideType.onlyImage,
}) : super(key: key);
final Widget child;
final SlideType slideType;
final Object tag;
final GlobalKey<ExtendedImageSlidePageState> slidePageKey;
@override
_ImageHeroState createState() => _ImageHeroState();
}
class _ImageHeroState extends State<ImageHero> {
RectTween _rectTween = RectTween(begin: Rect.zero, end: Rect.zero);
@override
Widget build(BuildContext context) {
return Hero(
tag: widget.tag,
createRectTween: (Rect begin, Rect end) {
_rectTween = RectTween(begin: begin, end: end);
return _rectTween;
},
// Make hero better when slides out.
flightShuttleBuilder: (BuildContext flightContext,
Animation<double> animation,
HeroFlightDirection flightDirection,
BuildContext fromHeroContext,
BuildContext toHeroContext) {
// Make hero more smoothly.
final Hero hero = (flightDirection == HeroFlightDirection.pop
? fromHeroContext.widget
: toHeroContext.widget) as Hero;
if (flightDirection == HeroFlightDirection.pop) {
final bool fixTransform = widget.slideType == SlideType.onlyImage &&
(widget.slidePageKey.currentState.offset != Offset.zero ||
widget.slidePageKey.currentState.scale != 1.0);
final Widget toHeroWidget = (toHeroContext.widget as Hero).child;
return AnimatedBuilder(
animation: animation,
builder: (BuildContext buildContext, Widget child) {
Widget animatedBuilderChild = hero.child;
// Make hero more smoothly.
animatedBuilderChild = Stack(
clipBehavior: Clip.antiAlias,
alignment: Alignment.center,
children: <Widget>[
Opacity(
opacity: 1 - animation.value,
child: UnconstrainedBox(
child: SizedBox(
width: _rectTween.begin.width,
height: _rectTween.begin.height,
child: toHeroWidget,
),
),
),
Opacity(
opacity: animation.value,
child: animatedBuilderChild,
),
],
);
// Fix transform when slides out.
if (fixTransform) {
final Tween<Offset> offsetTween = Tween<Offset>(
begin: Offset.zero,
end: widget.slidePageKey.currentState.offset,
);
final Tween<double> scaleTween = Tween<double>(
begin: 1.0,
end: widget.slidePageKey.currentState.scale,
);
animatedBuilderChild = Transform.translate(
offset: offsetTween.evaluate(animation),
child: Transform.scale(
scale: scaleTween.evaluate(animation),
child: animatedBuilderChild,
),
);
}
return animatedBuilderChild;
},
);
}
return hero.child;
},
child: widget.child,
);
}
}