Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access children/parent widgets from a given widget, in a Flutter test?

Tags:

I'm writing unit and integration tests for Flutter. Is it possible to access children/parent widgets from a given widget?

like image 450
Seth Ladd Avatar asked Nov 14 '17 22:11

Seth Ladd


People also ask

How do you call parent function from child widget in Flutter?

To call a function of a parent, you can use the callback pattern. In this example, a function on the color selected is passed to the child. The child calls the function when a button is pressed: import 'package:flutter/material.


1 Answers

Yes, you can use find.descendant and Element.ancestorWidgetOfExactType.

Examples:

// Finds a RichText widget that a descendant (child/grand-child/etc) of a // tab widget with text "Tab 1" find.descendant(of: find.text('Tab 1'), matching: find.byType(RichText));  // Finds a parent widget of type MyParentWidget. tester.element(find.byType(MyChildWidget))   .ancestorWidgetOfExactType(MyParentWidget); 
like image 117
Yegor Avatar answered Sep 22 '22 05:09

Yegor