Open Popup Menu Button When Listview Item Is Clicked In Flutter

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

Displays a menu when pressed and calls onSelected when the menu is dismissed because an item was selected. The value passed to onSelected is the value of the selected menu item.One of child or icon may be provided, but not both. If icon is provided, then PopupMenuButton behaves like an IconButton.

Open Popup Menu Button When Listview Item Is Clicked In Flutter

PopupMenu In ListView
Complete Code For PopupMenu In ListView 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(),
    );
  }
}

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

class MyHomePageState extends State<MyHomePage> {
  List<String> _texts = [
    "BajarangiSoft.com",
    "google.com",
    "youtube.com",
    "yahoo.com",
    "gmail.com",
    "flutter.dev",
  ];

  _showPopupMenu() async {
    await showMenu(
      context: context,
      position: RelativeRect.fromLTRB(100, 400, 100, 400),
      items: [
        PopupMenuItem(
          child: Text("Menu 1"),
        ),
        PopupMenuItem(
          child: Text("Menu 2"),
        ),
        PopupMenuItem(
          child: Text("Menu 3"),
        ),
      ],
      elevation: 8.0,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.blue[900],
        title: Text(" PopupMenu In ListView"),
      ),
      body: ListView(
        children: _texts
            .map((t) => ListTile(
          title: Text(t,style: TextStyle(color: Colors.white),),
          onTap: _showPopupMenu,
        ))
            .toList(),
      ),
    );
  }
}

Related Post