I wanted to create an InputChip Widget that looked the this (The red circle). screenshot from figma M3 Official Figma

有了这个代码,我就可以用蓝色圈出芯片:

@override
Widget build(BuildContext context) {
  return InputChip(
    label: const Text('tag'),
    onDeleted: () {},
  );
}

通过添加一些参数,我可以更改 colored颜色 :

@override
Widget build(BuildContext context) {
  return InputChip(
    label: const Text('tag'),
    backgroundColor: Colors.blue[100],
    onDeleted: () {},
  );
}

screenshot of my widget

但我不能删除轮廓.

我已try 将selected参数更改为true,但这会创建一个我不想要的复选标记:

@override
Widget build(BuildContext context) {
  return InputChip(
    label: const Text('tag'),
    selected: true,
    onDeleted: () {},
  );
}

enter image description here

我怎样才能做到这一点呢?

推荐答案

如果您看到InputChip的实现,则边框样式由值isSelected控制:

@override
BorderSide? get side => !isSelected
  ? isEnabled
    ? BorderSide(color: _colors.outline)
    : BorderSide(color: _colors.onSurface.withOpacity(0.12))
  : const BorderSide(color: Colors.transparent);

从源代码看,似乎没有其他方法可以实现边框样式和背景 colored颜色 的精确组合,所以您只需像这样启用selected和禁用showCheckmark:

InputChip(
  // ...
  selected: true,
  showCheckmark: false,
)

Note:也可以将side设置为BorderSide(color: Colors.transparent),但我们还必须手动设置背景 colored颜色 . Select 芯片时使用的背景 colored颜色 取自RawChip中名为_getBackgroundColor的私有方法,这使得解决方案更加复杂.

Flutter相关问答推荐

滚动时应用栏 colored颜色 更改Flutter

Flutter:InteractiveViewer中可伸缩SVG背景上的定位小部件不对齐

Flutter 布局-全屏幕容器

如何在扩展其他小部件时访问Ref

在背景图像上排列定位徽标

Android Studio扩展控件上未加载Google map

使用Ffltter CREATE创建项目时,androidManifest.xml中缺少包属性

为什么我的Flutter 动画过渡没有发生?

Firestore 不会取代 map

Dart/Flutter 泛型:类型 '(SearchFilterItemModel) => String' 不是类型 '(SearchFilterItemModel) => String' 的子类型

如何修改来自合并 list 的 Android list 权限?

如何获取flutter中gridview项目占用的高度和宽度?

当键盘隐藏表单时该怎么办?

Flutter蓝牙 - 如何通过蓝牙从设备中获取文件?

如何使Flutter 屏幕无法关闭?

将状态维护到 asyncNotifier

Flutter Websockets MacOS:相同的代码在调试模式下有效,但在发布模式下无效:(操作系统错误:提供了 node 名或服务名..)

无法在Flutter 图像小部件中打开大尺寸(500MB)图像

如何在Flutter中制作带有波浪边框的圆圈来显示图像?

如何在Flutter 中根据其父小部件高度设置图标按钮飞溅半径