flutter 奇怪的行为时,把脚手架内NestedScrollview?自动滚动到顶部时,剪辑下SliverAppbar

7uhlpewt  于 2023-11-21  发布在  Flutter
关注(0)|答案(2)|浏览(149)

我有一个奇怪的行为,自动滚动到顶部时,单击listview. builder的第一个索引。我想实现一个SliverAppbar底部是一个TabBar和一个身体与TabView。每个TabView有一个脚手架与FloatingButton。
下面是我的代码来重现这个问题。只要点击索引0接近应用程序栏和scrollview自动滚动到顶部。

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图像https://drive.google.com/file/d/1P7XyFMwpkOfdgW2UFXHmPNrDT4_rYufH/view?usp=sharing

9bfwbjaz

9bfwbjaz1#

问题解决了!
只需将这一行添加到NestedScrollView

physics: const NeverScrollableScrollPhysics(),

字符串
Click Here to Check Code

ryevplcw

ryevplcw2#

Scaffold是你的“Material - ism”页面的 backbone ,它有解剖结构。像appBarbodybottomNavigationBar等。问题是NestedScrollView是一个小部件,你不要用Widget Package Scaffold,因为Scaffold是小部件的 backbone ,试着放在body里面。

/// 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();
              },
          );
        }),
      ),
    );
  }
}

字符串

相关问题