gpt4 book ai didi

flutter - 检测 Scrollable 小部件是手动滚动还是以编程方式滚动

转载 作者:行者123 更新时间:2023-12-05 06:10:20 30 4
gpt4 key购买 nike

有没有办法检测是否滚动

  1. PageView(PageController)或
  2. 可滚动 小部件(ScrollController)

由用户手动完成,或在 Controller 对象上使用 jumpTo()animateTo() 以编程方式完成。

将可滚动小部件包装在 NotificationListener 中也不会为我们提供任何此类回调或标志。

我找到了一个解决方案,但我觉得它有点老套,想知道是否有更好的解决方案:

我的解决方案:Scrollable 小部件包装在 GestureDetector 小部件中,并使用 onTapUponTapDown 回调函数并设置标志 ( isScrollManual) 检查滚动是手动的还是编程的。

代码如下:

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyPageControllers(),
),
),
);
}
}

class MyPageControllers extends StatefulWidget {
@override
_MyPageControllersState createState() => _MyPageControllersState();
}

class _MyPageControllersState extends State<MyPageControllers> {

PageController _controller1;
bool isScrollManual = false;

@override
void initState() {
super.initState();
_controller1 = PageController();

_controller1.addListener(() {
if(isScrollManual){
/// Manual Scroll
}else{
/// Programmatic scroll
}
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: GestureDetector(
onTapDown: (tapDownDetails){
isScrollManual = true;
setState(() {});
},
onTapUp: (tapUpDetails){
isScrollManual = false;
setState(() {});
},
child: PageView.builder(
controller: _controller1,
itemBuilder: _itemBuilder,
),
),
);
}

Widget _itemBuilder(BuildContext context, int index) =>
Container(
color: Colors.primaries[index % Colors.primaries.length],
child: Center(
child: Text(
index.toString(),
style: TextStyle(color: Colors.white, fontSize: 60),
),
),
);
}

最佳答案

我找到的唯一可行的解​​决方案是将 Scrollable 小部件包装在 GestureDetector 小部件中,并使用 onTapUponTapDown 回调函数并设置一个标志 (isScrollManual) 以检查滚动是手动的还是编程的。

代码如下:

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyPageControllers(),
),
),
);
}
}

class MyPageControllers extends StatefulWidget {
@override
_MyPageControllersState createState() => _MyPageControllersState();
}

class _MyPageControllersState extends State<MyPageControllers> {

PageController _controller1;
bool isScrollManual = false;

@override
void initState() {
super.initState();
_controller1 = PageController();

_controller1.addListener(() {
if(isScrollManual){
/// Manual Scroll
}else{
/// Programmatic scroll
}
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: GestureDetector(
onTapDown: (tapDownDetails){
isScrollManual = true;
setState(() {});
},
onTapUp: (tapUpDetails){
isScrollManual = false;
setState(() {});
},
child: PageView.builder(
controller: _controller1,
itemBuilder: _itemBuilder,
),
),
);
}

Widget _itemBuilder(BuildContext context, int index) =>
Container(
color: Colors.primaries[index % Colors.primaries.length],
child: Center(
child: Text(
index.toString(),
style: TextStyle(color: Colors.white, fontSize: 60),
),
),
);
}

关于flutter - 检测 Scrollable 小部件是手动滚动还是以编程方式滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64457895/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com