【问题标题】:how I can get The value of The Chip when the user Pressed or select from the list of chip widget当用户按下或从芯片小部件列表中选择时,我如何获得芯片的值
【发布时间】:2021-06-18 22:02:28
【问题描述】:

我正在创建一个允许芯片在水平空间上水平滚动的功能。为了实现这个功能,我们在 SingleChildScrollView 中开发了 Row 组件。当用户按下其中一个筹码时,我想要筹码按钮的值,onpressed 对我不起作用??

here is my Build method where is use the chips

  @override
  Widget build(BuildContext context) {
    print(widget.useridforinfo);
    print(widget.usertypeforinfo);
    return Scaffold(
     key: _key,
      appBar: AppBar(
        title: Text(widget.title),
        centerTitle: true,
        elevation: 0.0,
        backgroundColor: Colors.orange.shade900,
      ),
      body: Column(
        children: <Widget>[
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: rowChips(),
          ),
        ],
      ),

      );
  }

 rowChips(){
    return Row(
      children: <Widget>[
        chipForRow("Aids Abeba",Color(0xFFff8a65)),
        chipForRow("Adama",Color(0xFF9575cd)),
        chipForRow("Hawasssa",Color(0xFF4db6ac)),
        chipForRow("Bishoftu",Color(0xFF5cda65)),
        chipForRow("Aids Abeba",Color(0xFFff8a65)),
        chipForRow("Adama",Color(0xFF9575cd)),
        chipForRow("Hawasssa",Color(0xFF4db6ac)),
        chipForRow("Bishoftu",Color(0xFF5cda65)),
      ],
    );
  }

here is my method to Create the chips

  Widget chipForRow(String label, Color color){
    return Container(
      margin: EdgeInsets.all(6.0),
      child: Chip(
        labelPadding: EdgeInsets.all(5.0),
         avatar: CircleAvatar(
           backgroundColor: Colors.grey.shade900,
           child:  Text(label[0].toUpperCase())
         ),
        label: Text(
          label,
          style: TextStyle(
            color: Colors.white,
          ),
        ),
        backgroundColor: color,
        elevation: 6.0,
        shadowColor: Colors.grey[60],
        padding: EdgeInsets.all(6.0),

      ),

    );

  }

【问题讨论】:

标签: flutter dart


【解决方案1】:

用 GestureDetector Widget 包裹 Chip 小部件,并像这样使用 onTap 参数:

更简单的方法

...

GestureDetector(
    child: Chip(
      labelPadding: EdgeInsets.all(5.0),
      avatar: CircleAvatar(
          backgroundColor: Colors.grey.shade900,
          child: Text(label.toString()[0].toUpperCase())),
      label: Text(
        label.toString(),
        style: TextStyle(
          color: Colors.white,
        ),
      ),
      backgroundColor: color,
      elevation: 6.0,
      shadowColor: Colors.grey[60],
      padding: EdgeInsets.all(6.0),
    ),
    onTap: () {
      //Prints the label of each tapped chip
      print(label);
    },
  ),

 ...

另一种方法

创建一个列表来保存 ChipForRow 对象

//List to hold Chip Objects
  final List<ChipForRow> chips = [
    ChipForRow("Aids Abeba", Color(0xFFff8a65)),
    ChipForRow("Adama", Color(0xFF9575cd)),
    ChipForRow("Hawasssa", Color(0xFF4db6ac)),
    ChipForRow("Bishoftu", Color(0xFF5cda65)),
    ChipForRow("Aids Abeba", Color(0xFFff8a65)),
    ChipForRow("Adama", Color(0xFF9575cd)),
    ChipForRow("Hawasssa", Color(0xFF4db6ac)),
    ChipForRow("Bishoftu", Color(0xFF5cda65)),
  ];

创建一个 Stateful Widget 类 ChipFirRow 而不是函数

class ChipForRow extends StatelessWidget {
  final String label;
  final Color color;

  ChipForRow(this.label, this.color);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(6.0),
      child: GestureDetector(
        child: Chip(
          labelPadding: EdgeInsets.all(5.0),
          avatar: CircleAvatar(
              backgroundColor: Colors.grey.shade900,
              child: Text(label.toString()[0].toUpperCase())),
          label: Text(
            label.toString(),
            style: TextStyle(
              color: Colors.white,
            ),
          ),
          backgroundColor: color,
          elevation: 6.0,
          shadowColor: Colors.grey[60],
          padding: EdgeInsets.all(6.0),
        ),
        onTap: () {
          print(label);
        },
      ),
    );
  }
}

使用**第二种方法,我们可以在任何ChipForRow小部件被选择或取消选择时重建它,通过添加/删除指示何时选择或取消选择芯片它是字符串列表中的标签。

【讨论】:

  • 谢谢?是否可以在用户按下时更改芯片的背景颜色,我使用的是普通芯片,在我上面的代码中,我想在用户按下时更改芯片的背景颜色
  • 谢谢大哥,GestureDetector() 工作正常,但是我想改变Pressed芯片的背景颜色,当用户按下芯片时,
  • 是否可以选择多个芯片,无需制作芯片过滤芯片,我想使用普通芯片,但我也希望用户一次选择多个芯片跨度>
  • @bebe,选择多个Chip是很有可能的。但首先你必须稍微改变你的实现方法。我将编辑我的答案以反映我的解决方案
  • 您的代码工作正常,但我想知道如何在您的代码中添加两(2)个东西,首先,我想在用户选择时更改芯片的颜色,其次,我想用户一次选择两个以上的芯片,请将这两个东西添加到您的答案中,谢谢您的提前帮助,
猜你喜欢
  • 1970-01-01
  • 2019-09-11
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多