Home > Mobile >  PopupMenuButton can't navigate to other screens
PopupMenuButton can't navigate to other screens

Time:10-14

I found this solution on github when directly using ontap didn't work, but apparently I am doing something wrong. I will post the code for you to see, thanks for answering.

import 'package:flutter/material.dart';

import 'routegenerator.dart';


enum PopupMenuAction {

  s,

  s1,

  s2,

  s3,

  s4,

}


class Buttonall extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return PopupMenuButton<PopupMenuAction>(

        itemBuilder: (context) => [

              PopupMenuItem(

                child: Text('1STAR'),

                value: PopupMenuAction.s1,

              ),

              PopupMenuItem(

                child: Text('2STARS'),

                value: PopupMenuAction.s2,

              ),

              PopupMenuItem(

                child: Text('3STARS'),

                value: PopupMenuAction.s,

              ),

              PopupMenuItem(

                child: Text('4STARS'),

                value: PopupMenuAction.s4,

              ),

              PopupMenuItem(

                child: Text('5 STARS'),

                value: PopupMenuAction.s,

              )

            ],

        onSelected: (choice) {

          switch (choice) {

            case PopupMenuAction.s:

              Navigator.of(context).pushNamed('/s');

              break;

            case PopupMenuAction.s1:

              Navigator.of(context).pushNamed('/s1');

              break;


            case PopupMenuAction.s2:

              Navigator.of(context).pushNamed('/s2');

              break;

            case PopupMenuAction.s3:

              Navigator.of(context).pushNamed('/s3');


              break;


            case PopupMenuAction.s4:

              Navigator.of(context).pushNamed('/s4');


              break;

          }

        });

  }

}

After I tried to run this code, I still couldn't navigate by clicking on the popupmenuitems. I tried about three to five methods proposed online without much help, even after trying to look at some other projects code.

CodePudding user response:

i tried your code and updated some things and it's working:


import 'package:demo/b.dart';
import 'package:flutter/material.dart';

enum PopupMenuAction {
  s,

  s1,

  s2,

  s3,

  s4,
}

class Buttonall extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PopupMenuButton<PopupMenuAction>(
        itemBuilder: (context) => [
              PopupMenuItem(
                child: Text('1STAR'),
                value: PopupMenuAction.s1,
              ),
              PopupMenuItem(
                child: Text('2STARS'),
                value: PopupMenuAction.s2,
              ),
              PopupMenuItem(
                child: Text('3STARS'),
                value: PopupMenuAction.s,
              ),
              PopupMenuItem(
                child: Text('4STARS'),
                value: PopupMenuAction.s4,
              ),
              PopupMenuItem(
                child: Text('5 STARS'),
                value: PopupMenuAction.s,
              )
            ],
        onSelected: (choice) {
          switch (choice) {
            case PopupMenuAction.s:
              Navigator.of(context).pushNamed(BScreen.routeName);

              break;

            case PopupMenuAction.s1:
              Navigator.of(context).pushNamed(BScreen.routeName);

              break;

            case PopupMenuAction.s2:
              Navigator.of(context).pushNamed(BScreen.routeName);

              break;

            case PopupMenuAction.s3:
              Navigator.of(context).pushNamed(BScreen.routeName);

              break;

            case PopupMenuAction.s4:
              Navigator.of(context).pushNamed(BScreen.routeName);

              break;
          }
        });
  }
}


here BScreen.routename is another screen

BScreen that i created

import 'package:flutter/material.dart';

class BScreen extends StatefulWidget {
  static const routeName = '/bscreen'; // this is name for avoiding errors

  const BScreen({Key? key}) : super(key: key);
  @override
  State<BScreen> createState() => _BScreenState();
}

class _BScreenState extends State<BScreen> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

last thing we need to update/define route in main.dart

import 'package:demo/a.dart';
import 'package:demo/b.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
       
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(body: Buttonall()),
      routes: {
        BScreen.routeName: (ctx) => BScreen(),
      },
    );
  }
}

main change in main.dart is routes:{} and wrapping your home: into Scaffold.

CodePudding user response:

try wrapping PopupMenuButton<PopupMenuAction> in scaffold i could help you much better if you provide logs

  • Related