简体   繁体   English

Flutter-在TextTextField上显示对话框编号选择器

[英]Flutter - Show Dialog NumberPicker on Tap TextFormField

The problem is simple: I need to show numberpickerdialog when i click in form field. 问题很简单:在表单字段中单击时,我需要显示numberpickerdialog Then i need to asign numberpicker value to field. 然后我需要将数字选择器值分配给字段。

FORM FIELD 表格栏位

 final maxValue = new GestureDetector(
              onTap: () {
                print("entra");
           _showDialog(context);
              },
              child: TextFormField(
                  //controller: inputMaxValue,
                  decoration: InputDecoration(
                    hintText: DemoLocalizations.of(context).trans('value-meter-max'),
                    focusedBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Colors.blue[300], width: 2.5),
                    ),
                  )),
            );

DIALOG 对话

  void _showDialog(context) {
    showDialog<double>(
    context: context,
      builder: (BuildContext context) {
        return new NumberPickerDialog.decimal(
          minValue: 1,
          maxValue: 10,
          title: new Text("Pick a new price"),
          initialDoubleValue: _currentPrice,
        );
      }
    ).then((double value) {
      if (value != null) {
        setState(() => _currentPrice = value);
      }
    });
  }

The problem: When i click field dialog doesn´t show: How can i launch showDialog when i click in this field? 问题:单击字段对话框时未显示:单击此字段时如何启动showDialog?

I recreated your case and noticed that the issue could be due to TextFormField used. 我重新创建了您的案例,并注意到该问题可能是由于使用了TextFormField引起的。 Ideally, a TextFormField should only be used to edit a text because we are clicking on it anyway which enables the field with cursor. 理想情况下, TextFormField仅应用于编辑文本,因为无论如何我们都单击它可以启用带有光标的字段。 If we are wrapping it with GestureDetector , we are trying to tap on it again which could be conflicting with the click event. 如果使用GestureDetector包装它,我们将尝试再次点击它,这可能与click事件冲突。 I would rather use InputDecorator and wrap it with GestureDetector . 我宁愿用InputDecorator ,并把它包GestureDetector Here's a working example of it which opens a dialog: 这是打开对话框的工作示例:

  @override   Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
           child: GestureDetector(
               child: InputDecorator(
                 decoration: InputDecoration(
                     labelText: 'Test'
                 ),
               ),
             onTap: () {
                 _showDialog();
             },
           )
      )
      );

  }

  void _showDialog() {
    // flutter defined function
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Alert Dialog title"),
          content: new Text("Alert Dialog body"),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            new FlatButton(
              child: new Text("Close"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );   }

在此处输入图片说明

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

相关问题 Flutter 中不推荐使用 TextFormField 的自动验证 - Autovalidate of TextFormField is deprecated in Flutter 如何在 TextFormField 中显示输入时间? - how to show the input time in a TextFormField? Flutter Textformfield 验证器将最后一个 TextFormfield 集中在验证错误上,而不是第一个 - Flutter Textformfield validator Focuses Last TextFormfield on validation error instead of first 如何更改 TextFormField() 小部件颜色 Flutter - How to change TextFormField() widget color Flutter 表单的 TextFormField 中的多个光标抖动 - Multiple cursor in form's TextFormField flutter 如何在 Flutter 中对齐 TextFormField 旁边的文本? - How to align Text next to TextFormField in Flutter? Flutter:向 Column 添加了一个额外的 TextFormField 但显示没有变化 - Flutter: Added an additional TextFormField to Column but no change in display Flutter 如何在显示对话框中使用文本形式获取用户输入? - Flutter how to get user input using text form in show dialog? 在 listview flutter 内的 textformfield 中按下数字后键盘关闭 - keyboard close after pressing a number in textformfield inside listview flutter 在Flutter TextFormField中输入数据时,键盘不断消失 - Keyboard keeps disappearing while entering data in Flutter TextFormField
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM