-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
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.