简体   繁体   English

使用 flutter 在 linux 应用程序中滚动列表视图

[英]Scrolling Listview in linux app with flutter

I am developing an app to run on a raspberry pi 4.我正在开发一个在树莓派 4 上运行的应用程序。

I have a simple ListView, I am compiling for linux with ' flutter build linux ' and to be able to scroll on a touch screen I have to move my finger on the scroll bar.我有一个简单的 ListView,我正在为 linux 编译“ flutter build linux ”,为了能够在触摸屏上滚动,我必须在滚动条上移动手指。 If I scroll with the mouse wheel it works fine, but on a touch screen I necessarily have to do it on the scroll bar and not on the elements contained in the ListView.如果我用鼠标滚轮滚动它工作正常,但在触摸屏上我必须在滚动条上而不是在 ListView 中包含的元素上进行滚动。

However, if I build the app for use with flutter-pi, that is, build it as ' flutter build bundle ', move it to the pi and then run it as ' flutter-pi my_app ' then the listview scrolls on a touch screen.但是,如果我构建用于 flutter-pi 的应用程序,即将其构建为“ flutter build bundle ”,将其移动到 pi,然后将其作为“ flutter-pi my_app ”运行,然后列表视图会在触摸屏上滚动. it works by hovering over items in the listview.它通过将鼠标悬停在列表视图中的项目上来工作。 For more details on how to compile for flutter-pi有关如何为flutter-pi编译的更多详细信息

And if I compile for the web, the scroll above the listview elements also works, it doesn't work when I compile for linux desktop app如果我为 web 编译,列表视图元素上方的滚动也有效,当我为 linux 桌面应用程序编译时它不起作用

My question is: Is there a way to scroll the listview by moving your finger over the elements in a native linux application and not through flutter-pi or web?我的问题是:有没有办法通过将手指移到本机 linux 应用程序中的元素上而不是通过 flutter-pi 或 web 来滚动列表视图?

This is my code:这是我的代码:

import 'package:flutter/material.dart';

Future<void> main() async {
   runApp(const MyApp2());
}
class MyApp2 extends StatelessWidget {
   const MyApp2({super.key});
  
  

   @override
   Widget build(BuildContext context) {
     var list2 = <Color>[];
     list2.add(Colors.black);
     list2.add(Colors.amber);
     list2.add(Color.fromARGB(255, 241, 150, 143));
     list2.add(Colors.blue);
     list2.add(Colors.pink);
     list2.add(Colors.blueAccent);
     return MaterialApp(
       title: 'ListView',
       home: Scaffold(
         body: ListView.builder(
               physics: const AlwaysScrollableScrollPhysics(),
               itemCount: list2.length,
               itemBuilder: (context, index) {
                 return Padding(
                   padding: const EdgeInsets.symmetric(vertical: 20),
                   child: Container(
                     color: list2[index],
                     height: 150,
                   ),
                 );
               }),
       ),
     );
   }
}

As @pskink says, you have to use MaterialScrollBehavior , I leave the modified code to serve as an example正如@pskink 所说,您必须使用MaterialScrollBehavior ,我将修改后的代码作为示例

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  runApp(const MyApp2());
}


class MyApp2 extends StatelessWidget {
  const MyApp2({super.key});
  
  @override
  Widget build(BuildContext context) {
    var list2 = <Color>[];
    list2.add(Colors.black);
    list2.add(Colors.amber);
    list2.add(Color.fromARGB(255, 241, 150, 143));
    list2.add(Colors.blue);
    list2.add(Colors.pink);
    list2.add(Colors.blueAccent);
    return MaterialApp(
// THIS IS THE SCROLL BEHAVIOR
      scrollBehavior: const MaterialScrollBehavior().copyWith(dragDevices: {PointerDeviceKind.mouse, PointerDeviceKind.touch}),
      title: 'ListView',
      home: Scaffold(
        body: ListView.builder(
              physics: const AlwaysScrollableScrollPhysics(),
              itemCount: list2.length,
              itemBuilder: (context, index) {
                return Padding(
                  padding: const EdgeInsets.symmetric(vertical: 20),
                  child: Container(
                    color: list2[index],
                    height: 150,
                  ),
                );
              }),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM