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

时间:2019-11-25 15:23:35

标签: android ios flutter mobile

我正在尝试这种设计: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
[标签]

PS:我知道这不是一个标签,因为它只是一个文本,如果有人可以帮助我,那会很好,谢谢。

2 个答案:

答案 0 :(得分:1)

如果我正确地找到了您想要的东西,则需要使用Row(阅读更多here)来执行此操作,如以下示例所示,使用您的代码:

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

答案 1 :(得分:0)

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

相关问题