当前位置:首页 > 科技  > 软件

如何在 Flutter 中实现 2D 可滚动的表格,探索 Flutter 中的二维可滚动项,表格解决方案

来源: 责编: 时间:2024-07-11 17:31:19 802观看
导读背景表格是在应用程序中高效组织数据的有用工具。Flutter 的最新更新引入了 TwoDimensionalScrollView 小部件。TwoDimensionalScrollView 是一个小部件,它结合了 TwoDimensionalScrollable 和 TwoDimensionalViewport

背景

表格是在应用程序中高效组织数据的有用工具。Flutter 的最新更新引入了 TwoDimensionalScrollView 小部件。WZV28资讯网——每日最新资讯28at.com

TwoDimensionalScrollView 是一个小部件,它结合了 TwoDimensionalScrollable 和 TwoDimensionalViewport 在垂直和水平维度上创建内容的交互式滚动窗格,但直接使用它有点具有挑战性。WZV28资讯网——每日最新资讯28at.com

下面是在 Flutter 中实现 2D ScrollView 的演示代码。WZV28资讯网——每日最新资讯28at.com

介绍

Flutter 的预构建 widget 具有出色的性能,仅当子视图位于视图中时才延迟渲染子视图,从而提高了性能,但 Flutter 发布了一个新包 two_dimensional_scrollables 来实现在垂直轴和水平轴上滚动的 TableView。WZV28资讯网——每日最新资讯28at.com

在本教程中,我们将探索这个包来实现一个非常简单的 Tableview,并了解如何自定义它。WZV28资讯网——每日最新资讯28at.com

您可以在这里[1]找到完整的源代码。WZV28资讯网——每日最新资讯28at.com

执行

添加依赖项

要添加依赖项,请首先打开 pubspec.yaml 文件,然后添加依赖项。WZV28资讯网——每日最新资讯28at.com

dependencies:  two_dimensional_scrollables: <latest version>

将 TableView 添加到屏幕

添加 TableView 时,可以使用两个构造函数:WZV28资讯网——每日最新资讯28at.com

  • TableView.list :这个与 Flutter 的 ListView 类似。它一次添加所有单元格,并且适用于较短的列表。
  • Tableview.builder :当视图进入视口时,它会延迟添加子项。非常适合您不想一次加载所有内容的较大列表!

让我们创建一个简单的 Tableview 来显示以下信息。WZV28资讯网——每日最新资讯28at.com

class Employee { final String id; final String name; final String role; final String email;     Employee({         required this.id,         required this.name,         required this.role,         required this.email}); static get getEmployees{    return [      Employee(id: '1', name: 'John Doe', role: 'Manager', email: 'john@example.com'),      Employee(id: '2', name: 'Jane Smith', role: 'Developer', email: 'jane@example.com'),      Employee(id: '3', name: 'Mike Johnson', role: 'Designer', email: 'mike@example.com'),      Employee(id: '4', name: 'Emily Brown', role: 'HR Specialist',email: 'emily@example.com'),      Employee(id: '5', name: 'Alex Lee', role: 'Marketing Analyst', email: 'alex@example.com'),      Employee(id: '6', name: 'John Doe', role: 'Manager', email: 'john@example.com'),      Employee(id: '7', name: 'Jane Smith', role: 'Developer', email: 'jane@example.com'),      Employee(id: '8', name: 'Mike Johnson', role: 'Designer', email: 'mike@example.com'),      Employee(id: '9', name: 'Emily Brown', role: 'HR Specialist',email: 'emily@example.com'),      Employee(id: '10', name: 'Alex Lee', role: 'Marketing Analyst', email: 'alex@example.com'),     ];  }}

这是 TableView.builder 的基本示例代码。WZV28资讯网——每日最新资讯28at.com

class TwoDimensionalScrollableDemo extends StatelessWidget {   TwoDimensionalScrollableDemo({super.key});final  List<Employee> employees = Employee.getEmployees;  @override  Widget build(BuildContext context) {    return Scaffold(      appBar: AppBar(        title: const Text("Table view Demo"),      ),      body: TableView.builder(          columnCount: 2,          rowCount: 11,          columnBuilder: buildTableSpan,          rowBuilder: buildTableSpan,          cellBuilder: (BuildContext context, TableVicinity vicinity) {            return Container(child: Center(child: addText(vicinity)));          }),    );  }  TableSpan buildTableSpan(int index){    return TableSpan(extent: FixedTableSpanExtent(50));  }  Widget addText(TableVicinity vicinity) {    if (vicinity.yIndex == 0 && vicinity.xIndex == 0) {      return const Text("Index");    } else if (vicinity.yIndex == 0 && vicinity.xIndex == 1) {      return const Text("name");    } else if (vicinity.xIndex == 0) {      return Text(employees[vicinity.yIndex-1].id);    } else if (vicinity.xIndex == 1) {      return Text(employees[vicinity.yIndex-1].name);    }    return Text("");  }}

这对于几行代码来说已经相当不错了,让我们分解一下 TableView.builder 所需的参数。WZV28资讯网——每日最新资讯28at.com

  • columnCount :该参数设置表中的列数。
  • rowCount :该参数设置表中的行数。
  • columnBuilder :它是一个帮助定义表中每列的布局和功能的函数。它接受一个整数作为参数,并返回一个 TableSpan ,它构造和配置列。
  • rowbuilder :与 columnBuilder 类似,此函数定义 TableView 中每行的布局和行为。
  • cellBuilder :它处理表中每个单元格的布局。它采用 TableVicinity 参数,其中包含特定单元格的行索引和列索引。这有助于您自定义表格中各个单元格的外观和行为。

运行上面的代码,您将看到以下输出。WZV28资讯网——每日最新资讯28at.com

图片图片WZV28资讯网——每日最新资讯28at.com

现在,让我们研究一下 TableView.builder 提供的功能!那么,让我们看看如何自定义和控制 TableView 并添加更多功能。WZV28资讯网——每日最新资讯28at.com

TableSpan 表跨度

TableSpan 表示 TableView 中的一行或一列。WZV28资讯网——每日最新资讯28at.com

添加边框

TableSpan buildTableSpan(int index) {  TableSpanDecoration decoration = const TableSpanDecoration(      border: TableSpanBorder(          trailing: BorderSide(color: Colors.black),          leading: BorderSide(color: Colors.black)));  return  TableSpan(      extent: const FixedTableSpanExtent(100),      foregroundDecoration: decoration);}

TableSpanDecoration 类用于指定 TableSpan 的装饰。我们可以通过 TableSpanBorder 类中的 border 参数为 TableSpan 添加边框,该类具有代表行左右边框和列上下边框的拖尾和前导属性。WZV28资讯网——每日最新资讯28at.com

添加颜色

TableSpan buildTableSpan(int index) {  TableSpanDecoration decoration =  TableSpanDecoration(      color: index == 0 ? Colors.grey[300] : null,      border: const TableSpanBorder(          trailing: BorderSide(color: Colors.black),          leading: BorderSide(color: Colors.black)));  return  TableSpan(      extent: const FixedTableSpanExtent(100),      backgroundDecoration: decoration);}

TableSpanDecoration 中的 color 属性用于设置 TableSpan 的颜色。WZV28资讯网——每日最新资讯28at.com

运行代码,输出将如下所示WZV28资讯网——每日最新资讯28at.com

图片图片WZV28资讯网——每日最新资讯28at.com

因为WZV28资讯网——每日最新资讯28at.com

  • 在 TableView 中,装饰按特定顺序绘制。
  • 首先绘制 mainAxis 的 backgroundDecoration (作为主轴的行或列)。
  • 然后,绘制其他轴上的装饰。
  • 接下来,绘制跨度内的各个单元格内容。
  • 最后,任何指定的 foregroundDecoration 都会绘制在内容之上。

这里默认的轴是 Axis.vertical ,所以,先绘制列,然后绘制行,边框与行装饰重叠,所以,让我们为前景装饰添加边框。WZV28资讯网——每日最新资讯28at.com

