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

Pass params to stateless widget in Flutter

我正在尝试为内联文本链接制作一个无状态小部件。

我在这里使用这个答案来创建链接

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
          },
      ),
    ],
  ),
)

,但我想要一个可以导入的 TextSpan,并且已经应用​​了样式,向它传递一个文本作为标签和一个函数。

为此我需要一个有状态的小部件,还是我可以只使用一个无状态的小部件?

您可以毫无问题地使用无状态小部件。

如果您需要在按下链接时更改文本样式,则需要使用有状态小部件。