如何将 onTap 添加到 Flutter 索引内的单个图像?

How do I add an onTap to individual images inside an index on Flutter?

问题:我在带有图像索引的网格内使用切换按钮。当用户点击不同的图像时,切换按钮起作用并用蓝色边框突出显示所选图像,但它不会使用 OnPressed 或 OnTap 触发针对所选图像的特定操作。

我的解决方案:我已经尝试将 onTap 或 OnPressed 添加到索引内的单个图像,但它显示“此表达式的类型为 'void',因此无法使用其值。”并且不适用于打印语句。

class Backgrounds extends StatefulWidget {

  @override
  _BackgroundsState createState() => _BackgroundsState();
}

class _BackgroundsState extends State<Backgrounds> {

  List<bool> isSelected;

  void initState() {
    isSelected = [true, false, false, false, false, false, false, false, false];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var counter = 0;
    return GridView.count(
        padding: EdgeInsets.all(10),
        crossAxisCount: 3,
        mainAxisSpacing: 10,
        crossAxisSpacing: 8,
        children: [
         // Here is where I've added the onTap and the error comes up
          InkWell(
            onTap: print('hi'),
            child: Image.asset('images/image1.png'
            ),
          ),
          Image.asset('images/image2.png',
          ),
          Image.asset('images/image3.png'),
          Image.asset('images/image4.png'),
          Image.asset('images/image5.png'),
          Image.asset('images/image6.png'),
          Image.asset('images/image7.png'),
          Image.asset('images/image8.png'),
          Image.asset('images/image9.png'),
          ].asMap().entries.map((widget) {
          final index = ++counter - 1;
          return ToggleButtons(
    onPressed: (int index) {
    setState(() {
    for (int i = 0; i < isSelected.length; i++) {
    isSelected[i] = i == widget.key;
    }
    });
    },
     isSelected: [isSelected[widget.key]],
    selectedBorderColor: Color(0xff2244C7),
            borderColor: Colors.transparent,
    borderWidth: 1,
    borderRadius: BorderRadius.all(Radius.circular(8),
    ),
    children: [widget.value],
    );
    }).toList());
  }
}

应该这样写:

onTap: () {do whatever you need;},

错误是抱怨你放在那里的印刷品没有 return 任何东西。