TableSpan buildTableSpan(int index) {  TableSpanDecoration foreGroundDecoration = const TableSpanDecoration(      border: TableSpanBorder(          trailing: BorderSide(color: Colors.black),          leading: BorderSide(color: Colors.black)));  TableSpanDecoration backGroundDecoration = TableSpanDecoration(    color: index == 0 ? Colors.grey[300] : null,  );  return TableSpan(      extent: const FixedTableSpanExtent(100),      backgroundDecoration: backGroundDecoration,      foregroundDecoration: foreGroundDecoration);}

在前景装饰中添加边框可确保其呈现在 TableView 中单元格内容的顶部。WZV28资讯网——每日最新资讯28at.com

图片图片WZV28资讯网——每日最新资讯28at.com

TableSpan.extent

它表示行的高度和列的宽度,TableSpanExtent 有四种类型。WZV28资讯网——每日最新资讯28at.com

1.FixedTableSpanExtent

具有固定[像素]的跨度。WZV28资讯网——每日最新资讯28at.com

extent: const FixedTableSpanExtent(50),

图片图片WZV28资讯网——每日最新资讯28at.com

2.FractionTableSpanExtent

它将跨度范围指定为视口范围的一部分。它与 Expanded 小部件相同,根据提供的分数占用空间。WZV28资讯网——每日最新资讯28at.com

extent: const FractionalTableSpanExtent(0.5),

图片图片WZV28资讯网——每日最新资讯28at.com

3.RemainingTableSpanExtent

它指定跨度应占据视口中的剩余空间。WZV28资讯网——每日最新资讯28at.com

TableSpan buildColumnSpan(int index) {  TableSpanDecoration decoration =  const TableSpanDecoration(      border: TableSpanBorder(          trailing: BorderSide(color: Colors.black),          leading: BorderSide(color: Colors.black)));  return TableSpan(      extent: index==0? FixedTableSpanExtent(100):RemainingTableSpanExtent(), backgroundDecoration: decoration);}

图片图片WZV28资讯网——每日最新资讯28at.com

4.CombiningTableSpanExtent

它以两个范围作为参数,并通过组合器函数运行这两个范围的结果。WZV28资讯网——每日最新资讯28at.com

TableSpan buildRowSpan(int index) {    TableSpanExtent extent1 = FixedTableSpanExtent(100);    TableSpanExtent extent2 = FixedTableSpanExtent(100);    double combiner(double value1, double value2) {      return value1 + value2;    }    TableSpanDecoration foreGroundDecoration = const TableSpanDecoration(        border: TableSpanBorder(            trailing: BorderSide(color: Colors.black),            leading: BorderSide(color: Colors.black)));    TableSpanDecoration backGroundDecoration = TableSpanDecoration(      color: index == 0 ? Colors.grey[300] : null,    );    if (index == 1) {      return TableSpan(          extent: CombiningTableSpanExtent(extent1, extent2, combiner),          backgroundDecoration: backGroundDecoration,          foregroundDecoration: foreGroundDecoration);    }    return TableSpan(        extent: const FixedTableSpanExtent(100),        backgroundDecoration: backGroundDecoration,        foregroundDecoration: foreGroundDecoration);  }

图片图片WZV28资讯网——每日最新资讯28at.com

当鼠标指针(按下或不按下按钮)进入此跨度描述的行或列时触发。WZV28资讯网——每日最新资讯28at.com

void Function(PointerEnterEvent)? onEnter

void Function(PointerExitEvent)? onExit

当鼠标指针(按下或未按下按钮)退出此跨度描述的行或列时,它会触发。WZV28资讯网——每日最新资讯28at.com

void Function(PointerExitEvent)? onExit

recognizerFactories

recognizerFactories: <Type, GestureRecognizerFactory>{  TapGestureRecognizer:  GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(        () => TapGestureRecognizer(),        (TapGestureRecognizer t) {          t.notallow=(TapDownDetails tapdownDetails){            print(tapdownDetails.localPosition);          };          t.notallow=(TapUpDetails tapupDetails){            print(tapupDetails.localPosition);          };        }     ),},

