Is there a way to pass multiple index arguments to another screen using pushNamed?

梦想的初衷 提交于 2020-11-29 15:09:50

问题


I'm trying to navigate to a new screen using ListViewBuilder and Cards. I currently have my named route set so that it accepts an index of my parsed json model. So the question is how do I pass the 'name' (String) parameter, as well as, 'nextEpisodes' (ListString) and 'prevEpisodes' (ListString). I'd like to access all of these variables on the next screen.

Thanks in advance.

ListView.builder(
                  shrinkWrap: true,
                  itemCount: slist.length,
                  itemBuilder: (context, index) {
                    print(slist[index].prevEpisodes);
                    return Card(
                      child: ListTile(
                          onTap: () {
                            Navigator.of(context).pushNamed('/episodes', arguments: slist[index].name);
                          },
                          title: Text(slist[index].name),
                          //                        subtitle: Text(shows[index].showNextAirDate),
                          //                        leading: CircleAvatar(
                          //                          backgroundImage:
                          //                              AssetImage('assets/${shows[index].showPic}'),
                          //                        ),
                          trailing: Icon(Icons.keyboard_arrow_right,
                              color: Colors.black26, size: 30.0)),
                    );
                  }),

回答1:


You can send whole list item from particular index of the List from one screen to another like this way

Inside the class A

 List<YOUR_BEAN> list = new List<YOU_BEAN>();


    Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => B(bean: list [index])), //// HERE B IS THE CLASS ON WHICH YOU NEED TO CARRY DATA FROM CLASS A
    );

And inside the Bclass you need to add the constructor to catch the value from A class like below

class B extends StatefulWidget {
      YOUR_BEAN bean;

      B ({Key key, @required this.bean}) : super(key: key); ////YOU WILL GET THE DATA HERE FROM THE CONSTRUCTOR , AND USE IT INSIDE THE CLASS LIKE "widget.bean" 

      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
        return _B();
      }
    }

And please check the example of it to pass data from one class to another

A class which send data to B class

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import 'B.dart';
import 'Fields.dart';

    class A extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
        return _A();
      }
    }

    class _A extends State<A> {

      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Screen A',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
              primaryColor: Colors.red,
              accentColor: Color(0xFFFEF9EB),
            ),
            home: Scaffold(
                appBar: new AppBar(),
                body: Container(
                  margin: EdgeInsets.all(20.0),
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.all(10.0),
                        child: Text("Screen A"),
                      ),
                      Expanded(
                        child: ListView.builder(
                            itemCount: fields.length,
                            itemBuilder: (BuildContext ctxt, int index) {
                              return ListTile(
                                title: new Text("Rating #${fields[index].rating}"),
                                subtitle: new Text(fields[index].title),
                                onTap: (){

                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: (context) => B(bean: fields [index])), //// HERE B IS THE CLASS ON WHICH YOU NEED TO CARRY DATA FROM CLASS A
                                  );
                                },
                              );
                            }),
                      )
                    ],
                  ),
                )));
      }
    }

    List<Fields> fields = [
      new Fields(
        'One',
        1,
      ),
      new Fields(
        'Two',
        2,
      ),
      new Fields(
        'Three',
        3,
      ),
      new Fields(
        'Four',
        4,
      ),
      new Fields(
        'Five',
        5,
      ),
    ];

Now check B class which receive the data from A class

import 'package:flutter/material.dart';

import 'Fields.dart';


    class B extends StatefulWidget{

      Fields bean;

      B ({Key key, @required this.bean}) : super(key: key); ////YOU WILL GET THE DATA HERE FROM THE CONSTRUCTOR , AND USE IT INSIDE THE CLASS LIKE "widget.bean"

      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
         return _B ();

      }

    }

    class _B extends State<B> {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return MaterialApp(
            title: 'Screen A',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
              primaryColor: Colors.red,
              accentColor: Color(0xFFFEF9EB),
            ),
            home: Scaffold(
                appBar: new AppBar(),
                body: Container(
                  margin: EdgeInsets.all(20.0),
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.all(10.0),
                        child: Center(
                          child: Text("Screen B" ,style: TextStyle(fontSize: 20.0),),
                        )
                      ),
                       Text("Rating=>>>  ${widget.bean.rating}  and Title ${widget.bean.title} ")
                    ],
                  ),
                )));
      }
    }

And I have used the Pojo class for the list ,please check it once

Fields.dart

class Fields {
  final String title;
  final int rating;

  Fields(this.title, this.rating);
}

And the output of the above program as follow.



来源:https://stackoverflow.com/questions/61742331/is-there-a-way-to-pass-multiple-index-arguments-to-another-screen-using-pushname

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!