popup_menu_2 0.1.5 copy "popup_menu_2: ^0.1.5" to clipboard
popup_menu_2: ^0.1.5 copied to clipboard

A popup menu containing clickable buttons, each with a global click function referring to the item. This is designed to avoid breaking the user's attention.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:popup_menu_2/popup_menu_2.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primarySwatch: Colors.brown, primaryColor: Colors.white),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  GlobalKey mkey = GlobalKey();
  GlobalKey<ContextualMenuState> keyState = GlobalKey<ContextualMenuState>();

  Future<void> _incrementCounter() async {
    // await Future.delayed(const Duration(milliseconds: 500));

    _counter++;
    setState(() {});

    keyState.currentState?.dismiss();
  }

  Future<void> _decrementCounter() async {
    // await Future.delayed(const Duration(milliseconds: 500));
    _counter--;
    setState(() {});
    keyState.currentState?.dismiss();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
          //   leading: _action(),
          bottom: AppBar(
            actions: [_action()],
          ),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text(
                'The Accountant result',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _action() {
    return PhysicalModel(
      color: Colors.grey,
      shape: BoxShape.circle,
      elevation: 10,
      child: SizedBox(
        height: AppBar().preferredSize.height,
        width: AppBar().preferredSize.height,
        child: add(),
      ),
    );
  }

  Widget add() {
    return Builder(builder: (context) {
      return Padding(
        padding: const EdgeInsets.all(12.0),
        child: ContextualMenu(
          key: keyState,
          targetWidgetKey: mkey,
          maxColumns: 2,
          backgroundColor: Colors.red,
          dismissOnClickAway: true,
          items: [
            ContextPopupMenuItem(
              onTap: _incrementCounter,
              child: const Icon(Icons.add, color: Colors.white),
            ),
            ContextPopupMenuItem(
              onTap: _decrementCounter,
              child: const Icon(Icons.remove, color: Colors.white),
            ),
          ],
          child: Icon(
            Icons.add,
            key: mkey,
            color: Colors.white,
          ),
        ),
      );
    });
  }
}
15
likes
130
points
144
downloads

Publisher

verified publisheryoumti.net

Weekly Downloads

A popup menu containing clickable buttons, each with a global click function referring to the item. This is designed to avoid breaking the user's attention.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

cupertino_icons, flutter

More

Packages that depend on popup_menu_2