recognizerFactories 是一个映射,其中键是手势类型,值是 GestureRecognizerFactory 的实例。WZV28资讯网——每日最新资讯28at.com

GestureRecognizerFactoryWithHandlers 有两个参数。调用时返回 TapGestureRecognizer 实例的函数和初始化 TapGestureRecognizer 并设置事件处理程序以处理特定事件详细信息的回调函数。WZV28资讯网——每日最新资讯28at.com

添加填充

padding 属性用于为每行和每列添加填充。WZV28资讯网——每日最新资讯28at.com

padding: TableSpanPadding(leading: 10),

向 UI 添加更多列和行

让我们在 TableView 中显示来自 Employee 类的更多数据,例如 email 和 role 。WZV28资讯网——每日最新资讯28at.com

Widget addText(TableVicinity vicinity) {    if (vicinity.yIndex == 0 && vicinity.xIndex == 0) {      return const Text("Index");    } else if (vicinity.yIndex == 0 && vicinity.xIndex == 1) {      return const Text("name");    } else if (vicinity.yIndex == 0 && vicinity.xIndex == 2) {      return const Text("Email");    } else if (vicinity.yIndex == 0 && vicinity.xIndex == 3) {      return const Text("Role");    } else if (vicinity.xIndex == 0) {      return Text(employees[vicinity.yIndex - 1].id);    } else if (vicinity.xIndex == 1) {      return Text(employees[vicinity.yIndex - 1].name);    } else if (vicinity.xIndex == 2) {      return Text(employees[vicinity.yIndex - 1].email);    } else if (vicinity.xIndex == 3) {      return Text(employees[vicinity.yIndex - 1].role);    }    return Text("");  }...      body: TableView.builder(          mainAxis: Axis.horizontal,          columnCount: 4,          rowCount: 21,          columnBuilder: buildColumnSpan,          rowBuilder: buildTableSpan,          cellBuilder: (BuildContext context, TableVicinity vicinity) {            return Center(child: addText(vicinity));          }),...TableSpan buildColumnSpan(int index) {  TableSpanDecoration decoration = const TableSpanDecoration(      border: TableSpanBorder(          trailing: BorderSide(color: Colors.black),          leading: BorderSide(color: Colors.black)));  if (index == 2) {    return TableSpan(      extent: const RemainingTableSpanExtent(),      backgroundDecoration: decoration,    );  } else if (index == 3) {    return TableSpan(      extent: const FractionalTableSpanExtent(0.5),      backgroundDecoration: decoration,    );  }  return TableSpan(      extent: FixedTableSpanExtent(100), backgroundDecoration: decoration);}

输出是:WZV28资讯网——每日最新资讯28at.com

图片图片WZV28资讯网——每日最新资讯28at.com

固定行和列

固定持续出现在 TableView 视口边缘的特定数量的行和列。WZV28资讯网——每日最新资讯28at.com

TableView.builder(...    pinnedRowCount: 1,    pinnedColumnCount: 1,   ),

图片图片WZV28资讯网——每日最新资讯28at.com

滚动细节

ScrollableDetail 允许对小部件的垂直和水平滚动行为进行特定配置。WZV28资讯网——每日最新资讯28at.com

verticalDetails: ScrollableDetails.vertical(  reverse: true,  controller: verticalController,  physics: const AlwaysScrollableScrollPhysics(),  decorationClipBehavior: Clip.hardEdge),

ScrollableDetails 中的 verticalDetails 允许对小部件的垂直滚动行为进行特定配置,它封装了各种属性。WZV28资讯网——每日最新资讯28at.com

verticalDetails: ScrollableDetails.vertical(  reverse: true,  controller: verticalController,  physics: const AlwaysScrollableScrollPhysics(),  decorationClipBehavior: Clip.hardEdge,)

以下是属性的详细说明:WZV28资讯网——每日最新资讯28at.com

  • reverse :当设置为 true 时,小部件内的内容向相反方向滚动。
  • controller :指定用于管理和控制滚动行为的 ScrollController 。它允许您滚动到特定位置或收听滚动偏移量的变化。
  • physics :确定滚动物理的行为。
  • decorationClipBehavior :指定可滚动区域装饰的剪切行为

