-
Notifications
You must be signed in to change notification settings - Fork 8
Add Swipe support between navbar pages #55
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
Open
bebaoboy
wants to merge
11
commits into
maheshj01:main
Choose a base branch
from
bebaoboy:issue-42-swipe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5b62253
first commit
bebaoboy e41ae50
finish base feature
bebaoboy 6ff7e3d
Merge branch 'issue-42-swipe' into main
bebaoboy 1116ba7
Merge pull request #1 from bebaoboy/main
bebaoboy f358ec4
add variables to control swipe + update example app
bebaoboy 2b93618
added tests
bebaoboy 4652402
make docs more clear
bebaoboy 7953798
fix landscape mode
bebaoboy f55c3e1
update as requested:
bebaoboy 34a1131
minor fix
bebaoboy 74066a9
Merge branch 'main' into issue-42-swipe
bebaoboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,96 @@ | ||
// ignore_for_file: public_member_api_docs, sort_constructors_first, avoid_print | ||
import 'package:flutter/material.dart'; | ||
import 'package:navbar_router/navbar_router.dart'; | ||
import 'package:navbar_router/src/navbar_swipeable_utls.dart'; | ||
|
||
class Gesture { | ||
bool dragging = false; | ||
int pageViewIndex = 0; | ||
late PageController pageController; | ||
double Function() getPadding; | ||
BuildContext context; | ||
Gesture({ | ||
required this.getPadding, | ||
required this.context, | ||
}); | ||
|
||
/// Swipeable functions below | ||
// convert scrollable pixels to current index | ||
double getPageFromPixels(context) { | ||
return pageController.offset / | ||
(MediaQuery.of(context).size.width - getPadding()); | ||
} | ||
|
||
double getPixelsFromPage(int page) { | ||
return (MediaQuery.of(context).size.width - getPadding()) * page; | ||
} | ||
|
||
// control when user can swipe to other page | ||
bool handleOverscroll(OverscrollNotification value) { | ||
if (!dragging) return false; | ||
print(value.overscroll); | ||
if (value.overscroll < 0 && pageController.offset + value.overscroll <= 0) { | ||
if (pageController.offset != 0) { | ||
pageController.jumpTo(0); | ||
} | ||
return true; | ||
} | ||
if (pageController.offset + value.overscroll >= | ||
pageController.position.maxScrollExtent) { | ||
if (pageController.offset != pageController.position.maxScrollExtent) { | ||
pageController.jumpTo(pageController.position.maxScrollExtent); | ||
} | ||
return true; | ||
} | ||
pageController.jumpTo(pageController.offset + value.overscroll); | ||
|
||
return true; | ||
} | ||
|
||
void onDragStart(details) { | ||
if (dragging) return; | ||
if (details.localPosition.dx <= kDragAreaWidth || | ||
details.localPosition.dx >= | ||
MediaQuery.of(context).size.width - kDragAreaWidth) { | ||
dragging = true; | ||
} | ||
} | ||
|
||
double? onDragUpdate(DragUpdateDetails details) { | ||
// print(details.delta); | ||
double? newOffset; | ||
if (dragging) { | ||
var page = getPageFromPixels(context); | ||
// print(page); | ||
if ((page == 0 && details.delta.dx > 0.1) || | ||
(page >= NavbarNotifier.length - 1 && details.delta.dx < -0.1)) { | ||
return null; | ||
} | ||
double newOffset = pageController.offset - details.delta.dx; | ||
print(newOffset / getPixelsFromPage(NavbarNotifier.currentIndex)); | ||
|
||
// handle fade animation when swiping | ||
|
||
pageController.jumpTo(newOffset); | ||
} | ||
return newOffset; | ||
} | ||
|
||
int onDragEnd(details) { | ||
print(pageController.offset); | ||
// when user release the drag, we calculate which page they're on | ||
var page = getPageFromPixels(context); | ||
print(page); | ||
int value = page.round(); | ||
if (value < 0) { | ||
pageController.animateTo(pageController.position.minScrollExtent, | ||
duration: Durations.long1, curve: Curves.ease); | ||
} else if (value >= NavbarNotifier.length) { | ||
pageController.animateTo(pageController.position.maxScrollExtent, | ||
duration: Durations.long1, curve: Curves.ease); | ||
} else {} | ||
|
||
dragging = false; | ||
return value; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this comment and add
Even without specifying
swipeableArea
whole width should be swipeable