Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Last active November 20, 2022 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/e0e342e0099ef0845ecb5290a0ccae71 to your computer and use it in GitHub Desktop.
Save DaisukeNagata/e0e342e0099ef0845ecb5290a0ccae71 to your computer and use it in GitHub Desktop.
Flutter_Transition_Animation
import 'package:flutter/material.dart';
import 'package:sample_segue/sample_widget.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
late Animation<double> animation;
late AnimationController controller;
GlobalKey key = GlobalKey();
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
animation = Tween<double>(
begin: 0,
end: 0,
).animate(controller);
animation.addListener(() {
if (controller.isAnimating) {
animation = Tween<double>(
begin: 0,
end: MediaQuery.of(context).size.width + 100.0,
).chain(CurveTween(curve: Curves.easeInOut)).animate(controller);
}
});
}
@override
Widget build(BuildContext context) {
return ColoredBox(
color: Colors.white,
child: Stack(
children: [
TextButton(
onPressed: () {
if (key.globalPaintBounds?.center.dx == 50.0) {
_segue();
}
},
child: const Center(
child: Text(
'segue_page1',
style: TextStyle(
color: Colors.black,
),
),
),
),
AnimatedBuilder(
animation: animation,
builder: (context, child) {
return AnimatedContainer(
duration: const Duration(seconds: 1),
transform: Matrix4.translationValues(animation.value, 0, 0),
child: Container(
key: key,
width: 100,
height: 100,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10),
),
),
);
},
),
],
),
);
}
void _segue() {
controller.forward().whenComplete(() {
Navigator.push(
context,
PageRouteBuilder<void>(
settings: const RouteSettings(name: '/lib/sample_widget'),
pageBuilder: (context, animation, secondaryAnimation) =>
const SampleWidget(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
if (animation.isCompleted) {
return FadeTransition(
opacity: animation,
child: child,
);
} else {
return Container();
}
},
),
).then((_) => controller.reset());
});
}
}
extension GlobalKeyEx on GlobalKey {
Rect? get globalPaintBounds {
final renderObject = currentContext?.findRenderObject();
final translation = renderObject?.getTransformTo(null).getTranslation();
if (translation != null && renderObject?.paintBounds != null) {
return renderObject!.paintBounds
.shift(Offset(translation.x, translation.y));
} else {
return null;
}
}
}
@DaisukeNagata
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment