xxxxxxxxxx
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() => HomeState();
}
class HomeState extends State<Home> with SingleTickerProviderStateMixin {
AnimationController controller;
Animation<Offset> offset;
@override
void initState() {
super.initState();
controller =
AnimationController(vsync: this, duration: Duration(seconds: 1));
offset = Tween<Offset>(begin: Offset.zero, end: Offset(0.0, 1.0))
.animate(controller);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Center(
child: RaisedButton(
child: Text('Show / Hide'),
onPressed: () {
switch (controller.status) {
case AnimationStatus.completed:
controller.reverse();
break;
case AnimationStatus.dismissed:
controller.forward();
break;
default:
}
},
),
),
Align(
alignment: Alignment.bottomCenter,
child: SlideTransition(
position: offset,
child: Padding(
padding: EdgeInsets.all(50.0),
child: CircularProgressIndicator(),
),
),
)
],
),
);
}
}
xxxxxxxxxx
import 'package:flutter/material.dart';
class SlideInAnimation extends StatefulWidget {
final Widget child;
final Duration duration;
SlideInAnimation({required this.child, this.duration = const Duration(milliseconds: 500)});
@override
_SlideInAnimationState createState() => _SlideInAnimationState();
}
class _SlideInAnimationState extends State<SlideInAnimation> with SingleTickerProviderStateMixin {
late AnimationController _animationController;
late Animation<Offset> _animation;
@override
void initState() {
super.initState();
_animationController = AnimationController(vsync: this, duration: widget.duration);
_animation = Tween<Offset>(begin: Offset(1.0, 0.0), end: Offset.zero)
.animate(CurvedAnimation(parent: _animationController, curve: Curves.easeInOut));
_animationController.forward();
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SlideTransition(
position: _animation,
child: widget.child,
);
}
}