Android Flutter实现兴趣标签选择功能

作者:岛上码农 时间:2021-07-05 14:29:17 

前言

我们在首次使用内容类 App 的时候,不少都会让我们选择个人偏好。这种通常是通过标签来实现,比如列举出一系列的技术栈,然后让我们选择。通过这些标签选择可以预先知道用户的偏好信息,从而可以选择感兴趣的内容进行推送,这样会让用户快速看到想看的内容。我们本篇就来看看 Flutter 如何实现兴趣标签的选择,这可以通过 InputChip 来轻松实现。

InputChip

InputChip 类是一个简单的小组件,它的内容区左侧有一个 avatar 子组件,右侧是一个 label 组件。然后支持删除和标记选中,因此非常适合做兴趣标签的选择。下面是未选中和选中的两个状态,选中的时候会在左侧的 avatar 区域打个勾表示选中。这个组件相比我们自己去写一个类似的组件来说会简化很多。而且,多个InputChip 组件可以作为Wrap 组件的子组件,实现多个 InputChip 时自动等间距排布和超出宽度自动换行,这也恰恰是做兴趣标签所需要的。

Android Flutter实现兴趣标签选择功能

我们来看一下 InputChip 构造方法和主要属性。

const InputChip({
 Key? key,
 this.avatar,
 required this.label,
 this.labelStyle,
 this.labelPadding,
 this.selected = false,
 this.isEnabled = true,
 this.onSelected,
 this.deleteIcon,
 this.onDeleted,
 this.deleteIconColor,
 this.deleteButtonTooltipMessage,
 this.onPressed,
 this.pressElevation,
 this.disabledColor,
 this.selectedColor,
 this.tooltip,
 this.side,
 this.shape,
 this.clipBehavior = Clip.none,
 this.focusNode,
 this.autofocus = false,
 this.backgroundColor,
 this.padding,
 this.visualDensity,
 this.materialTapTargetSize,
 this.elevation,
 this.shadowColor,
 this.selectedShadowColor,
 this.showCheckmark,
 this.checkmarkColor,
 this.avatarBorder = const CircleBorder(),
 @Deprecated(
   'Migrate to deleteButtonTooltipMessage. '
   'This feature was deprecated after v2.10.0-0.3.pre.'
 )
 this.useDeleteButtonTooltip = true,
})

属性很多,但是实际用的是下面这几个:

  • avatar:左侧的子组件,通常可以用使用圆形(如CircularAvatar)组件,注意高度是不可改的,随整个 InputChip 的高度决定;

  • label:右侧的标签组件,通常是一个文本组件,支持单行或多行文本,该组件决定了 InputChip 的高度;

  • labelPadding:标签的内边距;

  • selected:选中状态,如果是选中状态则会在左侧有个打勾的标记;

  • isEnabled:是否启用,默认是启用状态,如果禁用则选中事件的回调(onSelected)和点击事件回调(onPressed)都无法使用,但是删除是可以用的。

  • onSelected:选中状态改变时的回调函数。

  • deleteIcon:删除图标,默认是 Icons.cancel 图标。

  • onDeleted:删除事件回调。

  • onPressed:点击事件回调;

  • backgroundColorselectedColor:默认背景色和选中后背景色。

通过这些属性我们就可以构建基础的兴趣标签,比如下面的代码,这里的 item 是标签的数据实体对象,有三个属性,分别是标签名称 name,标签默认背景色color 和选中状态 selected。 当标签选中后我们将 InputChipavatar设置为 null,从而不显示 avatar

InputChip(
   labelPadding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
   backgroundColor: item.color,
   selectedColor: Colors.red[400],
   selected: item.selected,
   onSelected: (isSelected) {
     setState(() {
       item.selected = isSelected;
     });
   },
   avatar: item.selected
       ? null
       : CircleAvatar(
           backgroundColor: Colors.lightBlue,
           child: Text(
             item.name.substring(0, 1),
             style: const TextStyle(color: Colors.white),
           ),
         ),
   label: Text(
     item.name,
   ),
 )

兴趣标签选择实现

兴趣标签通常会有多个,这时候需要逐个等间距排开,超出宽度后换行。这个可以通过 Wrap 组件和 InputChip 组件实现。代码非常简单,就是将一组 InputChip 组件作为 Wrap 组件的 children 参数,然后设置 Wrap 中子组件的间距即可。

Wrap(
 spacing: 10.0,
 children: _techList
     .map(
       (item) => InputChip(
         labelPadding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
         backgroundColor: item.color,
         selectedColor: Colors.red[400],
         selected: item.selected,
         onSelected: (isSelected) {
           setState(() {
             item.selected = isSelected;
           });
         },
         avatar: item.selected
             ? null
             : CircleAvatar(
                 backgroundColor: Colors.lightBlue,
                 child: Text(
                   item.name.substring(0, 1),
                   style: const TextStyle(color: Colors.white),
                 ),
               ),
         label: Text(
           item.name,
         ),
       ),
     )
     .toList(),
),

最终我们实现的效果如下图所示。

Android Flutter实现兴趣标签选择功能

来源:https://juejin.cn/post/7163059109665701902

标签:Android,Flutter,标签,选择
0
投稿

猜你喜欢

  • Android开发中计算器的sin、cos及tan值计算问题分析

    2023-11-11 08:21:40
  • 关于c#中枚举类型支持显示中文的扩展说明

    2023-02-02 22:35:02
  • 关于Mybatis与JPA的优缺点说明

    2023-08-23 22:28:33
  • SpringBoot+docker环境变量配置详解

    2022-06-13 07:37:20
  • Java web实现账号单一登录,防止同一账号重复登录(踢人效果)

    2021-05-24 13:26:09
  • phonegap教程使用jspdf库在应用中生成pdf文件(pdf生成方法)

    2022-10-29 05:50:11
  • Java在web页面上的编码解码处理及中文URL乱码解决

    2023-08-25 11:10:19
  • Java各种排序算法汇总(冒泡,选择,归并,希尔及堆排序等)

    2021-10-05 14:46:23
  • C# 线程安全详解

    2023-02-07 10:40:46
  • 老生常谈java中cookie的使用

    2023-11-11 04:37:59
  • Android webview如何加载HTML,CSS等语言的示例

    2022-09-20 23:06:18
  • Android 6.0权限请求相关及权限分组方法

    2023-11-27 20:08:03
  • 关于C#理解装箱与拆箱

    2023-06-18 21:07:50
  • java文件上传下载代码实例

    2023-11-10 05:06:14
  • 海量数据去重排序bitmap(位图法)在java中实现的两种方法

    2022-10-10 17:27:36
  • Java实现图片倒影的源码实例内容

    2022-08-30 02:39:24
  • 利用Postman和Chrome的开发者功能探究项目(毕业设计项目)

    2021-10-27 10:19:43
  • Java thread.isInterrupted() 返回值不确定结果分析解决

    2023-11-09 19:27:09
  • Java实现两人五子棋游戏(五) 判断是否有一方胜出

    2022-03-03 18:29:44
  • Java自定义过滤器和拦截器实现ThreadLocal线程封闭

    2023-09-19 04:29:04
  • asp之家 软件编程 m.aspxhome.com