-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add current-tab-press-to-scroll-top feature (#162)
from the [task](https://www.notion.so/leftclick/Fix-feed-85f2d89ba34540fd96dcc6e556cbc00f) > if scrolled, when pressing again on home screen on feed, it should scroll back up if we have some inner page opened and pressing the same tab -> open the root tab page if the root tab page is opened and the scroll is not on top -> scroll it to the top https://github.com/user-attachments/assets/778f9298-83bd-434e-9aad-86904d902eda
- Loading branch information
Showing
7 changed files
with
90 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:ice/app/extensions/extensions.dart'; | ||
import 'package:ice/app/router/main_tabs/components/main_tab_navigation_container.dart'; | ||
|
||
void useScrollTopOnTabPress(BuildContext context, {required ScrollController scrollController}) { | ||
final tabPressStream = MainTabNavigationContainer.of(context).tabPressStream; | ||
|
||
useEffect(() { | ||
final listener = tabPressStream.listen((tabPressData) { | ||
// taking the GoRouterState here instead of out of listener, | ||
// because otherwise it is considered the same even if the screen is changed | ||
final routerState = GoRouterState.of(context); | ||
if ( | ||
// if we pressed the same tab we're currently on | ||
tabPressData.current == tabPressData.pressed && | ||
// if we pressed the tab that our page belongs to | ||
tabPressData.pressed == routerState.currentTab && | ||
// if we're on the root tab page | ||
routerState.topRoute?.path == routerState.fullPath) { | ||
scrollController.animateTo(0, duration: Duration(milliseconds: 500), curve: Curves.easeOut); | ||
} | ||
}); | ||
return () => listener.cancel(); | ||
}, [tabPressStream]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export 'main_tab_button.dart'; | ||
export 'tab_icon.dart'; | ||
export 'tab_item.dart'; | ||
export 'main_tab_navigation_container.dart'; |
27 changes: 27 additions & 0 deletions
27
lib/app/router/main_tabs/components/main_tab_navigation_container.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:ice/app/router/main_tabs/components/components.dart'; | ||
|
||
typedef TabPressSteamData = ({TabItem current, TabItem pressed}); | ||
|
||
class MainTabNavigationContainer extends InheritedWidget { | ||
const MainTabNavigationContainer({ | ||
super.key, | ||
required super.child, | ||
required this.tabPressStream, | ||
}); | ||
|
||
final Stream<TabPressSteamData> tabPressStream; | ||
|
||
static MainTabNavigationContainer of(BuildContext context) { | ||
final MainTabNavigationContainer? result = | ||
context.dependOnInheritedWidgetOfExactType<MainTabNavigationContainer>(); | ||
assert(result != null, 'No MainTabNavigationContainer found in context'); | ||
return result!; | ||
} | ||
|
||
@override | ||
bool updateShouldNotify(MainTabNavigationContainer oldWidget) => | ||
oldWidget.tabPressStream != tabPressStream; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters