繁体   English   中英

DropdownButton 在输入时隐藏,我不能 select 选项

[英]DropdownButton hide on enter on it and I can't select an option

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

这意味着我不能 select 选项,除非我通过单击智能手机后退按钮关闭键盘。

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

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

这是一个未解决的问题: DropdownButton bad behavior when taped and keyboard is displayed

您可以使用PopupMenuButtonPopupMenuItem而不是DropdownButtonDropdownMenuItem

暂无
暂无

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

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