【问题标题】:Is there a way to create a text field with a prefix that is always visible in flutter有没有办法创建一个前缀在颤振中始终可见的文本字段
【发布时间】:2020-03-08 05:45:01
【问题描述】:

我想创建一个带有始终可见前缀的文本字段。我正在创建一个类似于用于 WhatsApp 电话号码输入的电话字段。我想将国家代码作为永久前缀。

我已经尝试过 InputDecoration 的 prefix 和 prefixText 选项,但是,前缀仅在 textField 处于焦点时可见。对于如何实现这一目标提供的任何帮助将不胜感激。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    更新:我们现在可以用prefixIconConstraints 覆盖prefixIconsuffixIcon 的最小填充。

    我们可以使用prefixIcon 代替prefixText。由于prefixIcon 接受任何小部件,我们可以使用prefixIcon: Text("$")

    这是一个例子:

    InputDecoration(
       isDense: true,
       prefixIcon:Text("\$"),
       prefixIconConstraints: BoxConstraints(minWidth: 0, minHeight: 0),
    ),
    

    【讨论】:

    • 谢谢@Abhishek。我也遇到过这个问题,所以想分享一下。
    • 如果你添加labelText换行符
    • 你可能需要像这样Text("\$") 否则它会抛出错误
    • @MDompekidis 感谢您指出这一点。我没有注意到这一点。我已经改变了答案。
    • @Uni 完美。解决了我的问题。谢谢!
    【解决方案2】:

    当我必须显示前缀始终可见时,这是完美的解决方案

    prefixIcon: Padding(padding: EdgeInsets.all(15), child: Text('+91 ')),
    

    【讨论】:

      【解决方案3】:

      有一个bug 已用解决方案关闭:

      暂时关闭,因为无法保持前缀文本可见 在所有具有浮动占位符的州,这是 这个小部件。

      还有一些对我有用的解决方法:

      TextStyle _decorationStyleOf(BuildContext context) {
        final theme = Theme.of(context);
        return theme.textTheme.subhead
            .copyWith(color: theme.hintColor);
      }
      
      Stack(
        alignment: Alignment.centerLeft,
        children: <Widget>[
          Builder(
            builder: (context) {
              return Text(
                'prefix',
                style: _decorationStyleOf(context),
              );
            }
          ),
          TextField(
            decoration: InputDecoration(
              prefixText: 'prefix',
              prefixStyle: TextStyle(color: Colors.transparent),
            ),
          ),
        ],
      )
      

      【讨论】:

      • 这不起作用。它只会使 prefixText 完全透明。
      • 新更新!!!在下面查看我的答案。只需更改InputDecoration的属性即可。
      【解决方案4】:
      FocusNode focusNode = FocusNode();
      
      
      @override
        void dispose() {
      
          focusNode.dispose();
      
          super.dispose();
        }
      

      内部构建写入

      hintText = prefix;
      
      
      focusNode.addListener(() {
        if (focusNode.hasFocus) {
      
          setState(() {
            hintText = '';
            hasHint = false;
          });
      
        }
        else {
      
          setState(() {
            hintText = prefix;
            hasHint = true;
          });
      
        }
      
      });
      

      在 InputDecoration 中,这样做:

       InputDecoration(
      
            border: InputBorder.none,
            errorMaxLines: 1,          
            prefixText: hasHint ? '' : prefix,
            prefixStyle: TextStyle(
                color: Colors.black54
            ),
            hasFloatingPlaceholder: true,
            hintText: hasHint ? prefix : '',
            hintStyle: TextStyle(
                fontSize: 15.0,
                color: Colors.black54
            ),
          ),
      

      其中前缀变量是你想成为前缀的任何东西。如果它解决了您的问题,请投票赞成这个答案。 dubace 的回答只是让我的 prefixText 变得完全透明。

      当 textField 没有被关注时,这会放置一个代替 prefixText 的提示。当它被聚焦时,prefixText 出现,提示消失。在 textField 中,考虑放置一个 ontap 方法,在其中设置状态,以便提示消失并显示前缀。因为在某些情况下,当文本字段被点击时,系统仍然无法理解它是否具有焦点,除非实际输入了某些内容。

      【讨论】:

        【解决方案5】:

        如果在 TextField 控制器中定义一个初始值也可以解决这个问题。 由于 TextField 将拥有内容,导致出现前缀。

        像这样:

        TextEditingController currencyField = TextEditingController(text: '0,00');
        
        TextFormField(
          autofocus: false,
          controller: currencyField,
          decoration: InputDecoration(
            border: UnderlineInputBorder(),
            hintText: '0,00',
            prefixText: '\$ ',
            labelText: 'Price',
            floatingLabelBehavior: FloatingLabelBehavior.always,
          ),
          textInputAction: TextInputAction.next,
          keyboardType: TextInputType.number,
          inputFormatters: [
            FilteringTextInputFormatter.digitsOnly,
            CurrencyFormatter(), // My currency formatter
          ],
          enableSuggestions: true,
          enableInteractiveSelection: true,
          toolbarOptions: ToolbarOptions(
            copy: true,
            cut: true,
            paste: true,
            selectAll: true,
          ),
        );
        

        【讨论】:

          【解决方案6】:

          对我来说解决这个问题的方法是(用作文本字段前缀图标)

          return Align(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    '${m4eWalletProvider.wallet.walletInfo.currency.toString()} ',
                    style: Theme.of(context)
                        .textTheme
                        .headline
                        .copyWith(fontWeight: FontWeight.bold),
                  ),
                );
          

          【讨论】:

          • 欢迎来到 Stack Overflow。在 Stack Overflow 上不鼓励仅使用代码的答案,因为它们没有解释它是如何解决问题的。请编辑您的答案以解释此代码的作用以及它如何回答问题,以便对其他有类似问题的用户有用。
          【解决方案7】:

          prefixIcon 解决方法对我不起作用,因为我的输入是多行的。到达两行后,图标居中,而不是像真正的前缀文本那样与第一行保持对齐。

          因此,我发现了CrossAxisAlignment.textBaseline,它允许使用 Row 简单地附加前缀小部件,如下所示:

          Row(
              crossAxisAlignment: CrossAxisAlignment.baseline,
              textBaseline: TextBaseline.alphabetic,
              children: [Text("prefix: "), inputWidget]
          )
          

          【讨论】:

            猜你喜欢
            • 2020-01-28
            • 2020-02-18
            • 2021-10-15
            • 1970-01-01
            • 1970-01-01
            • 2020-10-29
            • 2021-08-24
            • 2020-07-05
            • 2021-12-10
            相关资源
            最近更新 更多