Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Keyboard visibility changes disrupt route transition animation in NavigationSheet #215

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@
"type": "dart",
"program": "lib/tutorial/keyboard_dismiss_behavior.dart",
"cwd": "./cookbook"
},
{
"name": "Navigation Sheet and Keyboard",
"request": "launch",
"type": "dart",
"program": "lib/tutorial/navigation_sheet_and_keyboard.dart",
"cwd": "./cookbook"
}
]
}
154 changes: 154 additions & 0 deletions cookbook/lib/tutorial/navigation_sheet_and_keyboard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:smooth_sheets/smooth_sheets.dart';

void main() {
runApp(MaterialApp.router(routerConfig: _router));
}

final _transitionObserver = NavigationSheetTransitionObserver();

final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const _Home(),
routes: [
ShellRoute(
observers: [_transitionObserver],
pageBuilder: (context, state, child) {
return ModalSheetPage(
child: _MySheet(
transitionObserver: _transitionObserver,
navigator: child,
),
);
},
routes: [
GoRoute(
path: 'a',
pageBuilder: (context, state) {
return DraggableNavigationSheetPage(
key: state.pageKey,
child: const _EditablePageContent(
height: 600,
nextLocation: '/a/b',
autofocus: true,
),
);
},
routes: [
GoRoute(
path: 'b',
pageBuilder: (context, state) {
return DraggableNavigationSheetPage(
key: state.pageKey,
child: const _EditablePageContent(
height: 300,
nextLocation: '/a/b/c',
autofocus: true,
),
);
},
routes: [
GoRoute(
path: 'c',
pageBuilder: (context, state) {
return DraggableNavigationSheetPage(
key: state.pageKey,
child: const _EditablePageContent(
nextLocation: '/',
height: double.infinity,
autofocus: false,
),
);
},
),
],
),
],
),
],
),
],
),
],
);

class _Home extends StatelessWidget {
const _Home();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
GoRouter.of(context).go('/a');
},
child: const Text('Open Sheet'),
),
),
);
}
}

class _MySheet extends StatelessWidget {
const _MySheet({
required this.transitionObserver,
required this.navigator,
});

final NavigationSheetTransitionObserver transitionObserver;
final Widget navigator;

@override
Widget build(BuildContext context) {
return NavigationSheet(
transitionObserver: transitionObserver,
child: ColoredBox(
color: Colors.white,
child: navigator,
),
);
}
}

class _EditablePageContent extends StatelessWidget {
const _EditablePageContent({
required this.nextLocation,
required this.autofocus,
required this.height,
});

final double height;
final String nextLocation;
final bool autofocus;

@override
Widget build(BuildContext context) {
return SheetContentScaffold(
body: SizedBox(
height: height,
child: Column(
children: [
TextField(
autofocus: autofocus,
),
ElevatedButton(
onPressed: () => context.go(nextLocation),
child: const Text('Next'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Back'),
),
],
),
),
);
}
}
4 changes: 4 additions & 0 deletions package/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.9.2 Aug 14, 2024

- Fix: Keyboard visibility changes disrupt route transition animation in NavigationSheet (#215)

## 0.9.1 Jul 30, 2024

- Fix: Sometimes touch is ignored when scrollable sheet reaches edge (#209)
Expand Down
18 changes: 18 additions & 0 deletions package/lib/src/navigation/navigation_sheet_activity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ class TransitionSheetActivity extends NavigationSheetActivity {
owner.setPixels(lerpDouble(startPixels, endPixels, fraction)!);
}
}

@override
void didFinalizeDimensions(
Size? oldContentSize,
Size? oldViewportSize,
EdgeInsets? oldViewportInsets,
) {
// Appends the delta of the bottom inset (typically the keyboard height)
// to keep the visual sheet position unchanged.
final newInsets = owner.metrics.viewportInsets;
final oldInsets = oldViewportInsets ?? newInsets;
final deltaInsetBottom = newInsets.bottom - oldInsets.bottom;
if (deltaInsetBottom != 0) {
owner
..setPixels(owner.metrics.pixels - deltaInsetBottom)
..didUpdateMetrics();
}
}
}

@internal
Expand Down
2 changes: 1 addition & 1 deletion package/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: smooth_sheets
description: Sheet widgets with smooth motion and great flexibility. Also supports nested navigation in both imperative and declarative ways.
version: 0.9.1
version: 0.9.2
repository: https://github.com/fujidaiti/smooth_sheets
screenshots:
- description: Practical examples of smooth_sheets.
Expand Down
Loading