You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter路由与抽屉动画类型不匹配错误求助

Fixing Flutter Route Animation Type Mismatch Errors

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> with Animation<Offset> in your drawer animation code.
  • Update any tween definitions from Tween<FractionalOffset> to Tween<Offset>, using Offset values instead of the old FractionalOffset syntax:
    // 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 an Offset tween.

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 animation and secondaryAnimation parameters as Animation<double> — this is the standard type the Flutter framework passes to buildTransitions.
  • Use animation.drive() to transform the progress-tracking Animation<double> into an Animation<Offset> that the SlideTransition widget 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:34:07