简体   繁体   中英

How to navigate to new page using flutter?

I want to click each navigation item and direct it to another activity.

I actually want to show the list view of 'now playing movie'.

When I click that drawer item, I have no idea how to display it, please help me

My code

navigation_drawer.dart looks like:

class NavigationDrawer extends StatelessWidget {
  const NavigationDrawer({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Material(
        color: Colors.black,
        child: Padding(
          padding: const EdgeInsets.fromLTRB(24.0, 80, 24, 0),
          child: Column(
            children: [
              DrawerItem(
                name: 'Now playing',
                icon: Icons.play_circle_outline,
                onPressed: ()=> onItemPressed(context, index: 0),
              ),
              const SizedBox(height: 30,),
              DrawerItem(
                  name: 'Popular',
                  icon: Icons.analytics_outlined,
                  onPressed: ()=> onItemPressed(context, index: 1)
              ),
              const SizedBox(height: 30,),
              DrawerItem(
                  name: 'Top Rated',
                  icon: Icons.star_border,
                  onPressed: ()=> onItemPressed(context, index: 2)
              ),
              const SizedBox(height: 30,),
              DrawerItem(
                  name: 'Upcoming',
                  icon: Icons.ondemand_video,
                  onPressed: ()=> onItemPressed(context, index: 3)
              ),
              const SizedBox(height: 30,),
              const Divider(thickness: 1, height: 10, color: Colors.grey,),
              const SizedBox(height: 30,),
              DrawerItem(
                  name: 'About',
                  icon: Icons.settings,
                  onPressed: ()=> onItemPressed(context, index: 4)
              ),
              const SizedBox(height: 30,),
            ],
          ),
        ),
      ),
    );
  }

  void onItemPressed(BuildContext context, {required int index}){
    Navigator.pop(context);

    switch(index){
      case 0:
        Navigator.push(context, MaterialPageRoute(builder: (context) => NowPlayingPages(nowplayingpages: ,)));
        break;
    }
  }
}

This keeps telling me that I need to put the arguments on it, but I didn't know what argument to use:

Navigator.push(context, MaterialPageRoute(builder: (context) => NowPlayingPages())); 

nowplaying_pages.dart:

class NowPlayingPages extends StatelessWidget {
  final List nowplayingpages;

  const NowPlayingPages({Key? key, required this.nowplayingpages}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    return Container(

      padding: EdgeInsets.all(10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          modified_text(text:'Now Playing', size: 26,color: Colors.white,),
          SizedBox(height: 37,),
          Container(height: 297,
            child: ListView.builder(itemCount: nowplayingpages.length,
                scrollDirection: Axis.vertical,
                itemBuilder: (context, index){
                  return InkWell(
                    onTap: (){
                      Navigator.push(context, MaterialPageRoute(builder: (context)=> Description(
                        name: nowplayingpages[index]['title'],
                        bannerurl:
                        'https://image.tmdb.org/t/p/w500' +
                            nowplayingpages[index]['backdrop_path'],
                        posterurl:
                        'https://image.tmdb.org/t/p/w500' +
                            nowplayingpages[index]['poster_path'],
                        description: nowplayingpages[index]['overview'],
                        vote: nowplayingpages[index]['vote_average']
                            .toString(),
                        launch_on: nowplayingpages[index]
                        ['release_date'],
                      )));
                    },
                    child: Container(
                      width: 167,
                      child: Column(
                        children: [
                          Container(
                            height:227,
                            decoration: BoxDecoration(image :DecorationImage(
                                image: NetworkImage(
                                    'https://image.tmdb.org/t/p/w500' + nowplayingpages[index]['poster_path']
                                )
                            )),
                          ),
                          Container(child: modified_text(text:nowplayingpages[index]['title']!=null?
                          nowplayingpages[index]['title']:'Loading', size: 14, color: Colors.white,))
                        ],
                      ),
                    ),
                  );
                }),)
        ],
      ),
    );
  }
}

In your NowPlayingPages class you have a field

final List nowplayingpages;

that's why flutter asking for arguments.. otherwise you can remove above mentioned field and you have to fetch the data from NowPlayingPages class onload and send it to listview

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