2016-12-14 90 views
1

例如,我可能在目前的widget樹一個富文本,看起來像有沒有一種簡單的方法可以在Flutter測試中查找由RichText構建的特定文本?

new RichText(
text: new TextSpan(
    text: 'Hello ', 
    style: DefaultTextStyle.of(context).style, 
    children: <TextSpan>[ 
    new TextSpan(text: 'bold', style: new TextStyle(fontWeight: FontWeight.bold)), 
    new TextSpan(text: ' world!'), 
    ], 
), 
) 

我嘗試使用find.text('Hello bold world!'),但它不工作,因爲它不是一個文本。

回答

2

最簡單的解決方法是在RichText上放一個鍵,然後讀取它。

如果這不適合任何原因,可以使用find.byWidgetPredicate並傳遞一個匹配RichText小部件的函數,其text.toPlainText()返回所需的字符串。

+0

謝謝!我喜歡第二種解決方案。 –

相關問題