Skip to content

<ScrollController attached to multiple scroll views> while navigating to back to a page with scrollController #74372

@HaijunWei

Description

@HaijunWei

I have a need, please watch the video.

1611197989633759.mp4

When the list is sliding, the top Widget(blue container) transparency will change according to the offset.
Tabar and TabarView(red container) will be nested in the list, and need tabController. So TickerProviderStateMixin is mixed in. When a new page is pushed, the program is abnormal.

controller.offset triggered assert(_positions.length == 1,'ScrollController attached to multiple scroll views.'); in scroll_controller.dart line 113

Code

code sample
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MainPage(),
    );
  }
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> with TickerProviderStateMixin {
  ScrollController _scrollController;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('MainPage: ${_scrollController.positions}');
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Text('Demo'),
        actions: [
          CupertinoButton(
            child: Text('Page 1'),
            onPressed: () {
              Navigator.of(context).push(CupertinoPageRoute(builder: (context) {
                return Page1();
              }));
            },
          )
        ],
      ),
      body: NestedScrollView(
        controller: _scrollController,
        headerSliverBuilder: (context, _) {
          return [
            OpacityView(
              controller: _scrollController,
            )
          ];
        },
        body: Container(
          height: 1000,
          color: Colors.red,
        ),
      ),
    );
  }
}

class OpacityView extends AnimatedWidget {
  OpacityView({Key key, this.controller})
      : super(key: key, listenable: controller);

  final ScrollController controller;

  @override
  Widget build(BuildContext context) {
    double opacity = 1;
    print('OpacityView: ${controller.positions}');
    opacity = 1 - (controller.offset / 200).clamp(0, 1);

    return SliverToBoxAdapter(
      child: Opacity(
        opacity: opacity,
        child: Container(
          height: 200,
          color: Colors.blue,
        ),
      ),
    );
  }
}

class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page 1'),
        backgroundColor: Colors.black,
      ),
    );
  }
}

Output

  • first run
    flutter: MainPage: []
    flutter: OpacityView: [_NestedScrollPosition#f0583(outer, offset: 0.0, range: null..null, viewport: null)]

  • pushed page1
    flutter: MainPage: [_NestedScrollPosition#f0583(outer, offset: 0.0, range: 0.0..200.0, viewport: 712.0)]
    flutter: OpacityView: [_NestedScrollPosition#f0583(outer, offset: 0.0, range: 0.0..200.0, viewport: 712.0), _NestedScrollPosition#152a0(outer, offset: 0.0, range: 0.0..200.0, viewport: 712.0)]

Exception

The following assertion was thrown building OpacityView(animation: ScrollController#35d66(2 clients), dirty, state: _AnimatedState#364a5):
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 113 pos 12: '_positions.length == 1'

Flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.5, on macOS 11.0.1 20B29 darwin-x64, locale
    zh-Hans-CN)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 12.2)
[!] Android Studio (version 4.1)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.52.1)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

Labels

c: crashStack traces logged to the consolef: scrollingViewports, list views, slivers, etc.found in release: 1.22Found to occur in 1.22frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions