TestWidgetsApp class

A minimal WidgetsApp wrapper for use in widget tests.

This provides a convenient way to wrap test widgets with the necessary app-level widgets (like Directionality, MediaQuery, etc.) that WidgetsApp provides, without the overhead of MaterialApp or CupertinoApp.

The pageRouteBuilder creates a Navigator which provides an Overlay, so widgets that need overlay support (like Draggable, RawTooltip, etc.) will work correctly when placed in home.

Example usage:

testWidgets('my test', (WidgetTester tester) async {
  await tester.pumpWidget(
    TestWidgetsApp(
      home: GestureDetector(
        onTap: () {},
        child: Container(),
      ),
    ),
  );
});

For navigation tests with routes:

testWidgets('navigation test', (WidgetTester tester) async {
  await tester.pumpWidget(
    TestWidgetsApp(
      home: const Text('Home'),
      routes: <String, WidgetBuilder>{
        '/details': (BuildContext context) => const Text('Details'),
      },
    ),
  );
  tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/details');
  await tester.pumpAndSettle();
});

For tests that need full control over route generation:

testWidgets('custom route test', (WidgetTester tester) async {
  await tester.pumpWidget(
    TestWidgetsApp(
      initialRoute: '/',
      onGenerateRoute: (RouteSettings settings) {
        return PageRouteBuilder<void>(
          settings: settings,
          pageBuilder: (_, _, _) => const Text('Generated'),
        );
      },
    ),
  );
});
Inheritance

Constructors

TestWidgetsApp({Key? key, GlobalKey<NavigatorState>? navigatorKey, Widget? home, String? initialRoute, RouteFactory? onGenerateRoute, InitialRouteListFactory? onGenerateInitialRoutes, RouteFactory? onUnknownRoute, List<NavigatorObserver> navigatorObservers = const <NavigatorObserver>[], Map<String, WidgetBuilder> routes = const <String, WidgetBuilder>{}, Color color = const Color(0xFFFFFFFF), TextStyle? textStyle, PageRouteFactory pageRouteBuilder = _defaultPageRouteBuilder, TransitionBuilder? builder, Map<ShortcutActivator, Intent>? shortcuts, Map<Type, Action<Intent>>? actions, String? restorationScopeId, Iterable<LocalizationsDelegate<Object?>>? localizationsDelegates, LocaleListResolutionCallback? localeListResolutionCallback})
Creates a minimal WidgetsApp for testing.
const

Properties

actions → Map<Type, Action<Intent>>?
The application's action map.
final
builder → TransitionBuilder?
A builder for inserting widgets above the Navigator.
final
color → Color
The primary color to use for the application in the operating system interface.
final
hashCode → int
The hash code for this object.
no setterinherited
home → Widget?
The widget displayed when the app launches.
final
initialRoute → String?
The name of the first route to show when the app launches.
final
key → Key?
Controls how one widget replaces another widget in the tree.
finalinherited
localeListResolutionCallback → LocaleListResolutionCallback?
The callback responsible for choosing the app's locale when the app is started, and when the user changes the device's locale.
final
localizationsDelegates → Iterable<LocalizationsDelegate<Object?>>?
The delegates for this app's Localizations widget.
final
A key to use when building the Navigator.
final
A list of NavigatorObserver for the app's Navigator.
final
onGenerateInitialRoutes → InitialRouteListFactory?
The routes generator callback used for generating initial routes if initialRoute is provided.
final
onGenerateRoute → RouteFactory?
The route generator callback used when the app is navigated to a named route.
final
onUnknownRoute → RouteFactory?
Called when onGenerateRoute fails to generate a route, except for the initialRoute.
final
pageRouteBuilder → PageRouteFactory
A function that creates page routes for named navigation.
final
restorationScopeId → String?
The identifier to use for state restoration of the app's Navigator.
final
routes → Map<String, WidgetBuilder>
The application's top-level routing table.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
shortcuts → Map<ShortcutActivator, Intent>?
The application's keyboard shortcut map.
final
textStyle → TextStyle?
The default text style for Text widgets in the app.
final

Methods

build(BuildContext context) → Widget
Describes the part of the user interface represented by this widget.
override
createElement() → StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
debugDescribeChildren() → List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) → String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String
Returns a one-line detailed description of the object.
inherited
toStringShort() → String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited