flutter sidekick hero-like animations
widgets for creating hero-like animations between two widgets within the same screen.
features
- hero-like animations.
- allow you to specify a different animation for each sidekick.
- widget to manage animations between children of two multi-child widgets.
getting started
in the pubspec.yaml
of your flutter project, add the following dependency:
the latest version is
dependencies:
...
flutter_sidekick: ^latest_version
in your library add the following import:
import 'package:flutter_sidekick/flutter_sidekick.dart';
for help getting started with flutter, view the online documentation.
widgets
sidekick
the sidekick
widget is heavily inspired by the hero widget api.
to link two sidekicks, the targettag
property of the one denoted as the source must be identical to the tag
property of the other one, denoted the target.
then to animate sidekicks, you can use the sidekickcontroller
and one of the move function.
the animation below can be created with the following code:
import 'package:flutter/material.dart';
import 'package:flutter_sidekick/flutter_sidekick.dart';
class simpleexample extends statefulwidget {
@override
_simpleexamplestate createstate() => _simpleexamplestate();
}
class _simpleexamplestate extends state<simpleexample>
with tickerproviderstatemixin {
sidekickcontroller controller;
@override
void initstate() {
super.initstate();
controller =
sidekickcontroller(vsync: this, duration: duration(seconds: 1));
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
@override
widget build(buildcontext context) {
return stack(
children: <widget>[
positioned(
top: 20.0,
left: 20.0,
width: 100.0,
height: 100.0,
child: gesturedetector(
ontap: () => controller.movetotarget(context),
child: sidekick(
tag: 'source',
targettag: 'target',
child: container(
color: colors.blue,
),
),
),
),
positioned(
bottom: 20.0,
right: 20.0,
width: 150.0,
height: 100.0,
child: gesturedetector(
ontap: () => controller.movetosource(context),
child: sidekick(
tag: 'target',
child: container(
color: colors.blue,
),
),
),
),
],
);
}
}
sidekickteambuilder
the sidekickteambuilder
widget can be used to create complex layouts, where widgets from one container can be moved to another one, and you want the transition to be animated:
import 'package:example/widgets/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_sidekick/flutter_sidekick.dart';
class item {
item({
this.id,
});
final int id;
}
class wrapexample extends statelesswidget {
@override
widget build(buildcontext context) {
return sidekickteambuilder<item>(
initialsourcelist: list.generate(20, (i) => item(id: i)),
builder: (context, sourcebuilderdelegates, targetbuilderdelegates) {
return padding(
padding: const edgeinsets.all(8.0),
child: column(
crossaxisalignment: crossaxisalignment.center,
children: <widget>[
sizedbox(
height: 120.0,
child: wrap(
children: sourcebuilderdelegates
.map((builderdelegate) => builderdelegate.build(
context,
wrapitem(builderdelegate.message, true),
animationbuilder: (animation) => curvedanimation(
parent: animation,
curve: curves.ease,
),
))
.tolist(),
),
),
expanded(
child: row(
mainaxisalignment: mainaxisalignment.center,
crossaxisalignment: crossaxisalignment.center,
children: <widget>[
circlebutton(
text: '>',
onpressed: () => sidekickteambuilder.of<item>(context)
.moveall(sidekickflightdirection.totarget),
),
sizedbox(width: 60.0, height: 60.0),
circlebutton(
text: '<',
onpressed: () => sidekickteambuilder.of<item>(context)
.moveall(sidekickflightdirection.tosource),
),
],
),
),
sizedbox(
height: 250.0,
child: wrap(
children: targetbuilderdelegates
.map((builderdelegate) => builderdelegate.build(
context,
wrapitem(builderdelegate.message, false),
animationbuilder: (animation) => curvedanimation(
parent: animation,
curve: flippedcurve(curves.ease),
),
))
.tolist(),
),
)
],
),
);
},
);
}
}
class wrapitem extends statelesswidget {
const wrapitem(
this.item,
this.issource,
) : size = issource ? 40.0 : 50.0;
final bool issource;
final double size;
final item item;
@override
widget build(buildcontext context) {
return gesturedetector(
ontap: () => sidekickteambuilder.of<item>(context).move(item),
child: padding(
padding: const edgeinsets.all(2.0),
child: container(
height: size - 4,
width: size - 4,
color: _getcolor(item.id),
),
),
);
}
color _getcolor(int index) {
switch (index % 4) {
case 0:
return colors.blue;
case 1:
return colors.green;
case 2:
return colors.yellow;
case 3:
return colors.red;
}
return colors.indigo;
}
}
Comments are closed.