Pemrograman Mobile 2
Muhamad Saad Nurul Ishlah, M.Comp.
Dept. Sistem Informasi & Dept. Ilmu Komputer, Universitas Pakuan
https://rive.app/community/3411-7160-safe-icon/
https://medium.com/flutter-community/flutter-animations-comprehensive-guide-cb93b246ca5d
(Implicit Animation)
AnimatedPositioned(
top: selectedItemIndex * itemHeight,
left: 0,
right: 0,
duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut,
child: //...
),
//...
AnimatedContainer(
duration: const Duration(milliseconds: 200),
curve: Curves.easeInOut,
decoration: BoxDecoration(
color: selectedItemIndex == i ? yellow : pink,
border: Border.all(
color: selectedItemIndex == i
? Colors.white
: Colors.transparent,
width: 2,
),
),
child: AnimatedDefaultTextStyle(
duration: const Duration(milliseconds: 200),
style: TextStyle(
color: selectedItemIndex == i
? Colors.black
: Colors.white,
),
child: const Text('Featured!'),
),
),
// dibungkus dalam "InkWell"
onTap: () => setState(() => selectedItemIndex = i),
Daftar Widget AnimatedFoo
AnimatedAlign
AnimatedContainer
AnimatedDefaultTextStyle
AnimatedOpacity
AnimatedPadding
AnimatedPhysicalModel
AnimatedPositioned
AnimatedPositionDirectional
AnimatedSize
begin
dan end
TweenAnimationBuilder(
duration: const Duration(milliseconds: 200),
tween: Tween<double>(begin: 0.01, end: _sliderValue),
child: Container(
decoration: BoxDecoration(
//...
),
child: Slider(
value: _sliderValue,
min: 0.01,
onChanged: (value) {
setState(() => _sliderValue = value);
},
),
),
builder: (BuildContext context, double? value, Widget? child) {
return ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 40 * (value ?? 0.01),
sigmaY: 40 * (value ?? 0.01),
),
child: child,
),
);
},
);
(Explicit Animation)
AnimationController({
double? value,
this.duration,
this.reverseDuration,
this.debugLabel,
this.lowerBound = 0.0,
this.upperBound = 1.0,
this.animationBehavior = AnimationBehavior.normal,
required TickerProvider vsync,
})
Callback Function | Fungsi |
---|---|
forward() | menjalankan animasi secara maju |
reverse() | menjalankan animasi secara mundur |
stop() | menghentikan animasi |
repeat() | mengulang animasi selama terlihat |
reset() | mengembalikan animasi ke nilai lowerBound |
Menset nilai animasi | |
Mengakses beberapa nilai getter untuk mengetahui state dari animasi, seperti: isAnimating, isComplete, isDismissed, dll |
class ExplicitAnimations extends StatefulWidget {
const ExplicitAnimations({Key? key}) : super(key: key);
@override
State<ExplicitAnimations> createState() => _ExplicitAnimationsState();
}
class _ExplicitAnimationsState extends State<ExplicitAnimations>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late final Animation<AlignmentGeometry> _alignAnimation;
late final Animation<double> _rotationAnimation;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(milliseconds: 800),
vsync: this,
)..repeat(reverse: true);
_alignAnimation = Tween<AlignmentGeometry>(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOutCubic,
),
);
_rotationAnimation = Tween<double>(begin: 0, end: 2).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOutCubic,
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlurContainer(
containerHeight: 200,
child: AlignTransition(
alignment: _alignAnimation,
child: RotationTransition(
turns: _rotationAnimation,
child: const Rectangle(
color1: pink,
color2: pinkDark,
width: 50,
height: 50,
),
),
),
);
}
}
Daftar Widget FooTransition
AlignTransition
DecoratedBoxTransition
DefaultTextStyleTransition
FadeTransition
PositionedTransition
RelativePositionedTransition
RotationTransition
ScaleTransition
SizeTransition
SlideTransition
StatusTransitionWidget
Tidak terdapat GradientTransition, tapi bisa dibuat dengan AnimatedBuilder & AnimatedWidget
class AnimatedBuilderExample extends StatefulWidget {
const AnimatedBuilderExample({Key? key}) : super(key: key);
@override
_AnimatedBuilderExampleState createState() => _AnimatedBuilderExampleState();
}
class _AnimatedBuilderExampleState extends State<AnimatedBuilderExample>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
@override
void initState() {
_controller = AnimationController(
duration: const Duration(milliseconds: 500),
vsync: this,
)..repeat(reverse: true);
super.initState();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget? child) {
return Container(
height: 100,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: const [purple, pink, yellow],
stops: [0, _controller.value, 1],
),
borderRadius: BorderRadius.circular(15),
border: Border.all(color: Colors.white),
),
);
},
);
}
}
class GradientTransition extends AnimatedWidget {
final Animation<double> stop;
const GradientTransition({
Key? key,
required this.stop,
}) : super(key: key, listenable: stop);
@override
Widget build(BuildContext context) {
return Container(
height: 100,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: const [purple, pink, yellow],
stops: [0, stop.value, 1],
),
),
);
}
}
// Kemudian gunakan dengan mempassing AnimationController
GradientTransition(stop: _controller),
Jika jawaban untuk semua pertanyaan tersebut adalah ya, maka gunakan Explicit Animation
Roaa. (2022, April 24). Flutter Animations Comprehensive Guide. Medium. Retrieved October 19, 2022, from https://medium.com/flutter-community/flutter-animations-comprehensive-guide-cb93b246ca5d