【问题标题】:How can i change color of hashtags flutter on textformfield?如何更改 textformfield 上主题标签的颜色?
【发布时间】:2021-11-23 13:35:48
【问题描述】:

我想在包含主题标签(#)时更改输入文本颜色,而不更改不包含主题标签的其他文本颜色

    TextFormField(
                                        minLines: 1,
                                        maxLines:
                                            5, // allow user to enter 5 line in textfield
                                        keyboardType: TextInputType
                                            .multiline, // user keyboard will have a button to move cursor to next line

                                        controller:
                                            postController.captionController,

                                        decoration: const InputDecoration.collapsed(
                                          hintText: 'caption',
                                          hintStyle: TextStyle(    
                                                                                    fontSize: 15,
)
                                          
                                        ),
                                        style: const TextStyle(
                                            color: Colors.black,
                                            fontSize: 18,
                                            fontWeight: FontWeight.bold),
                                        validator: (value) {
                                          return postController
                                              .validateCaption(value!);
                                        },
                                      ),

【问题讨论】:

标签: flutter textformfield


【解决方案1】:

我能够通过使用rich_text_controller 包来实现此功能。

这是我的例子:

class AppContent extends StatefulWidget {
  const AppContent({Key? key}) : super(key: key);

  @override
  _AppContentState createState() => _AppContentState();
}

class _AppContentState extends State<AppContent> {
  // Add a controller
  late RichTextController _controller;

  @override
  void initState() {
    _controller = RichTextController(
      patternMatchMap: {
        RegExp(r"\B#[a-zA-Z0-9]+\b"): TextStyle(color: Colors.cyan),
      },
      onMatch: (List<String> matches) {
        // Do something with matches.
      },
      deleteOnBack: true,
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TextField(
          controller: _controller,
        )
      ],
    );
  }
}

【讨论】:

    【解决方案2】:

    您可以像这样使用 .contains() 方法:

    String myString = "#Text";
    bool hasHash = myString.contains('#'); // yields true
    

    然后在您的 TextStyle() 中使用三元表达式:

    TextStyle(color:hasHash ? Colors.green : Colors.red;)
    

    现在将它应用到您的输入控制器。请参阅 Flutter Cookbock 中的 link。这是简短的表格,但我建议逐步按照文档中的步骤进行操作:

    1. 创建控制器:final myController = TextEditingController();
    2. 将其添加到表单中的字段:TextField(controller: myController);
    3. 检索当前值:myController.text
    4. 使用上面的逻辑:bool hasHash = myController.text.contains('#')
    5. 动态输出颜色:TextStyle( color:hasHash ? Colors.green : Colors.red;

    【讨论】:

    • 如果用户键入时主题标签在前,我想更改输入文本的颜色。我怎样才能做到这一点?
    • @stock 您只需将起始索引 (0) 作为参数传递给 .contains 方法 - 对于该基本行为,您不需要额外的包。既然你已经接受了答案,我只会给你谷歌的焦点节点类型;)
    猜你喜欢
    • 2023-03-26
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2020-01-22
    • 2022-08-18
    相关资源
    最近更新 更多