在Flutter中,有没有一种方法可以为DropdownButton添加标签?

3
我可以帮您翻译以下内容,涉及到IT技术。请注意保留HTML标签,并且尽可能使翻译通俗易懂:

我想要实现这样的设计:1
[标签]:[下拉列表]
[标签]:[文本框]
[标签]:[下拉列表]

我已经能够使用以下代码实现下拉列表:

  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(),
              ),
            ],
          ),
        ),
      ),
    );

然而,输出结果并不符合我的要求,因为输出结果的格式是这样的:2
[label]

PS:我知道这不是一个正确的标签,因为它只是一段文本,如果有人能帮我解决这个问题,那就太好了,提前感谢。

2个回答

3
如果我理解正确,您需要使用行(阅读更多这里)来实现此操作,就像以下示例中使用您的代码一样:
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(),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );

这会产生这个:

enter image description here


2

我知道这不是问题的答案,但是任何想要像在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(),
    ),
  ),
);

您可以这样使用它。 (代码中可能会有错误,我只是想举个例子来说明如何使用InputDecorator)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接