Skip to content

Commit 396fa75

Browse files
authored
Add transitionDuration parameter (#5854)
Adds transitionDuration parameter for specifying how long the animation should be. This adds the following suggestion from the issue flutter/flutter#114272 The duration of the animations are set to 1 second, we should have the option to set animation durations when creating slot layouts. Originally posted by @jamiewest in flutter/flutter#114272 (comment)
1 parent cd621aa commit 396fa75

File tree

9 files changed

+77
-9
lines changed

9 files changed

+77
-9
lines changed

packages/flutter_adaptive_scaffold/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.8
2+
3+
* Adds `transitionDuration` parameter for specifying how long the animation should be.
4+
15
## 0.1.7+2
26

37
* Fixes new lint warnings.

packages/flutter_adaptive_scaffold/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ Widget build(BuildContext context) {
4848
),
4949
)
5050
];
51-
5251
return AdaptiveScaffold(
52+
// An option to override the default transition duration.
53+
transitionDuration: Duration(milliseconds: _transitionDuration),
5354
// An option to override the default breakpoints used for small, medium,
5455
// and large.
5556
smallBreakpoint: const WidthPlatformBreakpoint(end: 700),
@@ -139,6 +140,8 @@ displayed and the entrance animation and exit animation.
139140
// AdaptiveLayout has a number of slots that take SlotLayouts and these
140141
// SlotLayouts' configs take maps of Breakpoints to SlotLayoutConfigs.
141142
return AdaptiveLayout(
143+
// An option to override the default transition duration.
144+
transitionDuration: Duration(milliseconds: _transitionDuration),
142145
// Primary navigation config has nothing from 0 to 600 dp screen width,
143146
// then an unextended NavigationRail with no labels and just icons then an
144147
// extended NavigationRail with both icons and labels.

packages/flutter_adaptive_scaffold/example/lib/adaptive_layout_demo.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,27 @@ class MyApp extends StatelessWidget {
2626
/// [AdaptiveLayout].
2727
class MyHomePage extends StatefulWidget {
2828
/// Creates a const [MyHomePage].
29-
const MyHomePage({super.key});
29+
const MyHomePage({super.key, this.transitionDuration = 1000});
30+
31+
/// Declare transition duration.
32+
final int transitionDuration;
3033

3134
@override
3235
State<MyHomePage> createState() => _MyHomePageState();
3336
}
3437

3538
class _MyHomePageState extends State<MyHomePage> {
3639
int selectedNavigation = 0;
40+
int _transitionDuration = 1000;
41+
42+
// Initialize transition time variable.
43+
@override
44+
void initState() {
45+
super.initState();
46+
setState(() {
47+
_transitionDuration = widget.transitionDuration;
48+
});
49+
}
3750

3851
@override
3952
Widget build(BuildContext context) {
@@ -158,6 +171,8 @@ class _MyHomePageState extends State<MyHomePage> {
158171
// AdaptiveLayout has a number of slots that take SlotLayouts and these
159172
// SlotLayouts' configs take maps of Breakpoints to SlotLayoutConfigs.
160173
return AdaptiveLayout(
174+
// An option to override the default transition duration.
175+
transitionDuration: Duration(milliseconds: _transitionDuration),
161176
// Primary navigation config has nothing from 0 to 600 dp screen width,
162177
// then an unextended NavigationRail with no labels and just icons then an
163178
// extended NavigationRail with both icons and labels.

packages/flutter_adaptive_scaffold/example/lib/adaptive_scaffold_demo.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,27 @@ class MyApp extends StatelessWidget {
4545
/// [AdaptiveScaffold].
4646
class MyHomePage extends StatefulWidget {
4747
/// Creates a const [MyHomePage].
48-
const MyHomePage({super.key});
48+
const MyHomePage({super.key, this.transitionDuration = 1000});
49+
50+
/// Declare transition duration.
51+
final int transitionDuration;
4952

5053
@override
5154
State<MyHomePage> createState() => _MyHomePageState();
5255
}
5356

5457
class _MyHomePageState extends State<MyHomePage> {
5558
int _selectedTab = 0;
59+
int _transitionDuration = 1000;
60+
61+
// Initialize transition time variable.
62+
@override
63+
void initState() {
64+
super.initState();
65+
setState(() {
66+
_transitionDuration = widget.transitionDuration;
67+
});
68+
}
5669

5770
// #docregion Example
5871
@override
@@ -68,8 +81,9 @@ class _MyHomePageState extends State<MyHomePage> {
6881
),
6982
)
7083
];
71-
7284
return AdaptiveScaffold(
85+
// An option to override the default transition duration.
86+
transitionDuration: Duration(milliseconds: _transitionDuration),
7387
// An option to override the default breakpoints used for small, medium,
7488
// and large.
7589
smallBreakpoint: const WidthPlatformBreakpoint(end: 700),

packages/flutter_adaptive_scaffold/example/pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ dependencies:
1212
sdk: flutter
1313
flutter_adaptive_scaffold:
1414
path: ..
15-
flutter_lints: ^2.0.0
1615

1716
dev_dependencies:
1817
build_runner: ^2.1.10

packages/flutter_adaptive_scaffold/example/test/adaptive_scaffold_demo_test.dart

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ void main() {
1515
final Finder pnav = find.byKey(const Key('primaryNavigation'));
1616
final Finder pnav1 = find.byKey(const Key('primaryNavigation1'));
1717

18-
Future<void> updateScreen(double width, WidgetTester tester) async {
18+
Future<void> updateScreen(double width, WidgetTester tester,
19+
{int transitionDuration = 1000}) async {
1920
await tester.binding.setSurfaceSize(Size(width, 800));
2021
await tester.pumpWidget(
2122
MaterialApp(
2223
home: MediaQuery(
2324
data: MediaQueryData(size: Size(width, 800)),
24-
child: const example.MyHomePage()),
25+
child: example.MyHomePage(
26+
transitionDuration: transitionDuration,
27+
)),
2528
),
2629
);
2730
}
@@ -98,4 +101,21 @@ void main() {
98101
expect(tester.getTopLeft(sBody), const Offset(400, 0));
99102
expect(tester.getBottomRight(sBody), const Offset(800, 800));
100103
});
104+
105+
testWidgets('animation plays correctly in declared duration',
106+
(WidgetTester tester) async {
107+
final Finder b = find.byKey(const Key('body'));
108+
final Finder sBody = find.byKey(const Key('sBody'));
109+
110+
await updateScreen(400, tester, transitionDuration: 500);
111+
await updateScreen(800, tester, transitionDuration: 500);
112+
113+
await tester.pump();
114+
await tester.pump(const Duration(milliseconds: 500));
115+
116+
expect(tester.getTopLeft(b), const Offset(88, 0));
117+
expect(tester.getBottomRight(b), const Offset(400, 800));
118+
expect(tester.getTopLeft(sBody), const Offset(400, 0));
119+
expect(tester.getBottomRight(sBody), const Offset(800, 800));
120+
});
101121
}

packages/flutter_adaptive_scaffold/lib/src/adaptive_layout.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class AdaptiveLayout extends StatefulWidget {
117117
this.body,
118118
this.secondaryBody,
119119
this.bodyRatio,
120+
this.transitionDuration = const Duration(seconds: 1),
120121
this.internalAnimations = true,
121122
this.bodyOrientation = Axis.horizontal,
122123
});
@@ -181,6 +182,11 @@ class AdaptiveLayout extends StatefulWidget {
181182
/// hinge when there is one.
182183
final double? bodyRatio;
183184

185+
/// Defines the duration of transition between layouts.
186+
///
187+
/// Defaults to [Duration(seconds: 1)].
188+
final Duration transitionDuration;
189+
184190
/// Whether or not the developer wants the smooth entering slide transition on
185191
/// [secondaryBody].
186192
///
@@ -213,7 +219,7 @@ class _AdaptiveLayoutState extends State<AdaptiveLayout>
213219
void initState() {
214220
if (widget.internalAnimations) {
215221
_controller = AnimationController(
216-
duration: const Duration(seconds: 1),
222+
duration: widget.transitionDuration,
217223
vsync: this,
218224
)..forward();
219225
} else {

packages/flutter_adaptive_scaffold/lib/src/adaptive_scaffold.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class AdaptiveScaffold extends StatefulWidget {
9595
this.largeBreakpoint = Breakpoints.large,
9696
this.drawerBreakpoint = Breakpoints.smallDesktop,
9797
this.internalAnimations = true,
98+
this.transitionDuration = const Duration(seconds: 1),
9899
this.bodyOrientation = Axis.horizontal,
99100
this.onSelectedIndexChange,
100101
this.useDrawer = true,
@@ -198,6 +199,11 @@ class AdaptiveScaffold extends StatefulWidget {
198199
/// Defaults to true.
199200
final bool internalAnimations;
200201

202+
/// Defines the duration of transition between layouts.
203+
///
204+
/// Defaults to [Duration(seconds: 1)].
205+
final Duration transitionDuration;
206+
201207
/// The orientation of the body and secondaryBody. Either horizontal (side by
202208
/// side) or vertical (top to bottom).
203209
///
@@ -522,6 +528,7 @@ class _AdaptiveScaffoldState extends State<AdaptiveScaffold> {
522528
)
523529
: null,
524530
body: AdaptiveLayout(
531+
transitionDuration: widget.transitionDuration,
525532
bodyOrientation: widget.bodyOrientation,
526533
bodyRatio: widget.bodyRatio,
527534
internalAnimations: widget.internalAnimations,

packages/flutter_adaptive_scaffold/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_adaptive_scaffold
22
description: Widgets to easily build adaptive layouts, including navigation elements.
3-
version: 0.1.7+2
3+
version: 0.1.8
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_adaptive_scaffold%22
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_adaptive_scaffold
66

0 commit comments

Comments
 (0)