首页 文章

GestureDetector onTap卡

提问于
浏览
0
new Expanded(

        child: _searchResult.length != 0 || controller.text.isNotEmpty
            ? new ListView.builder(
                itemCount: _searchResult.length,
                itemBuilder: (context, int i) {

                  return new Card(

                      child: new Column(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                        new Row(children: <Widget>[
                          //new GestureDetector(),

                          new Container(

                              width: 45.0,
                              height: 45.0,
                              decoration: new BoxDecoration(
                                  shape: BoxShape.circle,
                                  image: new DecorationImage(
                                      fit: BoxFit.fill,
                                      image: new NetworkImage(
                                          "https://raw.githubusercontent.com/flutter/website/master/_includes/code/layout/lakes/images/lake.jpg")))),
                          new Text(
                              " " +
                                  userDetails[returnTicketDetails[i]
                                      ["user_id"]]["first_name"] +
                                  " " +
                                  (userDetails[returnTicketDetails[i]
                                      ["user_id"]]["last_name"]),
                              style: const TextStyle(
                                  fontFamily: 'Poppins', fontSize: 20.0)),
                        ]),
                        new Column(
                          children: <Widget>[
                            new Align(
                                alignment: FractionalOffset.topRight,
                                child: new FloatingActionButton(
                                  onPressed: () {

                                    groupId = returnTicketDetails[i]["id"];

                                    print(returnTicketDetails[i]["id"]);
                                    print(widget.id);

                                    Navigator.push(
                                        context,
                                        new MaterialPageRoute(
                                            builder: (context) => new Tickets(groupId,widget.id)));

                                  },
                                  heroTag: null,
                                  backgroundColor: Color(0xFF53DD6C),
                                  child: new Icon(Icons.arrow_forward),
                                )),
                            new Padding(padding: new EdgeInsets.all(3.0)),
                          ],
                        )
                      ]));
                },
              )
            : new ListView.builder(
                itemCount: _searchResult.length,
                itemBuilder: (context, int i) {
                  return new Card(
                    child: new ListTile(
                        //title: new Text(userDetails[returnTicketDetails[i]["user_id"]]["first_name"]),
                        ),
                    margin: const EdgeInsets.all(0.0),
                  );
                },
              ),
      ),

嗨,大家好!当我在ListView中动态构建一个Card时,我正在思考而不是像我一样在每个中保留FloatingActionButton,在每张卡中实现onTap方法并触发一些东西 . 换句话说,我想保持卡尽可能简单,没有很多小部件 . 先感谢您!

3 回答

  • 2

    由于Card是"a sheet of Material",您可能希望使用InkWell,其中包括基于最接近的 Material 祖先的材质高光和闪光效果 .

    return InkWell(
      onTap: () {
        // function gets executed on a tap
      },
      child: Card(),
    );
    
  • 2

    用GestureDetector包装卡片,如下所示,

    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return new ListView.builder(
          itemBuilder: (context, i) {
            new GestureDetector(
              child: new Card(
              ....    
              ),
              onTap: onCardTapped(i),
            );
          },
        );
      }
    
      onCardTapped(int position) {
        print('Card $position tapped');
      }
    }
    
  • 2

    您应该真正将孩子包裹在InkWell而不是Card中:

    return Card(
        child: InkWell(onTap: () {}, 
                       child: Text("hello")));
    

    这将使启动动画在卡内正确显示,而不是在卡外部 .

相关问题