Flutter, passing parameters to PageRoutebuilder

后端 未结 2 1848
情书的邮戳
情书的邮戳 2021-01-28 06:04

In Method gotoThirdScreen(), I am tryiing to pass parameters to a class. I believe the issue starts at: //========================================================= //===

相关标签:
2条回答
  • 2021-01-28 06:44

    you can use onGenerateRoute here the example

    main.dart

    void main() {
      runApp(new App());
    }
    

    app.dart

    class App extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
            home: new HomeScreen(),
            routes: <String, WidgetBuilder>{
              '/home': (BuildContext context) => new HomeScreen(),
              //other routes
            },
            //onGenerateRoute Custom
            onGenerateRoute: getGenerateRoute);
      }
    }
    
    Route<Null> getGenerateRoute(RouteSettings settings) {
      final List<String> path = settings.name.split('/');
      if (path[0] != '') return null;
    
      if (path[1].startsWith('team')) {
        if (path.length != 3) return null;
        String _title = path[2];
        return new MaterialPageRoute<Null>(
          settings: settings,
          builder: (BuildContext context) => new TeamScreen(
                title: _title,
              ),
        );
      }
      // The other paths we support are in the routes table.
      return null;
    }
    

    home_screen.dart

    import 'package:flutter/material.dart';
    
    class HomeScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('Home Screen'),
          ),
          body: new Center(
            child: new Column(
              children: <Widget>[
                new RaisedButton(
                  onPressed: () {
                    String _title = "Team 1";
                    Navigator.pushNamed(context, '/team/$_title');
                  },
                  child: new Text('Go Team 1'),
                ),
                new RaisedButton(
                  onPressed: () {
                    String _title = "Team 2";
                    Navigator.pushNamed(context, '/team/$_title');
                  },
                  child: new Text('Go Team 2'),
                )
              ],
            ),
          ),
        );
      }
    }
    

    team.dart

    import 'package:flutter/material.dart';
    
    class TeamScreen extends StatelessWidget {
      final String title;
    
      const TeamScreen({Key key, this.title}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('Team screen'),
          ),
          body: new Center(
            child: new Text(title),
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2021-01-28 06:58

    Pass title value to SecondPage constructor:

    void _gotoThirdPage() {
      String page_title = "Yet Another Page";
    
      final pageRoute = new PageRouteBuilder(
        pageBuilder: (BuildContext context, Animation animation,
            Animation secondaryAnimation) {
          return new SecondPage(title: page_title);
        },
    
    0 讨论(0)
提交回复
热议问题