7

Following the concept of the app bar "page filter" I'd like to put a DropdownButton as the title of the AppBar. I tried, but it doesn't look good.

https://material.io/guidelines/layout/structure.html#structure-app-bar

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

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _value = 'one';

  @override
    void initState() {
      super.initState();
    }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new DropdownButton<String>(
          value: _value,
          items: <DropdownMenuItem<String>>[
            new DropdownMenuItem(
              child: new Text('one'),
              value: 'one',
            ),
            new DropdownMenuItem(
              child: new Text('two'),
              value: 'two'
            ),
          ], 
          onChanged: (String value) {
            setState(() => _value = value);
          },)
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'hello world',
            ),
          ],
        ),
      ),
    );
  }
}

However it looks like: ios simulator

which doesn't follow the material pattern found at the link stated above due to the weird looking underline... bonus: the text and button should be white.

11
  • > which doesn't follow the material pattern found at the link stated above there's no link Commented Mar 7, 2018 at 16:43
  • And I doupt material has any rules about DropDown inside appbar ; as according to material, only string and icons should be used inside appbar. Commented Mar 7, 2018 at 16:44
  • @Darky see edit above (link is also here: material.io/guidelines/layout/structure.html#structure-app-bar) Commented Mar 7, 2018 at 16:51
  • On IOS according to the specs the title is centered Commented Mar 7, 2018 at 16:52
  • @Darky that's very much unhelpful. I specifically said, "ignoring the left right position because it's being rendered in iOS" Commented Mar 7, 2018 at 16:53

4 Answers 4

13

I did find some things that helped my situation... The widgets DropdownButtonHideUnderline and Theme will help control how the dropdown looks in the title of the AppBar

new AppBar(
  title: new Theme(
      child: new DropdownButtonHideUnderline(
      child: new DropdownButton<String>(
        value: _value,
        items: <DropdownMenuItem<String>>[
          new DropdownMenuItem(
            child: new Text('My Page'),
            value: 'one',
          ),
        ], 
        onChanged: (String value) {
          setState(() => _value = value);
        },
      ),
    ), 
    data: new ThemeData.dark(),
  ),
),

However, now the popup's background color is black to match the dark theme... not sure if there's a way to have the theme not affect the actual popup.

I personally can live with the black background color of the popup... unless someone can also solve that.

enter image description here enter image description here

Sign up to request clarification or add additional context in comments.

Comments

3

Do something like this:

appBar: AppBar(
    title: Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        DropdownButton(
          value: _selectedItem,
          items: _dropdownMenuItems,
          underline: SizedBox(height: 0,),
          //underline: SizedBox(),
          onChanged: onChangeDropdownItem,
        ),
       ],
      ),
    ),

Then change your dropdown menu's style here:

/// Initialize dropdown menu
List<DropdownMenuItem<String>> buildDropdownMenuItems(List menu) {
List<DropdownMenuItem<String>> items = List();
for (String li in menu) {
  items.add(
    DropdownMenuItem(
      value: li,
      child: SizedBox(
        width: 100, 
        child: Text(li, style:  TextStyle(color: labelColor, fontWeight: 
               FontWeight.bold), 
               textAlign: TextAlign.center, overflow: TextOverflow.ellipsis,),),
    ),
  );
}
return items;

}

Comments

1

Please just change your code to the one I have mentioned below and this one might work for your app.enter image description here, sorry for the bad editing of the picture. I have included the full code for your reference,.

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new MyHomePage(),
  ));
}

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

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _value = 'one';

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
          title:
          new Row(
         mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          new DropdownButton<String>(
            value: _value,
            items: <DropdownMenuItem<String>>[
              new DropdownMenuItem(
                child: new Text('one'),
                value: 'one',
              ),
              new DropdownMenuItem(child: new Text('two'), value: 'two'),
            ],
            onChanged: (String value) {
              setState(() => _value = value);
            },
          ),

        ],
      )
 ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'hello world',
            ),
          ],
        ),
      ),
    );
  }
}

enter image description here

See the demo here: https://codepen.io/andrerpena/pen/LYpZRqG

2 Comments

but now the text is an ugly gray... I want it to be white with a white button. The only way that I could accomplish this was by making sure the brigthness was set to dark (and the easiest way to do that was wrap it all in a theme with new ThemeData.dark()... this does not solve the question (imo)
I added a Codepen demo to your answer: codepen.io/andrerpena/pen/LYpZRqG
0

To have a white menu, change data: new Theme.dark() to Theme.of(context).copyWith(brightness: Brightness.dark))

But then another problems pop : The menu is white ; but the menu options are written in white too, making them unreadable.

After a deep inspection, it seems like it's currently not possible to have options using a different font color in the iddle state and when the dropdown is focused. Consider creating an issue on their github

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.