Flutter Web:无法滚动鼠标向下(拖动)(Flutter 2.5+)

brqmpdu1  于 2023-08-07  发布在  Flutter
关注(0)|答案(5)|浏览(202)

[更新]

我可以确认该问题发生在2.5以上的Flutter中。使用2.2.3就可以了。问题是为什么在2.5中删除了这个功能?如何在Flutter 2.5中启用它?

[来源问题]

我使用SingleChildScrollView扑网页与桌面浏览器。滚动只能在鼠标滚轮上工作,但在鼠标点击(拖动)时不工作。我怎样才能将鼠标点击Map到触摸和滚动像移动的一样?

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SingleChildScrollView(
        child: Column(
          children: List<Widget>.generate(50, (i) => Text(i.toString())).toList(),
        ),
      ),
    );
  }
}

个字符

vdgimpew

vdgimpew1#

我的最终解决方案是这些建议的组合,希望在任何地方拖动滚动,所以使用了这个:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      scrollBehavior: MaterialScrollBehavior().copyWith(
        dragDevices: {PointerDeviceKind.mouse, PointerDeviceKind.touch, PointerDeviceKind.stylus, PointerDeviceKind.unknown},
      ),
      ...

字符串
很简单

vsaztqbk

vsaztqbk2#

在2.5之后改变鼠标滚动行为。详情请参见this

class MyCustomScrollBehavior extends MaterialScrollBehavior {
  // Override behavior methods and getters like dragDevices
  @override
  Set<PointerDeviceKind> get dragDevices => { 
    PointerDeviceKind.touch,
    PointerDeviceKind.mouse,
    // etc.
  };
}

// ScrollBehavior can be set for a specific widget.
final ScrollController controller = ScrollController();
ScrollConfiguration(
  behavior: MyCustomScrollBehavior(),
  child: ListView.builder(
    controller: controller,
    itemBuilder: (BuildContext context, int index) {
     return Text('Item $index');
    }
  ),
);

字符串

umuewwlo

umuewwlo3#

Flutter Web列表视图不检测鼠标滚动或拖动事件。您应该添加scrollConfiguration,而只有鼠标滚动和触摸事件才能起作用。首先使用ScrollConfiguration Package listview并添加行为。您可以看到live example here

ScrollConfiguration(
    behavior: ScrollConfiguration.of(context).copyWith(dragDevices: {
      PointerDeviceKind.touch,
      PointerDeviceKind.mouse,
    },),
    child: ListView(
      controller: _controller,
      physics: const AlwaysScrollableScrollPhysics(),
      scrollDirection: Axis.horizontal,
      children: <Widget>[
      
          for (int index = 0; index < showThumbnailList.length; index++) _thumbnail(showThumbnailList[index], index)
       
        // showThumbnailList.map((x) => _thumbnail(x) ).toList()),
      ],
    ),
  ),

字符串

mjqavswn

mjqavswn4#

对于Web,代码是不同的。您需要将ROW绑定到一个LISTVIEW,然后再绑定到m SCROLLCONFIGURATION

Container(
          width: 500,
          height: 200,
          child: ScrollConfiguration(
            behavior: ScrollConfiguration.of(context).copyWith(
              dragDevices: {
                PointerDeviceKind.touch,
                PointerDeviceKind.mouse,
              },
            ),
            child: ListView(
              controller: _controller,
              physics: const AlwaysScrollableScrollPhysics(),
              scrollDirection: Axis.horizontal,
              children: <Widget>[
                                          Container(width: 100, height: 10,color: Colors.red,),
                                          Container(width: 100, height: 10,color: Colors.blue,),
                                          Container(width: 100, height: 10,color: Colors.green,),
                                          Container(width: 100, height: 10,color: Colors.yellow,),
                                          Container(width: 100, height: 10,color: Colors.orange,),
              ],
            ),
          ),
        ),

字符串

thigvfpy

thigvfpy5#

试试这个,我做到了

class scrollHorizontal extends StatefulWidget{
  State<scrollHorizontal> createState() => _windowsScroll();
}

class _windowsScroll extends State<scrollHorizontal>{
  final ScrollController scrollController = ScrollController();

  @override
  Widget build(BuildContext Context){
    return GestureDetector(
        onHorizontalDragUpdate: (details) {
          // Scroll horizontally when the user drags horizontally (mouse clicks)
          final newScrollOffset = scrollController.offset - (details.delta.dx * 2);
          scrollController.jumpTo(
            newScrollOffset
          );
        },
      child: SingleChildScrollView(
        controller: scrollController,
        scrollDirection: Axis.horizontal,
        child: Row(
          children: [
            // Content Here
          ],
        ),
      ),
    );
  }
}

字符串

相关问题