简体   繁体   English

有没有办法在颤动中为 DropdownButton 添加标签?

[英]Is there a way to add a label to a DropdownButton in flutter?

I'm trying to get this kind of design: 1我正在尝试获得这种设计: 1
[Label] : [Dropdown List] [标签] : [下拉列表]
[Label] : [Text field] [标签] : [文本字段]
[Label] : [Dropdown List] [标签] : [下拉列表]

I'm already able to implement the dropdown using this code:我已经能够使用以下代码实现下拉列表:

  List<String> _locations = ['Cita', 'Junta', 'Proyecto', 'Examen', 'Otro']; // Option 2
  String _selectedLocation; // Option 2

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Crear Evento'),
      ),
      body: Center(
        child: Container(
          width: double.infinity,
          height: 400,
          padding: EdgeInsets.symmetric(horizontal: 20),
          alignment: Alignment.center,
          child: Column(
            children: <Widget>[
              new Text('Categoría'),
              DropdownButton(
                hint: Text('Categoría'), // Not necessary for Option 1
                value: _selectedLocation,
                onChanged: (newValue) {
                  setState(() {
                    _selectedLocation = newValue;
                  });
                },
                items: _locations.map((location) {
                  return DropdownMenuItem(
                    child: new Text(location),
                    value: location,
                  );
                }).toList(),
              ),
            ],
          ),
        ),
      ),
    );

However, the output of this is not in the way I'd like, as the output is in the format: 2但是,它的输出不是我想要的方式,因为输出的格式为: 2
[label] [标签]

PS.: I know it's not properly a label since it only is a text, if anyone could help me that'd be great, thanks in advance. PS.:我知道这不是一个正确的标签,因为它只是一个文本,如果有人能帮助我那就太好了,提前致谢。

If I got what you wanted right, you will need to use a Row (read more here ) to do that, like the below example, using your code:如果我得到了你想要的东西,你将需要使用 Row( 在这里阅读更多)来做到这一点,就像下面的例子一样,使用你的代码:

List<String> _locations = ['Cita', 'Junta', 'Proyecto', 'Examen', 'Otro']; // Option 2
  String _selectedLocation; // Option 2

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Crear Evento'),
      ),
      body: Center(
        child: Container(
          width: double.infinity,
          height: 400,
          padding: EdgeInsets.symmetric(horizontal: 20),
          alignment: Alignment.center,
          child: Column(
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('Categoría'),
                  Container(width: 8),
                  DropdownButton(
                    hint: Text('Categoría'), // Not necessary for Option 1
                    value: _selectedLocation,
                    onChanged: (newValue) {
                      setState(() {
                        _selectedLocation = newValue;
                      });
                    },
                    items: _locations.map((location) {
                      return DropdownMenuItem(
                        child: new Text(location),
                        value: location,
                      );
                    }).toList(),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

which produces this:产生这个:

在此处输入图片说明

I know this is not the answer for the question, but anyone (who was like me, earlier) who wants to add a labelText like in FormFields, you can use the InputDecorator .我知道这不是问题的答案,但是任何想要在 FormFields 中添加 labelText 的人(之前和我一样),您都可以使用InputDecorator

class ChecklistAddNewRepatDropdown extends StatelessWidget {
  final Map<int, List> tasks;
  final ChecklistAddNewState state;
  final int index;

  const ChecklistAddNewRepatDropdown({
     Key key,
     @required this.tasks,
     @required this.index,
     @required this.state,
   }) : super(key: key);

  @override
  Widget build(BuildContext context) {
 
 return InputDecorator(
  decoration: (state.emptyTasks.indexOf(index) == -1)
      ? inputDecoration('Repeat')
      : inputErrorDecoration('Repeat'),
  child: DropdownButtonHideUnderline(
    child: DropdownButton<String>(
      
      value: (tasks[index] == null || tasks[index][1] == "")
          ? 'One'
          : tasks[index][1],

      isExpanded: true,
      
      isDense: true,
      
      style: inputTextStyle(),
      
      icon: Icon(
        Icons.arrow_drop_down,
        color: Colors.black,
      ),
      
      iconSize: 24,

      onChanged: (value) {
        if (tasks[index] != null) {
          tasks[index][1] = value;
        } else {
          tasks[index] = ["", value];
        }
        checklistAddNewBloc.add(
          TasksTaskWhileChangingEvent(tasks),
        );
      },

      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    ),
  ),
);

you can sort of use it this way.你可以这样使用它。 (There may be errors in the code, I just want to give an example on how to use the InputDecorator). (代码中可能有错误,我只是想举例说明如何使用InputDecorator)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM