Skip to content

Instantly share code, notes, and snippets.

@tolo
Last active June 18, 2024 14:32
Show Gist options
  • Save tolo/922fd838cdea30b17121533b0012eda4 to your computer and use it in GitHub Desktop.
Save tolo/922fd838cdea30b17121533b0012eda4 to your computer and use it in GitHub Desktop.
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
import 'test_helpers.dart';
void main() {
// ...
/// THIS WORKS
testWidgets('routing config works after routing changes case 3',
(WidgetTester tester) async {
final GlobalKey<_StatefulTestState> key =
GlobalKey<_StatefulTestState>(debugLabel: 'testState');
final GlobalKey<NavigatorState> rootNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'root');
final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>(
RoutingConfig(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (_, __) =>
StatefulTest(key: key, child: const Text('home'))),
],
),
);
addTearDown(config.dispose);
await createRouterWithRoutingConfig(
navigatorKey: rootNavigatorKey,
config,
tester,
errorBuilder: (_, __) => const Text('error'),
);
expect(find.text('home'), findsOneWidget);
key.currentState!.value = 1;
config.value = RoutingConfig(
routes: <RouteBase>[
GoRoute(
path: '/',
builder: (_, __) =>
StatefulTest(key: key, child: const Text('home'))),
GoRoute(path: '/abc', builder: (_, __) => const Text('/abc')),
],
);
await tester.pumpAndSettle();
expect(key.currentState!.value == 1, isTrue);
});
/// THIS CRASHES (duplicate GlobalKey)
testWidgets('routing config works with shell route',
(WidgetTester tester) async {
final GlobalKey<_StatefulTestState> key =
GlobalKey<_StatefulTestState>(debugLabel: 'testState');
final GlobalKey<NavigatorState> rootNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'root');
final GlobalKey<NavigatorState> shellNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'shell');
final ValueNotifier<RoutingConfig> config = ValueNotifier<RoutingConfig>(
RoutingConfig(
routes: <RouteBase>[
ShellRoute(
navigatorKey: shellNavigatorKey,
routes: <RouteBase>[
GoRoute(path: '/', builder: (_, __) => const Text('home')),
],
builder: (_, __, Widget widget) =>
StatefulTest(key: key, child: widget)),
],
),
);
addTearDown(config.dispose);
await createRouterWithRoutingConfig(
navigatorKey: rootNavigatorKey,
config,
tester,
errorBuilder: (_, __) => const Text('error'),
);
expect(find.text('home'), findsOneWidget);
key.currentState!.value = 1;
config.value = RoutingConfig(
routes: <RouteBase>[
ShellRoute(
navigatorKey: shellNavigatorKey,
routes: <RouteBase>[
GoRoute(path: '/', builder: (_, __) => const Text('home')),
GoRoute(path: '/abc', builder: (_, __) => const Text('/abc')),
],
builder: (_, __, Widget widget) =>
StatefulTest(key: key, child: widget)),
],
);
await tester.pumpAndSettle();
expect(key.currentState!.value == 1, isTrue);
});
// ...
}
class StatefulTest extends StatefulWidget {
const StatefulTest({super.key, required this.child});
final Widget child;
@override
State<StatefulWidget> createState() => _StatefulTestState();
}
class _StatefulTestState extends State<StatefulTest> {
int value = 0;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
widget.child,
Text('State: $value'),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment