How To Get Popup Menu Item Value Of Flutter Popup Menu Button

admin_img Posted By Bajarangi soft , Posted On 30-10-2020

If icon is provided, then PopupMenuButton behaves like an IconButton.If both are null, then a standard overflow icon is created (depending on the platform).

How To Get Popup Menu Item Value Of Flutter Popup Menu Button

Get Popup Menu Item Value Of Flutter Popup Menu Button
Complete code For Get Popup Menu Item Value Of Flutter Popup Menu Button In Flutter
main.dart

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyHomePage(),
    );
  }
}

enum PageEnum {
  Bajarangi,
  google,
  yahoo,
}

class MyHomePage extends StatefulWidget {
  @override
  MyHomePageState createState() {
    return new MyHomePageState();
  }
}

class MyHomePageState extends State<MyHomePage> {
  String _currText = "";

  _onSelect(PageEnum value) {
    switch (value) {
      case PageEnum.induceSmile:
        setState(() => _currText = "Bajarangisoft.com");
        break;
      case PageEnum.google:
        setState(() => _currText = "Google.com");

        break;
      case PageEnum.yahoo:
        setState(() => _currText = "yahoo.com");

        break;
      default:
        setState(() => _currText = "Inducesmile.com");

        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.lightBlueAccent,
          title: Text("Get PopupMenuItem Value"),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Center(
              child: Text(_currText, style: TextStyle(fontSize: 20.0)),
            ),
            Center(
              child: PopupMenuButton<PageEnum>(
                onSelected: _onSelect,
                child: Text("Show Popup Menu",style: TextStyle(fontSize: 20),),
                itemBuilder: (context) => <PopupMenuEntry<PageEnum>>[
                  PopupMenuItem<PageEnum>(
                    value: PageEnum.induceSmile,
                    child: Text("Bajarangi"),
                  ),
                  PopupMenuItem<PageEnum>(
                    value: PageEnum.google,
                    child: Text("Google"),
                  ),
                  PopupMenuItem<PageEnum>(
                    value: PageEnum.yahoo,
                    child: Text("Yahoo"),
                  ),
                ],
              ),
            ),
          ],
        ));
  }
}

 

Related Post