简体   繁体   English

将参数传递给 Flutter 中的无状态小部件

[英]Pass params to stateless widget in Flutter

I am trying to do a stateless widget for inline text links.我正在尝试为内联文本链接做一个无状态小部件。

I am using this answer here https://stackoverflow.com/a/55607224/3808307 to create the links我在这里使用这个答案https://stackoverflow.com/a/55607224/3808307创建链接

RichText(
  text: TextSpan(
    children: [
      TextSpan(text: 'This is a going to be a Text which has '),
      TextSpan(
        text: 'single tap',
        style: style,
        recognizer: TapGestureRecognizer()
          ..onTap = () {
            // single tapped
          },
      ),
      TextSpan(text: ' along with '),
      TextSpan(
        text: 'double tap',
        style: style,
        recognizer: DoubleTapGestureRecognizer()
          ..onDoubleTap = () {
            // double tapped
          },
      ),
      TextSpan(text: ' and '),
      TextSpan(
        text: 'long press',
        style: style,
        recognizer: LongPressGestureRecognizer()
          ..onLongPress = () {
            // long pressed
          },
      ),
    ],
  ),
)

, but I would like to have a TextSpan that I can import, and already has the styling applied, passing it a text that would act as label and a function. ,但我想要一个可以导入的 TextSpan,并且已经应用​​了样式,向它传递了一个充当标签和函数的文本。

Do I need a stateful widget for this, or can I just use a stateless widget?我是否需要一个有状态的小部件,或者我可以只使用一个无状态的小部件?

You can use a stateless widget without a problem.您可以毫无问题地使用无状态小部件。

In the case that you need to change the style of the text when you press the links you will need to use a stateful widget.如果您需要在按下链接时更改文本样式,则需要使用有状态小部件。

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

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