dart 如何检测Scaffold ScrollBar主体中的所有点击

ua4mk5z4  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(76)

我想检测所有的点击,滚动等,将有一个计数器。我怎么做?
我的代码:

return Scaffold(
          body: Scrollbar(
            child: Column(
              children: [
                Expanded(
                  child: CustomScrollView(
....

字符串
这个答案没有解决我的问题,因为它使用了不同的主体:https://stackoverflow.com/a/60163647/7880054

body: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () => print('Tapped'),
          child: QQBody(),
        ),


如何检测所有的点击和滚动?

fwzugrvs

fwzugrvs1#

您可以使用GestureDetectorNotificationListener<ScrollNotification> Package 您的小部件,以检测滚动更改,如下所示:

import 'package:flutter/material.dart';

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) => const MaterialApp(home: HomePage());
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int tapCount = 0;
  int scrollCount = 0;

  @override
  Widget build(BuildContext context) => GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          setState(() => tapCount++);
          print('Tapped');
        },
        child: Scaffold(
          appBar: AppBar(title: const Text('Click and Scroll Counter')),
          body: NotificationListener<ScrollNotification>(
            onNotification: (scrollNotification) {
              if (scrollNotification is ScrollStartNotification ||
                  scrollNotification is ScrollUpdateNotification) {
                setState(() => scrollCount++);

                print('Scrolled');
              }

              return false;
            },
            child: Scrollbar(
              child: ListView.builder(
                itemCount: 100,
                itemBuilder: (context, index) =>
                    ListTile(title: Text('Item $index')),
              ),
            ),
          ),
          bottomNavigationBar: BottomAppBar(
            child: Padding(
              padding: const EdgeInsets.all(16),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Text('Taps: $tapCount'),
                  Text('Scrolls: $scrollCount'),
                ],
              ),
            ),
          ),
        ),
      );
}

字符串

相关问题