Search code examples
flutter

How to make an IconButton that displays a list of options


Greetings to all the devs who contribute their knowledge to beginners, I would like to know how I can make a button that will be hosted in the appBar and that when I touch it it shows some options, which have an icon and a text, and when I touch one of those options that I send to a new view, as shown in the following image.

enter image description here


Solution

  • Learn more about DropdownButton

    This is just a example, tested in DartPad:

    import 'package:flutter/material.dart';
    
    void main() =>
        runApp(MaterialApp(home: MyHomePage(), debugShowCheckedModeBanner: false));
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key? key}) : super(key: key);
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String dropdownValue = 'One';
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("ExampleDropDown"),
            actions: [
              DropdownButton(
                icon: Icon(Icons.arrow_downward),
                onChanged: (String? newValue) {
                  setState(() {
                    dropdownValue = newValue!;
                  });
                },
                items: <String>['One', 'Two', 'Free', 'Four']
                    .map<DropdownMenuItem<String>>(
                  (String value) {
                    return DropdownMenuItem<String>(
                      value: value,
                      child: Text(value),
                    );
                  },
                ).toList(),
              ),
            ],
          ),
          body: Center(child: Text("Example")),
        );
      }
    }