如何从Flutter中的RichText检索文本值?

huangapple go评论84阅读模式
英文:

How to retrieve text value from RichText in flutter?

问题

以下是Flutter代码部分的翻译:

   Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Padding(
                  padding:
                      const EdgeInsets.only(bottom: LuminaSpacing.extraTiny),
                  child: RichText(
                    key: Key('last_updated'),
                    textAlign: TextAlign.end,
                    text: TextSpan(
                      text: '${_localize.last_updated}: ',
                      style: wldTheme.textTheme.display1,
                      children: <TextSpan>[
                        TextSpan(
                            text: '${settingStore.lastCheckinTime}',
                            style: wldTheme.textTheme.body1),
                      ],
                    ),
                  ),
                ),

以下是Appium Java代码部分的翻译:

FlutterFinder ele = new FlutterFinder(driver);

System.out.println("Last update: "+ele.byValueKey("last_update"));
// System.out.println("Last update: "+ele.byValueKey("last_update").getText());  // It waits for long duration

输出结果为:

Last update: [pro.truongsinh.appium_flutter.finder.FlutterElement@870fb657 -> unknown locator]
英文:

Here's the Flutter code:

   Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: &lt;Widget&gt;[
                Padding(
                  padding:
                      const EdgeInsets.only(bottom: LuminaSpacing.extraTiny),
                  child: RichText(
                    key: Key(&#39;last_updated&#39;),
                    textAlign: TextAlign.end,
                    text: TextSpan(
                      text: &#39;${_localize.last_updated}: &#39;,
                      style: wldTheme.textTheme.display1,
                      children: &lt;TextSpan&gt;[
                        TextSpan(
                            text: &#39;${settingStore.lastCheckinTime}&#39;,
                            style: wldTheme.textTheme.body1),
                      ],
                    ),
                  ),
                ),

I'm using appium with java to automate my test cases

Here's my code:

FlutterFinder ele = new FlutterFinder(driver);

System.out.println(&quot;Last update: &quot;+ele.byValueKey(&quot;last_update&quot;));
// System.out.println(&quot;Last update: &quot;+ele.byValueKey(&quot;last_update&quot;).getText());  // It waits for long duration

The output I get is

Last update: [pro.truongsinh.appium_flutter.finder.FlutterElement@870fb657 -&gt; unknown locator]

答案1

得分: 1

你需要创建一个类似这样的控制器:var textController = TextEditingController();,并将控制器与你的RichText小部件关联起来,使用这个控制器你可以这样获取文本的值:textController.text

英文:

you need to create a controller like this: var textController = TextEditingController(); and aggregate the controller to your RichText widget, with this controller you can get the value of the text like this: textController.text

huangapple
  • 本文由 发表于 2020年1月30日 19:26:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59984958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定