1+ import 'package:flutter/gestures.dart' ;
12import 'package:flutter/material.dart' ;
23
3- class MaxWidthBox extends StatelessWidget {
4+ class MaxWidthBox extends StatefulWidget {
45 final double ? maxWidth;
56
67 /// Control the internal Stack alignment. This widget
@@ -19,24 +20,91 @@ class MaxWidthBox extends StatelessWidget {
1920 this .background,
2021 this .alignment = Alignment .topCenter});
2122
23+ @override
24+ State <MaxWidthBox > createState () => _MaxWidthBoxState ();
25+ }
26+
27+ class _MaxWidthBoxState extends State <MaxWidthBox > {
28+ final _scrollController = ScrollController ();
29+
2230 @override
2331 Widget build (BuildContext context) {
2432 MediaQueryData mediaQuery = MediaQuery .of (context);
2533
26- if (maxWidth != null ) {
27- if (mediaQuery.size.width > maxWidth! ) {
28- mediaQuery =
29- mediaQuery. copyWith ( size: Size (maxWidth! , mediaQuery.size.height));
34+ if (widget. maxWidth != null ) {
35+ if (mediaQuery.size.width > widget. maxWidth! ) {
36+ mediaQuery = mediaQuery. copyWith (
37+ size: Size (widget. maxWidth! , mediaQuery.size.height));
3038 }
3139 }
3240
3341 return Stack (
34- alignment: alignment,
42+ alignment: widget. alignment,
3543 children: [
36- background ?? const SizedBox .shrink (),
44+ Scrollbar (
45+ controller: _scrollController,
46+ trackVisibility: true ,
47+ thumbVisibility: true ,
48+ child: Listener (
49+ onPointerSignal: (pointerSignal) {
50+ if (pointerSignal is PointerScrollEvent ) {
51+ final newOffset =
52+ _scrollController.offset + pointerSignal.scrollDelta.dy;
53+
54+ if (newOffset < _scrollController.position.minScrollExtent) {
55+ _scrollController
56+ .jumpTo (_scrollController.position.minScrollExtent);
57+ } else if (newOffset >=
58+ _scrollController.position.minScrollExtent &&
59+ newOffset <= _scrollController.position.maxScrollExtent) {
60+ _scrollController.jumpTo (newOffset);
61+ } else if (newOffset >
62+ _scrollController.position.maxScrollExtent) {
63+ _scrollController
64+ .jumpTo (_scrollController.position.maxScrollExtent);
65+ }
66+ }
67+ },
68+ child: widget.background ??
69+ Container (
70+ width: double .infinity,
71+ height: double .infinity,
72+ color: Colors .transparent),
73+ ),
74+ ),
3775 MediaQuery (
38- data: mediaQuery, child: SizedBox (width: maxWidth, child: child)),
76+ data: mediaQuery,
77+ child: SizedBox (
78+ width: widget.maxWidth,
79+ child: PrimaryScrollController (
80+ controller: _scrollController,
81+ child: widget.child,
82+ ),
83+ ),
84+ ),
3985 ],
4086 );
4187 }
88+
89+ @override
90+ void dispose () {
91+ _scrollController.dispose ();
92+ super .dispose ();
93+ }
4294}
95+
96+ // NotificationListener<ScrollNotification>(
97+ // onNotification: (scrollNotification) {
98+ // // Forward the scroll notification to the child
99+ // if (scrollNotification is ScrollUpdateNotification) {
100+ // _scrollController.jumpTo(_scrollController.offset +
101+ // scrollNotification.scrollDelta!.toDouble());
102+ // }
103+ // return false; // Don't consume the notification
104+ // },
105+ // child: widget.background ??
106+ // Container(
107+ // width: double.infinity,
108+ // height: double.infinity,
109+ // color: Colors.transparent),
110+ // ),
0 commit comments