So basically I have been trying to achieve an animation. Also using flutter_animate 4.2.0+1 package.
First let me show you the code:
in auth_page.dart
file:
class AuthPage extends StatefulWidget {
const AuthPage({super.key});
@override
State<AuthPage> createState() => _AuthPageState();
}
class _AuthPageState extends State<AuthPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
...
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
////SEE HERE////
AuthBox().animate(target: authBoxTarget ? 1 : 0).slideY(
delay: Duration(milliseconds: 500),
duration: Duration(milliseconds: 500),
begin: 0,
end: 1,
),
///////////
],
),
),
),
);
}
}
In the authbox.dart
file for the button: the onTap
function:
onTap: () {
setState(() {
authBoxTarget = true;
});
Future.delayed(
Duration(milliseconds: 100),
() {
showGeneralDialog(
...
transitionBuilder: ... );
return SlideTransition(
position: tween.animate(...),
child: child,
);
},
pageBuilder: (context, _, __) {
return Center(
child: Container(...
child: Center(
child: Text(
'Login',
style: TextStyle(...),
),
),
),
);
},
).then((_) {
setState(() {
authBoxTarget = false;
});
});
},
);
},
This should work right?
-when the dialogue appears the authbox should slide down and on dismissed the authbox should come back up.
But this is not hapenning the dialogue is coming but authbox is not going down…why??? give me solution.
mail.dart
file:
void main() {
runApp(
MyApp(),
);
Animate.restartOnHotReload = true;
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
home: const AuthPage(),
);
}
}
2
Answers
If you want to run both animations at once, you can declare a new bool in your code, for example:
Then, in your onTap handler, you add this at the beginning:
After your showGeneralDialog you add this:
And this is the code for your Animate Widget with the authbox inside:
Simple solution, but it works.
If you have any more questions, you can ask them in the comments.
Add a callback to
AuthBox
to toggle the animation state:Receive the callback in
AuthBox
:Use
WillPopScope
in the dialog to handle dismisses(and remove your .then() on the dialog):