Flutter

PopupMenuButton

Michelle Hwang 2021. 6. 26. 08:08

 

 

 

 

 

import 'package:flutter/material.dart';

enum RGB { red, green, blue }

void main() => runApp(WidgetDemo());

class WidgetDemo extends StatefulWidget {
  @override
  _WidgetDemoState createState() => _WidgetDemoState();
}

class _WidgetDemoState extends State<WidgetDemo> {
  RGB _selection = RGB.red;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('PoopupMenuButton Demo'),
        ),
        body: Center(
          child: PopupMenuButton<RGB>(
            onSelected: (RGB result) {
              setState(() {
                _selection = result;
              });
            },
            itemBuilder: (BuildContext context) =>
            <PopupMenuEntry<RGB>>[
              const PopupMenuItem<RGB>(child: Text('Red'), value: RGB.red,),
              const PopupMenuItem<RGB>(child: Text('Green'), value: RGB.green,),
              const PopupMenuItem<RGB>(child: Text('Blue'), value: RGB.blue,),
            ],
          ),
        ),
      ),
    );
  }
}

_selection 변수는 어떤 메뉴를 선택했는지 알기위해 필요하다. 

 

Flutter class 문서는 다음과 같다.

https://api.flutter.dev/flutter/material/PopupMenuButton-class.html

 

PopupMenuButton class - material library - Dart API

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

api.flutter.dev