Flutter路由与抽屉动画类型不匹配错误求助
Let's break down and resolve the two type mismatch issues you're hitting while implementing custom page transitions with Navigator.of(context).pushNamed('/abc'):
1. Drawer Animation: Animation<FractionalOffset> → Animation<Offset> Mismatch
This error comes from FractionalOffset being a deprecated type in modern Flutter releases, fully replaced by Offset. Here's how to fix it:
- Swap all references of
Animation<FractionalOffset>withAnimation<Offset>in your drawer animation code. - Update any tween definitions from
Tween<FractionalOffset>toTween<Offset>, usingOffsetvalues instead of the oldFractionalOffsetsyntax:// Before (deprecated) final tween = Tween<FractionalOffset>( begin: FractionalOffset(1.0, 0.0), end: FractionalOffset.zero, ); // After (current standard) final tween = Tween<Offset>( begin: const Offset(1.0, 0.0), end: Offset.zero, ); - If you're working with an existing
Animation<FractionalOffset>instance, convert it using.drive()to wrap it in anOffsettween.
2. Route Animation: Generic Animation Type Mismatch
The argument_type_not_assignable error occurs when you pass an untyped or mismatched Animation to a component expecting a specific generic type (most often Animation<double> for route transitions).
Looking at your FromRightToLeft route class, here's the corrected implementation:
Fixed FromRightToLeft Route Code
class FromRightToLeft<T> extends MaterialPageRoute<T> { FromRightToLeft({required WidgetBuilder builder, RouteSettings? settings}) : super(builder: builder, settings: settings); @override Widget buildTransitions( BuildContext context, Animation<double> animation, // Explicitly type as Animation<double> Animation<double> secondaryAnimation, Widget child, ) { // Convert the progress animation (0.0 → 1.0) to an offset animation for sliding final offsetAnimation = animation.drive( Tween<Offset>( begin: const Offset(1.0, 0.0), // Start off-screen to the right end: Offset.zero, // End at the screen's default position ).chain(CurveTween(curve: Curves.easeInOut)), ); return SlideTransition( position: offsetAnimation, // Now matches the required Animation<Offset> type child: child, ); } }
Key Fixes Explained:
- Explicitly type the
animationandsecondaryAnimationparameters asAnimation<double>— this is the standard type the Flutter framework passes tobuildTransitions. - Use
animation.drive()to transform the progress-trackingAnimation<double>into anAnimation<Offset>that theSlideTransitionwidget requires.
Quick Additional Tip
To ensure your custom transition applies when using pushNamed, register it via onGenerateRoute in your MaterialApp:
MaterialApp( // ... other config onGenerateRoute: (settings) { if (settings.name == '/abc') { return FromRightToLeft(builder: (context) => const AbcScreen()); } return null; }, );
内容的提问来源于stack exchange,提问作者Nirav Madariya

