【问题标题】:Flutter won't rebuild a textFlutter 不会重建文本
【发布时间】:2021-03-04 13:58:58
【问题描述】:

在我的应用中,我有一些 (youtube) 链接,它们的文本值始终为“Link”,但随着页面内容的变化,它们的点击启动参数会有所不同。

我认识到应用首先使用初始正确值构建这些链接,然后,一旦发生更改,它们将不会得到更新。

  RichText _createExerciseLink(String url) {
    return new RichText(
      text: new TextSpan(
        children: [
          new TextSpan(
            text: Translate.of(context).trans("exercise.link"),
        
            recognizer: new TapGestureRecognizer()
              ..onTap = () {
                print("Launcihng: ${url}");
                launch(url);
              },
          ),
        ],
      ),
    );
  }

但是,如果我将文本“链接”更改为相应的 HTTP 链接,那么它会重新呈现...

  RichText _createExerciseLink(String url) {
    return new RichText(
      text: new TextSpan(
        children: [
          new TextSpan(
            text: url,
   
            recognizer: new TapGestureRecognizer()
              ..onTap = () {
                print("Launcihng: ${url}");
                launch(url);
              },
          ),
        ],
      ),
    );
  }

我认为这是某种缓存机制?

有没有办法强制重新创建整个小部件?

【问题讨论】:

  • 我的建议有帮助吗?

标签: flutter


【解决方案1】:

似乎添加Key 是这里的诀窍:

父小部件:

            Key key = new Key(link);
               ...
            link != null ? _createExerciseLink(link, key) : Container(),

在正文中:


  RichText _createExerciseLink(String url, Key key) {
    return new RichText(
      key: key,
      text: new TextSpan(
        children: [
          new TextSpan(
            text: Translate.of(context).trans("exercise.link"),

            recognizer: new TapGestureRecognizer()
              ..onTap = () {
                print("Launcihng: ${url}");
                launch(url);
              },
          ),
        ],
      ),
    );
  }

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 2019-12-09
    • 2020-02-18
    • 2020-10-14
    • 2021-03-03
    • 1970-01-01
    • 2018-10-09
    • 2020-02-23
    • 2021-10-22
    相关资源
    最近更新 更多