一次仅使ListView的一个孩子可点击

时间:2018-07-18 23:44:37

标签: listview dart flutter

每当我同时单击Listview的多个项目时,print就会被多次调用。如何在不使用锁定布尔值的情况下一次仅使Listview的一个子项可单击?

class MyHomePage extends StatelessWidget {
  final List cards =
      new List.generate(6, (int i) => MyCard(testVariable: i)).toList();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ListView(
        children: cards,
      ),
    );
  }
}

class MyCard extends StatelessWidget {
  final testVariable;

  MyCard({@required this.testVariable});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 0.0),
      child: Card(
        child: new InkWell(
          onTap: () {
            print(testVariable);
          },
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(padding: EdgeInsets.all(100.0), child: Text(testVariable.toString()))
            ],
          ),
        ),
      ),
    );
  }
}

0 个答案:

没有答案