简体   繁体   中英

How to pass data from one class to another class in flutter using PageRouteBuilder

I want to pass data from page1 to page2 using PageRouteBuilder(for animation matters), problem is how to pass it. I don't to sacrifice my animation by using MaterialPageRoute().

/// My button in page 1
Listview.builder(
...
...
...
 onTap: () {
    Navigator.of(context).push(_createRouteSelectType(12)); //data i want to pass
 },
),

/// My PageRouteBuilder

Route _createRouteSelectType(buCode) {
  return PageRouteBuilder(
    pageBuilder: (context, animation, secondaryAnimation) => page2(buCode),
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      var begin = Offset(0.0, 1.0);
      var end = Offset.zero;
      var curve = Curves.decelerate;
      var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
      return SlideTransition(
        position: animation.drive(tween),
        child: child,
      );
    },
  );
}

/// My page 2 class / receiver
import 'package:flutter/material.dart';
class page2extends StatefulWidget {
  final int buCode;

  page2({Key key, @required this.buCode }) : super(key: key);
  @override
  _LoadT createState() => _LoadT();
}

Give this package a shot: auto_route , it:

  1. Let you specify your route animation ( and there are already some ready animations)
  2. Takes care of creating a class for the parameters your page takes, so it solves your problem in the question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM