Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 4eb24c1

Browse files
author
Yuncong Zhang
committed
[1.5.4] Upgrade some widgets. Fix hero.
1 parent 61eb598 commit 4eb24c1

File tree

6 files changed

+65
-31
lines changed

6 files changed

+65
-31
lines changed

Runtime/material/app.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,19 @@ public override State createState() {
106106

107107

108108
class _MaterialAppState : State<MaterialApp> {
109+
HeroController _heroController;
110+
109111
public override void initState() {
110112
base.initState();
113+
this._heroController = new HeroController(createRectTween: this._createRectTween);
111114
this._updateNavigator();
112115
}
113116

114117
public override void didUpdateWidget(StatefulWidget oldWidget) {
115118
base.didUpdateWidget(oldWidget);
119+
if (this.widget.navigatorKey != (oldWidget as MaterialApp).navigatorKey) {
120+
this._heroController = new HeroController(createRectTween: this._createRectTween);
121+
}
116122
this._updateNavigator();
117123
}
118124

@@ -124,6 +130,7 @@ void _updateNavigator() {
124130
this.widget.onGenerateRoute != null ||
125131
this.widget.onUnknownRoute != null) {
126132
this._navigatorObservers = new List<NavigatorObserver>(this.widget.navigatorObservers);
133+
this._navigatorObservers.Add(this._heroController);
127134
}
128135
else {
129136
this._navigatorObservers = null;

Runtime/widgets/basic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,7 @@ public Listener(
19731973
PointerHoverEventListener onPointerHover = null,
19741974
PointerUpEventListener onPointerUp = null,
19751975
PointerCancelEventListener onPointerCancel = null,
1976+
// TODO: onPointerSignal = null,
19761977
PointerScrollEventListener onPointerScroll = null,
19771978
PointerDragFromEditorEnterEventListener onPointerDragFromEditorEnter = null,
19781979
PointerDragFromEditorHoverEventListener onPointerDragFromEditorHover = null,

Runtime/widgets/dismissible.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Dismissible(
4242
Dictionary<DismissDirection?, float?> dismissThresholds = null,
4343
TimeSpan? movementDuration = null,
4444
float crossAxisEndOffset = 0.0f,
45-
DragStartBehavior dragStartBehavior = DragStartBehavior.down
45+
DragStartBehavior dragStartBehavior = DragStartBehavior.start
4646
) : base(key: key) {
4747
D.assert(key != null);
4848
D.assert(secondaryBackground != null ? background != null : true);

Runtime/widgets/form.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,10 @@ public bool validate() {
217217
return !this.hasError;
218218
}
219219

220-
bool _validate() {
220+
void _validate() {
221221
if (this.widget.validator != null) {
222222
this._errorText = this.widget.validator(this._value);
223223
}
224-
225-
return !this.hasError;
226224
}
227225

228226
public virtual void didChange(T value) {

Runtime/widgets/gesture_detector.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ public GestureDetector(
7171
GestureTapCancelCallback onTapCancel = null,
7272
GestureDoubleTapCallback onDoubleTap = null,
7373
GestureLongPressCallback onLongPress = null,
74+
// TODO: GestureLongPressStartCallback onLongPressStart = null,
75+
// TODO: GestureLongPressMoveUpdate onLongPressMoveUpdate = null,
7476
GestureLongPressUpCallback onLongPressUp = null,
77+
// TODO: GestureLongPressEnd onLongPressEnd = null,
7578
GestureLongPressDragStartCallback onLongPressDragStart = null,
7679
GestureLongPressDragUpdateCallback onLongPressDragUpdate = null,
7780
GestureLongPressDragUpCallback onLongPressDragUp = null,

Runtime/widgets/heroes.cs

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,46 @@ public Hero(
6666
public readonly bool transitionOnUserGestures;
6767

6868
internal static Dictionary<object, _HeroState>
69-
_allHeroesFor(BuildContext context, bool isUserGestureTransition) {
69+
_allHeroesFor(BuildContext context, bool isUserGestureTransition, NavigatorState navigator) {
7070
D.assert(context != null);
71+
D.assert(navigator != null);
7172
Dictionary<object, _HeroState> result = new Dictionary<object, _HeroState> { };
7273

74+
void addHero(StatefulElement hero, object tag) {
75+
D.assert(() => {
76+
if (result.ContainsKey(tag)) {
77+
throw new UIWidgetsError(
78+
"There are multiple heroes that share the same tag within a subtree.\n" +
79+
"Within each subtree for which heroes are to be animated (typically a PageRoute subtree), " +
80+
"each Hero must have a unique non-null tag.\n" +
81+
$"In this case, multiple heroes had the following tag: {tag}\n" +
82+
"Here is the subtree for one of the offending heroes:\n" +
83+
$"{hero.toStringDeep(prefixLineOne: "# ")}"
84+
);
85+
}
86+
87+
return true;
88+
});
89+
_HeroState heroState = (_HeroState) hero.state;
90+
result[tag] = heroState;
91+
}
92+
7393
void visitor(Element element) {
7494
if (element.widget is Hero) {
7595
StatefulElement hero = (StatefulElement) element;
7696
Hero heroWidget = (Hero) element.widget;
7797
if (!isUserGestureTransition || heroWidget.transitionOnUserGestures) {
7898
object tag = heroWidget.tag;
7999
D.assert(tag != null);
80-
D.assert(() => {
81-
if (result.ContainsKey(tag)) {
82-
throw new UIWidgetsError(
83-
"There are multiple heroes that share the same tag within a subtree.\n" +
84-
"Within each subtree for which heroes are to be animated (typically a PageRoute subtree), " +
85-
"each Hero must have a unique non-null tag.\n" +
86-
$"In this case, multiple heroes had the following tag: {tag}\n" +
87-
"Here is the subtree for one of the offending heroes:\n" +
88-
$"{element.toStringDeep(prefixLineOne: "# ")}"
89-
);
100+
if (Navigator.of(hero) == navigator) {
101+
addHero(hero, tag);
102+
}
103+
else {
104+
ModalRoute heroRoute = ModalRoute.of(hero);
105+
if (heroRoute != null && heroRoute is PageRoute && heroRoute.isCurrent) {
106+
addHero(hero, tag);
90107
}
91-
92-
return true;
93-
});
94-
_HeroState heroState = (_HeroState) hero.state;
95-
result[tag] = heroState;
108+
}
96109
}
97110
}
98111

@@ -131,6 +144,8 @@ public void endFlight() {
131144
}
132145

133146
public override Widget build(BuildContext context) {
147+
D.assert(context.ancestorWidgetOfExactType(typeof(Hero)) == null,
148+
() => "A Hero widget cannot be the descendant of another Hero widget.");
134149
if (this._placeholderSize != null) {
135150
if (this.widget.placeholderBuilder == null) {
136151
return new SizedBox(
@@ -163,7 +178,7 @@ public _HeroFlightManifest(
163178
HeroFlightShuttleBuilder shuttleBuilder,
164179
bool isUserGestureTransition
165180
) {
166-
D.assert(this.fromHero.widget.tag == this.toHero.widget.tag);
181+
D.assert(fromHero.widget.tag == toHero.widget.tag);
167182
this.type = type;
168183
this.overlay = overlay;
169184
this.navigatorRect = navigatorRect;
@@ -417,7 +432,7 @@ public override string ToString() {
417432
}
418433

419434
public class HeroController : NavigatorObserver {
420-
HeroController(CreateRectTween createRectTween) {
435+
public HeroController(CreateRectTween createRectTween) {
421436
this.createRectTween = createRectTween;
422437
}
423438

@@ -434,7 +449,16 @@ public override void didPush(Route route, Route previousRoute) {
434449
public override void didPop(Route route, Route previousRoute) {
435450
D.assert(this.navigator != null);
436451
D.assert(route != null);
437-
this._maybeStartHeroTransition(route, previousRoute, HeroFlightDirection.pop, false);
452+
if (!this.navigator.userGestureInProgress) {
453+
this._maybeStartHeroTransition(route, previousRoute, HeroFlightDirection.pop, false);
454+
}
455+
}
456+
457+
public override void didReplace(Route newRoute = null, Route oldRoute = null) {
458+
D.assert(this.navigator != null);
459+
if (newRoute?.isCurrent == true) {
460+
this._maybeStartHeroTransition(oldRoute, newRoute, HeroFlightDirection.push, false);
461+
}
438462
}
439463

440464
public override void didStartUserGesture(Route route, Route previousRoute) {
@@ -496,14 +520,15 @@ bool isUserGestureTransition
496520

497521
Rect navigatorRect = HeroUtils._globalBoundingBoxFor(this.navigator.context);
498522

499-
Dictionary<object, _HeroState>
500-
fromHeroes = Hero._allHeroesFor(from.subtreeContext, isUserGestureTransition);
501-
Dictionary<object, _HeroState> toHeroes = Hero._allHeroesFor(to.subtreeContext, isUserGestureTransition);
523+
Dictionary<object, _HeroState> fromHeroes =
524+
Hero._allHeroesFor(from.subtreeContext, isUserGestureTransition, this.navigator);
525+
Dictionary<object, _HeroState> toHeroes =
526+
Hero._allHeroesFor(to.subtreeContext, isUserGestureTransition, this.navigator);
502527

503528
to.offstage = false;
504529

505530
foreach (object tag in fromHeroes.Keys) {
506-
if (toHeroes[tag] != null) {
531+
if (toHeroes.ContainsKey(tag)) {
507532
HeroFlightShuttleBuilder fromShuttleBuilder = fromHeroes[tag].widget.flightShuttleBuilder;
508533
HeroFlightShuttleBuilder toShuttleBuilder = toHeroes[tag].widget.flightShuttleBuilder;
509534

@@ -521,16 +546,16 @@ bool isUserGestureTransition
521546
isUserGestureTransition: isUserGestureTransition
522547
);
523548

524-
if (this._flights[tag] != null) {
525-
this._flights[tag].divert(manifest);
549+
if (this._flights.TryGetValue(tag, out var result)) {
550+
result.divert(manifest);
526551
}
527552
else {
528553
this._flights[tag] = new _HeroFlight(this._handleFlightEnded);
529554
this._flights[tag].start(manifest);
530555
}
531556
}
532-
else if (this._flights[tag] != null) {
533-
this._flights[tag].abort();
557+
else if (this._flights.TryGetValue(tag, out var result)) {
558+
result.abort();
534559
}
535560
}
536561
}

0 commit comments

Comments
 (0)