flutter 从下到上导航页面

vd2z7a6w  于 2023-10-22  发布在  Flutter
关注(0)|答案(1)|浏览(181)

我想导航到一个新页面(从页面A到页面B),其中页面B将从底部移动到顶部。

第A页

Navigator.pushNamed(context, InvoiceRoutes.createInvoiceScreen.dart);
  • 酒店
class InvoiceRoutes {
  
  static const createInvoiceScreen = "/createInvoiceScreen";
    
  static MaterialPageRoute<dynamic> _createInvoiceScreenRoute() {
    return MaterialPageRoute(builder: (context) {
      return BlocProvider(
          create: (_) => sl<OwnerInvoiceListCubit>(),
          child: BottomToTopPageRoute(page: PageB()));
    });
  }

  Route<dynamic>? getRoutes(RouteSettings settings) {
    if (settings.name == createInvoiceScreen) {
      return _createInvoiceScreenRoute();
    }

    return null;
  }
}

自下而上的页面路线

import 'package:flutter/material.dart';

class BottomToTopPageRoute<T> extends PageRouteBuilder<T> {
  final Widget page;

  BottomToTopPageRoute({required this.page})
      : super(
          pageBuilder: (context, animation, secondaryAnimation) => page,
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            const begin = Offset(0.0, 1.0);
            const end = Offset.zero;
            const curve = Curves.fastOutSlowIn;

            var tween =
                Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
            var offsetAnimation = animation.drive(tween);

            return SlideTransition(
              position: offsetAnimation,
              child: child,
            );
          },
        );
}

错误

无法将参数类型“BottomToTopPageRoute”分配给参数类型“Widget?'

q9yhzks0

q9yhzks01#

出现此错误是因为您正在使用PageRouteBuilder代替小部件。不需要MaterialPageRoute,因为您正在使用PageRouteBuilder。
将函数_createInvoiceScreenRoute()替换为

static BottomToTopPageRoute<dynamic> _createInvoiceScreenRoute() {
return BottomToTopPageRoute(
    page: BlocProvider(
        create: (_) => sl<OwnerInvoiceListCubit>(), child: const PageB()));}

希望有帮助。

相关问题