Flutter 当检测到鼠标时,如何使页面视图允许滚动
我有一个web应用程序,它使用PageView允许浏览页面。我想以某种方式添加在检测到鼠标时滚动的功能,以使其更直观,并且不会混淆用户。我一直在尝试使用一个倾听者,但似乎无法理解。有人能帮我弄明白怎么做吗 这是密码Flutter 当检测到鼠标时,如何使页面视图允许滚动,flutter,debugging,Flutter,Debugging,我有一个web应用程序,它使用PageView允许浏览页面。我想以某种方式添加在检测到鼠标时滚动的功能,以使其更直观,并且不会混淆用户。我一直在尝试使用一个倾听者,但似乎无法理解。有人能帮我弄明白怎么做吗 这是密码 import 'package:flutter/material.dart'; void main() => runApp(MyApp()); int currentPage = 0; class MyApp extends StatelessWidget { fi
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
int currentPage = 0;
class MyApp extends StatelessWidget {
final PageController ctrl = PageController(viewportFraction: 0.8);
@override
Widget build(
BuildContext context,
) {
return MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => Container(
alignment: Alignment.center,
child: Container(
constraints: BoxConstraints(
maxWidth: 1000,
maxHeight: 2000,
),
child: PageView(
scrollDirection: Axis.vertical,
controller: ctrl,
children: [
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.green,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 20,
offset: Offset(1, 1))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.blue,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 20,
offset: Offset(1, 1))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.orange,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 20,
offset: Offset(1, 1))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.red,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 20,
offset: Offset(1, 1))
]),
),
],
),
),
),
)));
}
}
谢谢大家!
更新:这是我到目前为止的资料。我以为这可能行得通,但没有
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
int currentPage = 0;
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final PageController pageController = PageController(viewportFraction: 0.8);
bool pageIsScrolling = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => Container(
alignment: Alignment.center,
child: GestureDetector(
// to detect swipe
onPanUpdate: (details) {
_onScroll(details.delta.dy * -1);
},
child: Listener(
// to detect scroll
onPointerSignal: (pointerSignal) {
if (pointerSignal is PointerScrollEvent) {
_onScroll(pointerSignal.scrollDelta.dy);
}
},
child: Container(
constraints: BoxConstraints(
maxWidth: 1000,
maxHeight: 2000,
),
child: PageView(
scrollDirection: Axis.vertical,
controller: pageController,
children: [
Container(
margin: EdgeInsets.only(
bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(20)),
color: Colors.green,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 20,
offset: Offset(1, 1))
]),
),
Container(
margin: EdgeInsets.only(
bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(20)),
color: Colors.blue,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 20,
offset: Offset(1, 1))
]),
),
Container(
margin: EdgeInsets.only(
bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(20)),
color: Colors.orange,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 20,
offset: Offset(1, 1))
]),
),
Container(
margin: EdgeInsets.only(
bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(20)),
color: Colors.red,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 20,
offset: Offset(1, 1))
]),
),
],
),
),
),
)))));
}
void _onScroll(double offset) {
if (pageIsScrolling == false) {
pageIsScrolling = true;
if (offset > 0) {
pageController
.nextPage(
duration: Duration(milliseconds: 300), curve: Curves.easeInOut)
.then((value) => pageIsScrolling = false);
print('scroll down');
} else {
pageController
.previousPage(
duration: Duration(milliseconds: 300), curve: Curves.easeInOut)
.then((value) => pageIsScrolling = false);
print('scroll up');
}
}
}
}
import'package:flatter/signatures.dart';
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
int currentPage=0;
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
final PageController PageController=PageController(viewportFraction:0.8);
bool-pagescrolling=false;
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
车身:建造商(
生成器:(上下文)=>容器(
对齐:对齐.center,
儿童:手势检测器(
//检测刷卡
onPanUpdate:(详细信息){
_onScroll(details.delta.dy*-1);
},
孩子:听众(
//检测滚动
onPointerSignal:(pointerSignal){
if(pointerSignal是PointerScrollEvent){
_onScroll(pointerSignal.scrolldta.dy);
}
},
子:容器(
约束:BoxConstraints(
最大宽度:1000,
最高身高:2000,
),
子:页面视图(
滚动方向:轴垂直,
控制器:页面控制器,
儿童:[
容器(
页边距:仅限边距(
底部:50,右侧:30,左侧:30),
装饰:盒子装饰(
边界半径:
边界半径所有(半径圆形(20)),
颜色:颜色。绿色,
boxShadow:[
箱形阴影(
颜色:颜色。灰色[500],
半径:20,
偏置:偏置(1,1))
]),
),
容器(
页边距:仅限边距(
底部:50,右侧:30,左侧:30),
装饰:盒子装饰(
边界半径:
边界半径所有(半径圆形(20)),
颜色:颜色,蓝色,
boxShadow:[
箱形阴影(
颜色:颜色。灰色[500],
半径:20,
偏置:偏置(1,1))
]),
),
容器(
页边距:仅限边距(
底部:50,右侧:30,左侧:30),
装饰:盒子装饰(
边界半径:
边界半径所有(半径圆形(20)),
颜色:颜色。橙色,
boxShadow:[
箱形阴影(
颜色:颜色。灰色[500],
半径:20,
偏置:偏置(1,1))
]),
),
容器(
页边距:仅限边距(
底部:50,右侧:30,左侧:30),
装饰:盒子装饰(
边界半径:
边界半径所有(半径圆形(20)),
颜色:颜色,红色,
boxShadow:[
箱形阴影(
颜色:颜色。灰色[500],
半径:20,
偏置:偏置(1,1))
]),
),
],
),
),
),
)))));
}
void _onScroll(双偏移){
如果(PageIsCrolling==false){
PageIsCrolling=true;
如果(偏移量>0){
页面控制器
.下一页(
持续时间:持续时间(毫秒:300),曲线:Curves.easeInOut)
。然后((值)=>PageIsCrolling=false);
打印(“向下滚动”);
}否则{
页面控制器
.上一页(
持续时间:持续时间(毫秒:300),曲线:Curves.easeInOut)
。然后((值)=>PageIsCrolling=false);
打印(“向上滚动”);
}
}
}
}
添加鼠标区域作为页面视图的父级,并找到鼠标中心
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _enterCounter = 0;
int _exitCounter = 0;
double x = 0.0;
double y = 0.0;
void _incrementEnter(PointerEvent details) {
setState(() {
_enterCounter++;
});
}
void _incrementExit(PointerEvent details) {
setState(() {
_exitCounter++;
});
}
void _updateLocation(PointerEvent details) {
setState(() {
x = details.position.dx;
y = details.position.dy;
});
}
@override
Widget build(BuildContext context) {
return ConstrainedBox(
constraints: BoxConstraints.tight(Size(300.0, 200.0)),
child: MouseRegion(
onEnter: _incrementEnter,
onHover: _updateLocation,
onExit: _incrementExit,
child: Container(
color: Colors.lightBlueAccent,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('You have entered or exited this box this many times:'),
Text(
'$_enterCounter Entries\n$_exitCounter Exits',
style: Theme.of(context).textTheme.headline4,
),
Text(
'The cursor is here: (${x.toStringAsFixed(2)}, ${y.toStringAsFixed(2)})',
),
],
),
),
),
);
}
}