Reputation: 66
I have a weird behaviour, auto scroll to top when click the first index of the listview.builder. I want to achive a SliverAppbar with bottom is a TabBar and a body with TabView. Each TabView have a Scaffold with FloatingButton.
Here is my code to reproduce the issue. Just click the index 0 nearly to the app bar and the scrollview auto scroll to top.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: MyStatelessWidget(),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
SliverAppBar(
floating: true,
pinned: true,
title: Text(
'Appbar',
style: TextStyle(color: Colors.black),
),
expandedHeight: 100,
),
];
},
body: Builder(builder: (context) {
final ScrollController scrollController =
PrimaryScrollController.of(context)!;
return Scaffold(
body: ListView.separated(
controller: scrollController,
padding: EdgeInsets.zero,
itemBuilder: (context, index) {
return Container(
height: 100,
width: double.infinity,
child: Text('Index: $index'),
);
},
itemCount: 1000,
separatorBuilder: (BuildContext context, int index) {
return Divider();
},
),
);
}),
),
);
}
}
Flutter doctor -v
[✓] Flutter (Channel stable, 2.5.1, on macOS 11.4 20F71 darwin-arm, locale en)
• Flutter version 2.5.1 at /Users/Dakrain/Tools/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision ffb2ecea52 (10 days ago), 2021-09-17 15:26:33 -0400
• Engine revision b3af521a05
• Dart version 2.14.2
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /Users/Dakrain/Library/Android/sdk
• Platform android-31, build-tools 31.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.5.1, Build version 12E507
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2020.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
[✓] VS Code (version 1.60.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.26.0
[✓] Connected device (3 available)
• iPhone của Khoa (mobile) • cd7cb393a5d14f4110ff72e4e2744e367ce7aeae • ios • iOS 12.5.4 16H50
• iPhone 12 Pro (mobile) • 34C110D0-44D4-444A-902F-64911EE420E1 • ios •
com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
• Chrome (web) • chrome • web-javascript • Google Chrome
92.0.4515.107
• No issues found!
GIF image of the issue https://drive.google.com/file/d/1P7XyFMwpkOfdgW2UFXHmPNrDT4_rYufH/view?usp=sharing
Upvotes: 3
Views: 1165
Reputation: 81
Problem Solved!
Just add this line to your NestedScrollView
physics: const NeverScrollableScrollPhysics(),
Upvotes: 2
Reputation: 450
Ah, that kinda messy anyway. Scaffold
is a skeleton of your "Material - ism" page, and it has anatomy. like appBar
, body
, bottomNavigationBar
etc. The problem is NestedScrollView
is a widget, and you don't wrap Scaffold with a Widget
because Scaffold
is the skeleton for the widgets, try to put inside the body
instead.
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body:DefaultTabController(
length: 3,
child: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
SliverAppBar(
floating: true,
pinned: true,
title: Text(
'Appbar',
style: TextStyle(color: Colors.black),
),
expandedHeight: 100,
),
];
},
body: Builder(builder: (context) {
final ScrollController scrollController =
PrimaryScrollController.of(context)!;
return ListView.separated(
controller: scrollController,
padding: EdgeInsets.zero,
itemBuilder: (context, index) {
return Container(
height: 100,
width: double.infinity,
child: Text('Index: $index'),
);
},
itemCount: 1000,
separatorBuilder: (BuildContext context, int index) {
return Divider();
},
);
}),
),
);
}
}
Upvotes: 0