Skip to content

Conversation

@HansMuller
Copy link
Contributor

@HansMuller HansMuller commented Nov 21, 2016

Specifying null as the DropdownButton's value causes the dropdown to appear empty until a selection is made.

If the dropdown is added to an InputContainer, a hint and label can be displayed as well.

Changed IndexedStack: specifying index: null prevents the widget from displaying anything or responding to input.

Fixes #6422

Here's an example that demos this feature along with InputContainer.

import 'package:flutter/material.dart';

class InputContainerDemo extends StatefulWidget {
  @override
  _InputContainerDemoState createState() => new _InputContainerDemoState();
}

class _InputContainerDemoState extends State<InputContainerDemo> {
  static final List<String> _dropdownItems = <String>['One', 'Two', 'Free', 'Four'];
  String _dropdownValue = null;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('InputContainer Demo')),
      body: new Padding(
        padding: const EdgeInsets.all(32.0),
        child: new DropdownButtonHideUnderline(
          child: new Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              new InputContainer(
                labelText: _dropdownValue == null ? 'Where are you from' : 'From',
                isEmpty: _dropdownValue == null,
                child: new DropdownButton<String>(
                  value: _dropdownValue,
                  isDense: true,
                  onChanged: (String newValue) {
                    setState(() {
                      _dropdownValue = newValue;
                    });
                  },
                  items: _dropdownItems.map((String value) {
                    return new DropdownMenuItem<String>(
                      value: value,
                      child: new Text(value),
                    );
                  }).toList(),
                ),
              ),
              new SizedBox(height: 32.0),
              new Center(
                child: new RaisedButton(
                  onPressed: () {
                    setState((){
                      _dropdownValue = null;
                    });
                  },
                  child: new Text('CLEAR'),
                ),
              ),
            ],
          ),
        ),
      )
    );
  }
}

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

children: config.items
// The button's size is defined by its largest menu item. If value is
// null then an item does not appear.
new Opacity(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use an opacity here? I guess that works, but I would have expected there to be a more direct way of making a subtree take up space but be invisible.

In particular, the children can still be hit with opacity 0.0, but presumably that's not what you want.

new Opacity(
opacity: _selectedIndex == null ? 0.0 : 1.0,
child: new IndexedStack(
index: _selectedIndex ?? 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should teach indexedstack to take a null index, which means "show none of the children"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds good, I'll do that.

@abarth
Copy link
Contributor

abarth commented Nov 22, 2016

LGTM

@HansMuller HansMuller merged commit 516ac57 into flutter:master Nov 22, 2016
@HansMuller HansMuller deleted the dropdown_null_value branch November 22, 2016 00:36
/// from displaying their underlines.
/// * [RaisedButton], [FlatButton], ordinary buttons that trigger a single action.
/// * <https://material.google.com/components/buttons.html#buttons-dropdown-buttons>
class DropdownButton<T> extends StatefulWidget {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at some point we should document T here and in the related classes (not necessarily in this PR unless you're feeling particularly heroic)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will put it on my TODO list.

#6975

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dropdown null selection & hints

3 participants