|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +/// A [SingleChildScrollView] that always shows a Material [Scrollbar]. |
| 4 | +/// |
| 5 | +/// This differs from the behavior provided by [MaterialScrollBehavior] in that |
| 6 | +/// (a) the scrollbar appears even when [scrollDirection] is [Axis.horizontal], |
| 7 | +/// and (b) the scrollbar appears on all platforms, rather than only on |
| 8 | +/// desktop platforms. |
| 9 | +// TODO(upstream): SingleChildScrollView should have a scrollBehavior field |
| 10 | +// and pass it on to Scrollable, just like ScrollView does; then this would |
| 11 | +// be covered by using that. |
| 12 | +// TODO: Maybe show scrollbar only on mobile platforms, like MaterialScrollBehavior |
| 13 | +// and the base ScrollBehavior do? |
| 14 | +class SingleChildScrollViewWithScrollbar extends StatefulWidget { |
| 15 | + const SingleChildScrollViewWithScrollbar( |
| 16 | + {super.key, required this.scrollDirection, required this.child}); |
| 17 | + |
| 18 | + final Axis scrollDirection; |
| 19 | + final Widget child; |
| 20 | + |
| 21 | + @override |
| 22 | + State<SingleChildScrollViewWithScrollbar> createState() => |
| 23 | + _SingleChildScrollViewWithScrollbarState(); |
| 24 | +} |
| 25 | + |
| 26 | +class _SingleChildScrollViewWithScrollbarState |
| 27 | + extends State<SingleChildScrollViewWithScrollbar> { |
| 28 | + final ScrollController controller = ScrollController(); |
| 29 | + |
| 30 | + @override |
| 31 | + Widget build(BuildContext context) { |
| 32 | + return Scrollbar( |
| 33 | + controller: controller, |
| 34 | + child: SingleChildScrollView( |
| 35 | + controller: controller, |
| 36 | + scrollDirection: widget.scrollDirection, |
| 37 | + child: widget.child)); |
| 38 | + } |
| 39 | +} |
0 commit comments