代码之家  ›  专栏  ›  技术社区  ›  heyr

GestureDetector ONTAP卡

  •  2
  • heyr  · 技术社区  · 7 年前
    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中动态构建一张卡时,我在想,与其像以前那样在每张卡中保留floatingaction按钮,不如在每张卡中实现一个ontap方法并触发一些东西。 换言之,我想保持卡尽可能简单,没有许多小部件。 提前谢谢你!

    3 回复  |  直到 7 年前
        1
  •  10
  •   creativecreatorormaybenot    6 年前

    AS Card 是“一张材料”,你可能想用 InkWell ,其中包括 材料 基于最近的 Material 祖先。

    return Card(
      child: InkWell(
        onTap: () {
            // function gets executed on a tap
        },
        child: ..,
      ),
    );
    
        2
  •  6
  •   Phuah Yee Keat    6 年前

    你真的应该用墨水瓶而不是卡片包装孩子:

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

    这将使飞溅动画正确地显示在卡的内部,而不是卡的外部。

        3
  •  3
  •   Vinoth Kumar    7 年前

    用手势检测器把卡片包起来,如下所示,

    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');
      }
    }