cacheExtent 缓存范围

cacheExtent: 200,

与ListView类似, cacheExtent 是在进入屏幕可见部分之前绘制的区域的大小。WZV28资讯网——每日最新资讯28at.com

DiagonalDragBehavior 对角线拖动行为

该枚举允许开发人员指定如何使用 kTouchSlop 处理对角滚动。WZV28资讯网——每日最新资讯28at.com

结论

在本教程中,我们学习了如何使用 Flutter 中的 two_dimensional_scrollable 实现 Flutter 中的 tableView,我们可以用它做更多的事情并使用它添加更多功能。这是我对 Flutter 中 TableView 的一个小介绍。WZV28资讯网——每日最新资讯28at.com

图片翻译自:https://blog.canopas.com/how-to-implement-2d-scrollable-tableview-in-flutter-a8b0fe703614图片WZV28资讯网——每日最新资讯28at.com

参考资料:

[1]这里: https://gist.github.com/cp-sneha-s/d2375b2a624f5650fa3836a84805b15fWZV28资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-100463-0.html如何在 Flutter 中实现 2D 可滚动的表格,探索 Flutter 中的二维可滚动项,表格解决方案

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 一文带您理解Python的map/filter/reduce

下一篇: 基于Rspack实现大仓应用构建提效实践

标签:
  • 热门焦点
  • 6月iOS设备好评榜:第一蝉联榜首近一年

    作为安兔兔各种榜单里变化最小的那个,2023年6月的iOS好评榜和上个月相比没有任何排名上的变化,仅仅是部分设备好评率的下降,长年累月的用户评价和逐渐退出市场的老款机器让这
  • JavaScript 混淆及反混淆代码工具

    介绍在我们开始学习反混淆之前,我们首先要了解一下代码混淆。如果不了解代码是如何混淆的,我们可能无法成功对代码进行反混淆,尤其是使用自定义混淆器对其进行混淆时。什么是混
  • 一年经验在二线城市面试后端的经验分享

    忠告这篇文章只适合2年内工作经验、甚至没有工作经验的朋友阅读。如果你是2年以上工作经验,请果断划走,对你没啥帮助~主人公这篇文章内容来自 「升职加薪」星球星友 的投稿,坐
  • 得物效率前端微应用推进过程与思考

    一、背景效率工程随着业务的发展,组织规模的扩大,越来越多的企业开始意识到协作效率对于企业团队的重要性,甚至是决定其在某个行业竞争中突围的关键,是企业长久生存的根本。得物
  • Flowable工作流引擎的科普与实践

    一.引言当我们在日常工作和业务中需要进行各种审批流程时,可能会面临一系列技术和业务上的挑战。手动处理这些审批流程可能会导致开发成本的增加以及业务复杂度的上升。在这
  • 如何通过Python线程池实现异步编程?

    线程池的概念和基本原理线程池是一种并发处理机制,它可以在程序启动时创建一组线程,并将它们置于等待任务的状态。当任务到达时,线程池中的某个线程会被唤醒并执行任务,执行完任
  • 联想小新Pad Pro 12.6将要推出,搭载高通骁龙 870 处理器

    联想小新Pad Pro 12.6将于秋季新品会上推出,官方按照惯例直接在发布会前给出了机型的所有参数。联想小新 Pad Pro 12.6 将搭载高通骁龙 870 处理器,重量为 5
  • 亲历马斯克血洗Twitter,硅谷的苦日子在后头

    文/刘哲铭  编辑/李薇  马斯克再次挥下裁员大刀。  美国时间11月14日,Twitter约4400名外包员工遭解雇,此次被解雇的员工的主要工作为内容审核等。此前,T
  • 荣耀Magic4 至臻版 首创智慧隐私通话 强劲影音系统

    2022年第一季度临近尾声,在该季度内,许多品牌陆续发布自己的最新产品,让大家从全新的角度来了解当今的手机技术。手机是电子设备中,更新迭代十分迅速的一款产品,基
Top