Skip to content

Commit ae4cffd

Browse files
committed
Trying out go_router support for OpenContainer
1 parent cde5b36 commit ae4cffd

8 files changed

Lines changed: 632 additions & 96 deletions

File tree

packages/animations/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.3.0
2+
3+
* Added OpenContainerPage and onOpen hook to OpenContainer for go_router compatibility.
4+
15
## 2.2.0
26

37
* Adds support for custom `closedShadows` and `openShadows` to `OpenContainer`.

packages/animations/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,31 @@ _Examples of the fade pattern:_
7272
2. _A menu_
7373
3. _A snackbar_
7474
4. _A FAB_
75+
76+
## Usage with Declarative Routers (e.g. go_router)
77+
78+
The `OpenContainer` widget can be integrated with declarative routers like `go_router` to ensure that the browser URL updates when the container opens, while still preserving the container transform animation.
79+
80+
To do this, use the `onOpen` hook to trigger the navigation and `OpenContainerPage` in your route definition. Both must share the same `transitionTag`.
81+
82+
```dart
83+
// In your list page:
84+
OpenContainer(
85+
transitionTag: 'item-${item.id}',
86+
onOpen: () => context.push('/details/${item.id}'),
87+
closedBuilder: (context, action) => MyListTile(onTap: action),
88+
openBuilder: (context, action) => MyDetailsPage(id: item.id),
89+
)
90+
91+
// In your router configuration:
92+
GoRoute(
93+
path: '/details/:id',
94+
pageBuilder: (context, state) {
95+
final String id = state.pathParameters['id']!;
96+
return OpenContainerPage(
97+
transitionTag: 'item-$id',
98+
openBuilder: (context, action) => MyDetailsPage(id: id),
99+
);
100+
},
101+
)
102+
```
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:animations/animations.dart';
6+
import 'package:flutter/material.dart';
7+
import 'package:go_router/go_router.dart';
8+
9+
void main() {
10+
runApp(const GoRouterExampleApp());
11+
}
12+
13+
/// An example of integrating package:animations with package:go_router.
14+
class GoRouterExampleApp extends StatelessWidget {
15+
/// Creates a [GoRouterExampleApp].
16+
const GoRouterExampleApp({super.key});
17+
18+
@override
19+
Widget build(BuildContext context) {
20+
return MaterialApp.router(
21+
title: 'OpenContainer go_router Example',
22+
routerConfig: _router,
23+
);
24+
}
25+
}
26+
27+
final GoRouter _router = GoRouter(
28+
routes: <RouteBase>[
29+
GoRoute(
30+
path: '/',
31+
builder: (BuildContext context, GoRouterState state) {
32+
return const HomeScreen();
33+
},
34+
routes: <RouteBase>[
35+
GoRoute(
36+
path: 'details/:id',
37+
pageBuilder: (BuildContext context, GoRouterState state) {
38+
final String id = state.pathParameters['id']!;
39+
return OpenContainerPage<void>(
40+
transitionTag: 'item-$id',
41+
openBuilder: (BuildContext context, VoidCallback closeContainer) {
42+
return DetailsScreen(id: id);
43+
},
44+
);
45+
},
46+
),
47+
],
48+
),
49+
],
50+
);
51+
52+
/// The home screen of the go_router example.
53+
class HomeScreen extends StatelessWidget {
54+
/// Creates a [HomeScreen].
55+
const HomeScreen({super.key});
56+
57+
@override
58+
Widget build(BuildContext context) {
59+
return Scaffold(
60+
appBar: AppBar(title: const Text('OpenContainer go_router Example')),
61+
body: ListView.builder(
62+
itemCount: 20,
63+
itemBuilder: (BuildContext context, int index) {
64+
final id = index.toString();
65+
return OpenContainer(
66+
transitionTag: 'item-$id',
67+
onOpen: () {
68+
context.go('/details/$id');
69+
return Future<void>.value();
70+
},
71+
closedBuilder: (BuildContext context, VoidCallback openContainer) {
72+
return ListTile(
73+
onTap: openContainer,
74+
title: Text('Item $id'),
75+
subtitle: const Text('Tap to open with container transform'),
76+
);
77+
},
78+
openBuilder: (BuildContext context, VoidCallback closeContainer) {
79+
return DetailsScreen(id: id);
80+
},
81+
);
82+
},
83+
),
84+
);
85+
}
86+
}
87+
88+
/// The details screen of the go_router example.
89+
class DetailsScreen extends StatelessWidget {
90+
/// Creates a [DetailsScreen].
91+
const DetailsScreen({super.key, required this.id});
92+
93+
/// The ID of the item to display.
94+
final String id;
95+
96+
@override
97+
Widget build(BuildContext context) {
98+
return Scaffold(
99+
appBar: AppBar(title: Text('Details of Item $id')),
100+
body: Center(
101+
child: Column(
102+
mainAxisAlignment: MainAxisAlignment.center,
103+
children: <Widget>[
104+
Text(
105+
'Details for Item $id',
106+
style: Theme.of(context).textTheme.headlineMedium,
107+
),
108+
const SizedBox(height: 16),
109+
ElevatedButton(
110+
onPressed: () => context.pop(),
111+
child: const Text('Go Back'),
112+
),
113+
],
114+
),
115+
),
116+
);
117+
}
118+
}

packages/animations/example/lib/main.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:flutter/scheduler.dart';
88
import 'container_transition.dart';
99
import 'fade_scale_transition.dart';
1010
import 'fade_through_transition.dart';
11+
import 'go_router_example.dart';
1112
import 'shared_axis_transition.dart';
1213

1314
void main() {
@@ -94,6 +95,19 @@ class _TransitionsHomePageState extends State<_TransitionsHomePage> {
9495
);
9596
},
9697
),
98+
_TransitionListTile(
99+
title: 'go_router integration',
100+
subtitle: 'OpenContainerPage',
101+
onTap: () {
102+
Navigator.of(context).push(
103+
MaterialPageRoute<void>(
104+
builder: (BuildContext context) {
105+
return const GoRouterExampleApp();
106+
},
107+
),
108+
);
109+
},
110+
),
97111
],
98112
),
99113
),

packages/animations/example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies:
1515
cupertino_icons: ^1.0.2
1616
flutter:
1717
sdk: flutter
18+
go_router: ^14.0.0
1819

1920
dev_dependencies:
2021
flutter_test:

0 commit comments

Comments
 (0)