How to make able to scroll ListView inside SingleChildScrollView? - flutter

How to make able to scroll listview inside SingleChildScrollView?
That's right, normally the list view scrolls even if the parent is a SingleChildScrollView.
But my case is special because I used
NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
if (notification is UserScrollNotification) {
if (notification.direction == ScrollDirection.forward) {
//trigger doing some event ...
...changing screen because of this event
So my Listview scrolling is absolutely doing nothing and changing the screen because of the parent event.
child: ListView.builder( ... // when scroll it doesn`t scroll and change screen because of parent
Can anyone solve this?

I think you have not provided the physics property for the list view:
Use -> physics:AlwaysScrollableScrollPhysics() for the listview to make it scrollable.

Related

Swipe back in flutter. without any button

How Can I swipe for back in Flutter? not tap back button.
Without any button......
Just swipe from left to right. And go to the back page
you can wrap your screen widget with gesture detector and on detecting swipe left you can pop the screen like this
GestureDetector(
onPanUpdate: (details) {
if (details.delta.dx > 0) {
Navigator.of(context).pop();
}
},
child: Scaffold(
body: ///your body
),
);
Edit:
The above solution was the one that came to my mind and i got another answer on stack-overflow maybe that one is the correct way to do it here is the answer
https://stackoverflow.com/a/55577584/14466860

How to make mouse scroll to scroll horizontally in swiftui?

I have a View built using SwiftUI that uses Scroll view horizontally.
var body: some View {
VStack {
// this should scroll horizontally when user uses the mouse scroller
HStack {
}
}
}
Currently user can do that by pressing shift button and scroll but I want to get rid of shift button
Goal: Mouse scroller should scroll horizontally without using shift button
You can see a tutorial here:
https://www.youtube.com/watch?v=-JjN2sRQXLc&t=1176s
With the final code here:
https://github.com/gahntpo/ItunesSearchApp/blob/main/ItunesSearchApp/view/album/SongsForAlbumListView.swift
In sum you will need a ScrollViewReader that gives you a closure with a proxy that could be called to scrollTo method.
You'll need to mart the destinations to scrollTo with an identifiable object or using .id modifier.
All explained in the video.

ScrollView nested in TabView reset position to start while changing tab

I'm experimenting with SwiftUI and I found a weird behaviour of ScrollView when nested in a TabView. If I swipe the ScrollView down and then do a slow gesture to swipe to the next tab, it sometimes reset the scrollView Position to the start. I'm not sure how to explain it well so here is a gif:
I'm trying to understand what cause this and if there is a way to avoid it.
Here is the code I use:
var body: some View {
TabView(selection: $selectedTab,
content: {
ForEach(coreData.pageList, id: \.self) { page in
if(coreData.pageList[page] == nil) {
ProgressView().onAppear(perform: {
loadPage(page: page)
})
} else {
ScrollView {
Text("START OF PAGE ------- -----Page Number: \(page)! This is a very short text made big to simulate scrolling. This is a very short text made big to simulate scrolling. ").font(.system(size: 90))
}
}
}
})
}
Am I missing something simple, is it a bug (with workaround), am I using these views wrong?
Ultimately I just want to have something that look like a page that you swipe right left or right but having the scroll position reset so fast would not be great user experience and doesn't look great
Edit: I haven't found any solution yet, but an interesting point: if I put the ProgressView inside the scrollView the scrollview does NOT reset position but the next tab will already be scrolled with the same amount as the previews page. This is equally "bad" if not worse :-(.
Try to put it into separated view (passing all needed parameters), so SwiftUI will see that view not changed and so not rerender it, ie. it should look like
if(coreData.pageList[page] == nil) {
ProgressView().onAppear(perform: {
loadPage(page: page)
})
} else {
DetailsView(page: page) // << ScrollView inside
}

How to react to scroll down and up?

I want to know the direction of scrolling at the moment of scrolling up or down in the list view. but I couldn't find a solution.
I tried to use ScrollController to listen to the direction of scrolling for dealing with different work as up and down scrolls. But there is no way to listen to that with ScrollController.
Is there anyone to have dealt with this problem?
You can make use of NotificationListener<ScrollNotification>. You will need to wrap your scroll view in that widget and then listen to UserScrollNotifications:
NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
if (notification is UserScrollNotification) {
if (notification.direction == ScrollDirection.forward) {
// Handle scroll down.
} else if (notification.direction == ScrollDirection.reverse) {
// Handle scroll up.
}
}
// Returning null (or false) to
// "allow the notification to continue to be dispatched to further ancestors".
return null;
},
child: ListView(..), // Or whatever scroll view you use.
)

Dismiss keyboard on swipe back gesture in Flutter app

I am trying to dismiss the keyboard when the user swipes from the edge to pop route.
Currently the keyboard doesn't dismiss until the route is completely gone popped, messing up some of the other pages layout until it dismisses
I did try to use a WillPopScope to determine when the user was going to pop the route, but unfortunately this disables the swipe to pop functionality from iOS or the CupertinoPageRoute.
I just want to find out if there's anyway I can determine when the user swipes from the edge to pop or taps the back button on the appBar and dismiss the keyboard as they do so.
If possible, I am trying to dismiss keyboard as soon as they start swiping to pop, as it happens in many apps.
I am attaching attaching a gif showing the effect I'm trying to achieve.
As suggested by Ovidiu
class DismissKeyboardNavigationObserver extends NavigatorObserver {
#override
void didStartUserGesture(Route route, Route previousRoute) {
SystemChannels.textInput.invokeMethod('TextInput.hide');
super.didStartUserGesture(route, previousRoute);
}
}
and in your Material App
MaterialApp(
navigatorObservers: [DismissKeyboardNavigationObserver()],
)
You need to create a custom class extending NavigatorObserver, and pass an instance of it to the navigatorObservers property of your MaterialApp or CupertinoApp.
Within that custom class, you can override didStartUserGesture and didStopUserGesture, which will be called when the swipe gesture starts/ends. This should allow you to achieve the behavior you are looking for. Note that didStartUserGesture indicates the current route as well as the previous route, based on which you could add logic to determine whether the keyboard should be dismissed or not.
This should come naturally and you shouldn't be directly concerned with that because actually, when you pop a route with the keyboard on, it should dismiss properly.
However, if you want to detect when the user starts swiping and dismiss the keyboard along with it and then pop the current route, you can easily achieve it by wrapping your screen widget with a GestureDetector like so:
Widget build(BuildContext context) {
double dragStart = 0.0;
return GestureDetector(
onHorizontalDragStart: (details) => dragStart = details.globalPosition.dx,
onHorizontalDragUpdate: (details) {
final double screenWidth = MediaQuery.of(context).size.width;
// Here I considered a back swipe only when the user swipes until half of the screen width, but you can tweak it to your needs.
if (dragStart <= screenWidth * 0.05 && details.globalPosition.dx >= screenWidth) {
FocusScope.of(context).unfocus();
}
child: // Your other widgets...
},
this is something i wrote to handle this issue. doesnt use any external packages, you would just wrap your content in the main function at the top.
Widget swipeOffKeyboard(BuildContext context, {Widget? child}) {
return Listener(
onPointerMove: (PointerMoveEvent pointer) {
disKeyboard(pointer, context);
},
child: child, // your content should go here
);
}
void disKeyboard(PointerMoveEvent pointer, BuildContext context) {
double insets = MediaQuery.of(context).viewInsets.bottom;
double screenHeight = MediaQuery.of(context).size.height;
double position = pointer.position.dy;
double keyboardHeight = screenHeight - insets;
if (position > keyboardHeight && insets > 0) FocusManager.instance.primaryFocus?.unfocus();
}