Flutter 如何根据不同的页面内容大小定制滚动条
我希望能够从这段代码中创建一个构造器,以便能够在应用程序的不同页面上使用,以便滚动条根据页面内容大小而不是下面代码中的默认大小工作…(我甚至不知道我在说什么…) 请看下面的评论,以便更好地理解我的意思 这是自定义滚动条的代码Flutter 如何根据不同的页面内容大小定制滚动条,flutter,dart,flutter-web,flutterwebviewplugin,Flutter,Dart,Flutter Web,Flutterwebviewplugin,我希望能够从这段代码中创建一个构造器,以便能够在应用程序的不同页面上使用,以便滚动条根据页面内容大小而不是下面代码中的默认大小工作…(我甚至不知道我在说什么…) 请看下面的评论,以便更好地理解我的意思 这是自定义滚动条的代码 import 'dart:async'; import 'package:flutter/material.dart'; class WebScrollbar extends StatefulWidget { final Widget child; final
import 'dart:async';
import 'package:flutter/material.dart';
class WebScrollbar extends StatefulWidget {
final Widget child;
final ScrollController controller;
final double heightFraction;
final double width;
final Color color;
final Color backgroundColor;
final bool isAlwaysShown;
final Function scrollCallBack;
final double value;
WebScrollbar({
this.value,
this.scrollCallBack,
this.child,
this.controller,
this.heightFraction = 0.20,
this.width = 8,
this.color = Colors.black45,
this.backgroundColor = Colors.black12,
this.isAlwaysShown = false,
}) : assert(child != null),
assert(controller != null),
assert(heightFraction != null &&
heightFraction < 1.0 &&
heightFraction > 0.0),
assert(width != null),
assert(color != null),
assert(backgroundColor != null),
assert(isAlwaysShown != null);
@override
_WebScrollbarState createState() => _WebScrollbarState();
}
class _WebScrollbarState extends State<WebScrollbar> {
double _scrollPosition = 0;
bool _isUpdating;
Timer timer;
_scrollListener() {
setState(() {
_scrollPosition = widget.controller.position.pixels;
});
}
@override
void initState() {
widget.controller.addListener(_scrollListener);
_isUpdating = false;
super.initState();
}
@override
Widget build(BuildContext context) {
var screenSize = MediaQuery.of(context).size;
double _scrollerHeight = screenSize.height * widget.heightFraction;
double _topMargin = widget.controller.hasClients
? ((screenSize.height *
_scrollPosition /
widget.controller.position.maxScrollExtent) -
(_scrollerHeight *
_scrollPosition /
widget.controller.position.maxScrollExtent))
: 0;
return NotificationListener<ScrollNotification>(
onNotification: (notification) {
if (notification.depth == 0) {
if (notification is ScrollUpdateNotification) {
timer.cancel();
setState(() {
_isUpdating = true;
});
} else {
timer = Timer(Duration(seconds: 5), () {
setState(() {
_isUpdating = false;
});
});
}
// print(_isUpdating);
}
return true;
},
child: Stack(
children: [
widget.child,
AnimatedOpacity(
opacity: widget.isAlwaysShown
? 1
: widget.controller.hasClients
? _isUpdating
? 1
: 0
: 0,
duration: Duration(milliseconds: 900),
child: Container(
alignment: Alignment.centerRight,
height: MediaQuery.of(context).size.height,
width: 10,
margin: EdgeInsets.only(
left: MediaQuery.of(context).size.width - 10,
),
color: widget.backgroundColor,
child: Align(
alignment: Alignment.topCenter,
child: GestureDetector(
child: Container(
height: _scrollerHeight,
width: widget.width,
margin: EdgeInsets.only(
left: 1.0,
right: 1.0,
top: _topMargin,
),
decoration: BoxDecoration(
color: widget.color,
borderRadius: BorderRadius.all(
Radius.circular(3.0),
),
),
),
onTapCancel: () {
timer = Timer(Duration(seconds: 10), () {
setState(() {
_isUpdating = false;
});
});
},
onTapDown: (details) {
timer.cancel();
setState(() {
_isUpdating = true;
});
},
onVerticalDragUpdate: (dragUpdate) {
widget.controller.position
//Say in the line of code below. **The value (3.5)** should be manually set on each page of the app.//
.moveTo(dragUpdate.globalPosition.dy * 3.5);
setState(() {
if (dragUpdate.globalPosition.dy >= 0 &&
_scrollPosition <=
widget.controller.position.maxScrollExtent) {
_scrollPosition = dragUpdate.globalPosition.dy +
dragUpdate.globalPosition.dy *
(_scrollPosition /
widget
.controller.position.maxScrollExtent) -
(_scrollerHeight *
_scrollPosition /
widget.controller.position.maxScrollExtent);
}
});
},
),
),
),
),
],
),
);
}
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
类WebScrolBar扩展StatefulWidget{
最后一个孩子;
最终滚动控制器;
最终双高度分数;
最终双倍宽度;
最终颜色;
最终颜色背景色;
最后的布尔伊萨维斯霍恩;
最终函数调用;
最终双值;
网吧({
这个,这个价值,,
这是一个回调,
这个孩子,
这是一个控制器,
此点高度分数=0.20,
这个宽度=8,
this.color=Colors.black45,
this.backgroundColor=Colors.black12,
this.isAlwaysShown=false,
}):assert(child!=null),
断言(控制器!=null),
assert(heightFraction!=null&&
高度分数<1.0&&
高度分数>0.0),
断言(宽度!=null),
断言(颜色!=null),
断言(backgroundColor!=null),
断言(isAlwaysShown!=null);
@凌驾
_WebScrollbarState createState()=>\u WebScrollbarState();
}
类_WebScrollbarState扩展状态{
双滚动位置=0;
bool_正在更新;
定时器;
_scrollListener(){
设置状态(){
_scrollPosition=widget.controller.position.pixels;
});
}
@凌驾
void initState(){
widget.controller.addListener(_scrollListener);
_isUpdating=false;
super.initState();
}
@凌驾
小部件构建(构建上下文){
var screenSize=MediaQuery.of(context).size;
double _scrollerHeight=screenSize.height*widget.heightFraction;
double\u topMargin=widget.controller.hasClient
((屏幕尺寸、高度)*
_滚动位置/
widget.controller.position.maxScrollExtent)-
()*
_滚动位置/
widget.controller.position.maxScrollExtent)
: 0;
返回通知侦听器(
通知:(通知){
如果(notification.depth==0){
如果(通知为ScrollUpdateNotification){
timer.cancel();
设置状态(){
_isUpdating=true;
});
}否则{
计时器=计时器(持续时间(秒数:5),(){
设置状态(){
_isUpdating=false;
});
});
}
//打印(正在更新);
}
返回true;
},
子:堆栈(
儿童:[
小朋友,
动产能力(
不透明度:widget.isAlwaysShown
1.
:widget.controller.hasClients
?\u正在更新
1.
: 0
: 0,
持续时间:持续时间(毫秒:900),
子:容器(
对齐:alignment.centerRight,
高度:MediaQuery.of(context).size.height,
宽度:10,
页边距:仅限边距(
左:MediaQuery.of(context).size.width-10,
),
颜色:widget.backgroundColor,
子对象:对齐(
对齐:alignment.topCenter,
儿童:手势检测器(
子:容器(
高度:_-scrollerHeight,
宽度:widget.width,
页边距:仅限边距(
左:1.0,
右:1.0,
顶部:_topMargin,
),
装饰:盒子装饰(
颜色:widget.color,
borderRadius:borderRadius.all(
圆形半径(3.0),
),
),
),
onTapCancel:(){
计时器=计时器(持续时间(秒:10),(){
设置状态(){
_isUpdating=false;
});
});
},
onTapDown:(详细信息){
timer.cancel();
设置状态(){
_isUpdating=true;
});
},
垂直排水日期:(排水日期){
widget.controller.position
//在下面的代码行中说。**值(3.5)**应在应用程序的每个页面上手动设置//
.moveTo(dragUpdate.globalPosition.dy*3.5);
设置状态(){
如果(dragUpdate.globalPosition.dy>=0&&
_滚动位置