DropdownButton隐藏在回车键上,我无法选择一个选项

时间:2019-10-17 17:34:42

标签: flutter

在我的应用程序中,我使用一个DropdownButton来显示枚举选项列表。 创建它没关系,它向我显示了选项,但是问题是当我转到TextField并按DropdownButton时,它打开了值,但立即关闭并返回到最后一个TextField。

这意味着我无法选择该选项,除非我通过单击背面的智能手机按钮关闭键盘。

我正在使用流,例如,这是我的代码:

          InputField(
                  stream: _bloc.outCusto,
                  onChanged: _bloc.changeCusto,
                  initialData: _bloc.entity.custo,
                  textInputType: TextInputType.number,
                  label: "Custo",
                  hint: "Valor de compra",
                ),
          DropdownWidget(
              stream: _bloc.outSituacao,
              initialData: StatusEntityEnum.ACTIVE,
              onChange: _bloc.changeSituacao,
              values: situacoes,
              label: "Situação"),

这是我的小部件:

import 'package:flutter/material.dart';

class InputField extends StatelessWidget {
  final IconData icon;
  final String hint;
  final String label;
  final TextInputType textInputType;
  final bool obscure;
  final bool enable;
  final Stream<dynamic> stream;
  final Function(dynamic) onChanged;
  final dynamic initialData;
  final TextEditingController _controller = TextEditingController();
  final bool alwaysCursorInEnd;

  InputField(
      {Key key,
      this.icon,
      this.hint,
      this.obscure = false,
      this.enable = true,
      this.stream,
      this.label,
      this.textInputType = TextInputType.text,
      this.onChanged,
      this.initialData,
      this.alwaysCursorInEnd = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: this.stream,
        initialData: this.initialData,
        builder: (context, snapshot) {
          _controller.value = _controller.value.copyWith(text: snapshot.data);
          if (alwaysCursorInEnd) {
            _controller.selection = new TextSelection(
                baseOffset: _controller.value.text.length,
                extentOffset: _controller.value.text.length);
          }
          return TextField(
            enabled: this.enable,
            onChanged: this.onChanged,
            decoration: InputDecoration(
                labelText: this.label,
                hintText: this.hint,
                errorText: snapshot.hasError ? snapshot.error : null),
            keyboardType: this.textInputType,
            obscureText: this.obscure,
            controller: _controller,
          );
        });
  }
}

class DropdownWidget extends StatelessWidget {
  final List<DropdownMenuItem<dynamic>> values;
  final Function onChange;
  final String label;
  final Stream<dynamic> stream;
  final dynamic initialData;

  DropdownWidget(
      {this.label, this.values, this.onChange, this.stream, this.initialData});

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 60,
        alignment: Alignment.bottomLeft,
        decoration: BoxDecoration(
            border: Border(
                top: BorderSide.none,
                left: BorderSide.none,
                right: BorderSide.none,
                bottom: BorderSide.lerp(
                    BorderSide(color: Colors.grey, width: 1.0),
                    BorderSide(color: Colors.grey, width: 1.0),
                    0))),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              label,
              style: TextStyle(color: Colors.blueAccent, fontSize: 12),
            ),
            Container(
              height: 25,
              child: DropdownButtonHideUnderline(
                  child: StreamBuilder(
                      stream: this.stream,
                      initialData: this.initialData,
                      builder: (context, snapshot) {
                        return DropdownButton<dynamic>(
                          elevation: 3,
                          isExpanded: true,
                          value: snapshot.data,
                          items: values,
                          onChanged: onChange,
                        );
                      })),
            )
          ],
        ));
  }
}

1 个答案:

答案 0 :(得分:1)

这是一个尚未解决的问题:DropdownButton bad behaviour when tapped and keyboard is showing

您可以使用PopupMenuButtonPopupMenuItem代替DropdownButtonDropdownMenuItem