【问题标题】:how can we add superscript or subscript String on TextForm in Flutter?我们如何在 Flutter 中的 TextForm 上添加上标或下标字符串?
【发布时间】:2021-02-14 04:55:09
【问题描述】:

我的要求是在我的 Textform 标签中复制下图所示的标签文本,并在另一张图片中显示提示。我已经提到了这个answer,但它的 unicode 有限,也使用了 Easy Rich Text 库,但它为我提供了小部件,而不仅仅是用于标签文本和提示文本的字符串。

我怎样才能做到这一点?谢谢。底部附上代码。

@override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: controller,
      keyboardType: TextInputType.numberWithOptions(signed: true,decimal: true),
      decoration: setInputDecoration(text),
      cursorHeight: 20,
      inputFormatters: <TextInputFormatter>[
        FilteringTextInputFormatter.allow(RegExp("[0-9.]")),
      ],
      validator: (value){
        if(value.isEmpty){
          return emptyString;
        }else if(CalculatorManager().isNotValidDouble(value)){
          return invalidInput;
        }
        return null;
      },
    );
  }

  setInputDecoration(String text) {
    return InputDecoration(
      contentPadding: EdgeInsets.fromLTRB(5.0, 3.0, 1.0, 3.0),
      filled: true,
      fillColor: Colors.white,
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.lightBlueAccent, width: 2.0),
      ),
      enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.lightBlueAccent, width: 1.0),
      ),
      hintText: text,
      labelText: text,
      hintStyle: TextStyle(
        color: Colors.grey,
      ),
      border: OutlineInputBorder(
        borderRadius: BorderRadius.all(
          Radius.circular(10.0),
        ),
        borderSide: BorderSide.none,
      ),
    );
  }

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    我可以看到您在下标字母中使用了一些。我建议创建这些地图并在您的代码中使用它。 因为我们无法为ₘᵢₙ 提供一些东西,所以我们可以组合使用 Unicode。

      Map<String, String> ksub_map = {
        'min': '\u2098\u1D62\u2099',
        't': '\u209C',
        'e': '\u2091',
      };
    

    您可以在代码中这样检索它。

      print(ksub_map['min']);  // ₘᵢₙ
      print(ksub_map['t']);  // ₜ
      print(ksub_map['e']);  // ₑ
    

    【讨论】:

    • 谢谢,但我的回答中已经提到了它,而且这个 unicodes 是有限的。例如,我如何下标“min”?
    • @Swapnil 可以组合使用。像这样 '\u2098\u1D62\u2099' => 这将导致 ₘᵢₙ。
    • 谢谢,这将在某些情况下工作,但希望有一个更清洁和强大的解决方案。
    • 因为使用这个没有任何干净的解决方案。谢谢:)
    【解决方案2】:

    【讨论】:

    • 请不要发布“仅链接”的答案。一旦链接更改,它们就会变坏。我们的目标是成为网络搜索的终点,而不是另一个松散滑的垫脚石。
    猜你喜欢
    • 2019-06-12
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多