Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">Local Hero</string>

</resources>
103 changes: 82 additions & 21 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class _LocalHeroPlayground extends StatelessWidget {
appBar: AppBar(
title: const TabBar(
tabs: <Widget>[
Text('Simple example'),
Text('Animate wrap reordering'),
Text('Move between containers'),
Text('Draggable content'),
Expand All @@ -49,6 +50,7 @@ class _LocalHeroPlayground extends StatelessWidget {
body: const SafeArea(
child: TabBarView(
children: <Widget>[
_SimpleExample(),
_WrapReorderingAnimation(),
_AcrossContainersAnimation(),
_DraggableExample(),
Expand Down Expand Up @@ -82,16 +84,19 @@ class _Tile extends StatelessWidget {
required this.model,
required this.size,
this.onTap,
this.onAnimationEnd,
}) : super(key: key);

final _TileModel model;
final VoidCallback? onTap;
final VoidCallback? onAnimationEnd;
final double size;

@override
Widget build(BuildContext context) {
return LocalHero(
tag: model.text!,
onAnimationEnd: onAnimationEnd,
child: GestureDetector(
onTap: onTap,
child: _RawTile(
Expand Down Expand Up @@ -131,14 +136,63 @@ class _RawTile extends StatelessWidget {
}
}

class _SimpleExample extends StatefulWidget {
const _SimpleExample({
Key? key,
}) : super(key: key);

@override
State<_SimpleExample> createState() => _SimpleExampleState();
}

class _SimpleExampleState extends State<_SimpleExample> {
var alignment = Alignment.bottomLeft;

@override
Widget build(BuildContext context) {
return Stack(
children: [
Align(
alignment: alignment,
child: LocalHero(
tag: 'unique',
onAnimationEnd: () {
print('ANIMATION ENDED');
},
child: GestureDetector(
onTap: () {
setState(() {
if (alignment == Alignment.topRight) {
setState(() {
alignment = Alignment.bottomLeft;
});
} else {
setState(() {
alignment = Alignment.topRight;
});
}
});
},
child: Container(
height: 50,
width: 50,
color: Colors.red,
),
),
),
)
],
);
}
}

class _WrapReorderingAnimation extends StatefulWidget {
const _WrapReorderingAnimation({
Key? key,
}) : super(key: key);

@override
_WrapReorderingAnimationState createState() =>
_WrapReorderingAnimationState();
_WrapReorderingAnimationState createState() => _WrapReorderingAnimationState();
}

class _WrapReorderingAnimationState extends State<_WrapReorderingAnimation> {
Expand Down Expand Up @@ -171,6 +225,9 @@ class _WrapReorderingAnimationState extends State<_WrapReorderingAnimation> {
...tiles.map(
(tile) => _Tile(
key: ValueKey(tile),
onAnimationEnd: tile.text == '1' ? (){
print('TILE ${tile.text} ENDED');
} : null,
size: 80,
model: tile,
onTap: () {
Expand Down Expand Up @@ -212,12 +269,10 @@ class _AcrossContainersAnimation extends StatefulWidget {
}) : super(key: key);

@override
_AcrossContainersAnimationState createState() =>
_AcrossContainersAnimationState();
_AcrossContainersAnimationState createState() => _AcrossContainersAnimationState();
}

class _AcrossContainersAnimationState
extends State<_AcrossContainersAnimation> {
class _AcrossContainersAnimationState extends State<_AcrossContainersAnimation> {
final List<_TileModel> rowTiles = <_TileModel>[];
final List<_TileModel> colTiles = <_TileModel>[];

Expand All @@ -239,25 +294,31 @@ class _AcrossContainersAnimationState
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(4),
child: Row(
children: <Widget>[
...rowTiles.map(
(tile) => _Tile(
key: ValueKey(tile),
model: tile,
size: 80,
onTap: () {
setState(() {
colTiles.add(tile);
rowTiles.remove(tile);
});
},
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: <Widget>[
...rowTiles.map(
(tile) => _Tile(
key: ValueKey(tile),
model: tile,
size: 80,
onTap: () {
setState(
() {
colTiles.add(tile);
rowTiles.remove(tile);
},
);
},
),
),
),
],
],
),
),
),
const SizedBox(height: 10),
Expand Down
11 changes: 9 additions & 2 deletions lib/src/widgets/local_hero.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class LocalHero extends StatefulWidget {
this.flightShuttleBuilder,
this.enabled = true,
required this.child,
this.onAnimationEnd,
}) : super(key: key);

/// The identifier for this particular local hero. This tag must be unique
Expand All @@ -53,12 +54,13 @@ class LocalHero extends StatefulWidget {
/// {@macro flutter.widgets.child}
final Widget child;

final VoidCallback? onAnimationEnd;

@override
_LocalHeroState createState() => _LocalHeroState();
}

class _LocalHeroState extends State<LocalHero>
with SingleTickerProviderStateMixin<LocalHero> {
class _LocalHeroState extends State<LocalHero> with SingleTickerProviderStateMixin<LocalHero> {
late LocalHeroController controller;
late LocalHeroScopeState scopeState;

Expand All @@ -67,6 +69,11 @@ class _LocalHeroState extends State<LocalHero>
super.initState();
scopeState = context.getLocalHeroScopeState();
controller = scopeState.track(context, widget);
controller.addListener(() {
if(controller.tag == widget.tag && controller.isAnimating == false){
widget.onAnimationEnd?.call();
}
});
}

@override
Expand Down