简体   繁体   English

Flutter:在 ListView 中按下卡片小部件不起作用

[英]Flutter: Pressing on Card Widget in ListView Not Working

I have ListView builder adsList which contains LstTiles adTile contains the Card.我有 ListView builder adsList 包含 LstTiles adTile 包含卡片。

When I press the Card widget I expect an action and to navigate to another screen or just print in console, but nothing happens although I wrapped the card inside GestureDetector.当我按下卡片小部件时,我期待一个动作并导航到另一个屏幕或只是在控制台中打印,但是尽管我将卡片包裹在 GestureDetector 中,但什么也没有发生。

here is the code这是代码

class _AdTileState extends State<AdTile> {
@override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 8),
      child: GestureDetector(
        onTap: (){
          print('card pressed');
          Navigator.push(context, new MaterialPageRoute(builder: (context) => AdView()));
        },
        child: Card(
          margin: EdgeInsets.fromLTRB(20, 6, 20, 0),
          child: ListTile(
          trailing: Image.network(widget.adModel.adImage),
            title: Text(widget.adModel.adName),
            subtitle: Text(widget.adModel.location),

          ),
        ),
      ),
    );
  }
}

Here is the code inside AdList:这是 AdList 中的代码:

class _AdsListState extends State<AdsList> {
@override
  Widget build(BuildContext context) {

final ads = Provider.of<List<AdModel>>(context);

return ListView.builder(
    scrollDirection: Axis.vertical,
    itemCount:(ads == null) ? 0 :  ads.length,
    itemBuilder: (context, index){
      return
        CustomAdTile(adModel: ads[index],);
    });
  }
}

And here is the code on the Page which show the ListView Builder:这是页面上显示 ListView Builder 的代码:

 Widget build(BuildContext context) {
return StreamProvider<List<Profile>>.value(
  value: DatabaseService().profiles,
  child: StreamProvider<List<AdModel>>.value(
    value: DatabaseService().ads,
    child: Directionality(
      textDirection: TextDirection.rtl,
      child: Scaffold(
        appBar: AppBar(),
            body: Column(
              children: <Widget>[
                Container(),
                Row(),                                        
                Expanded(
                    child: AdsList()
                ),
              ],
            ),

Code for CustomAdTile: CustomAdTile 的代码:

    class _CustomAdTileState extends State<CustomAdTile> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 5.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expanded(
            flex: 3,
            child:  Padding(
              padding: const EdgeInsets.fromLTRB(0.0, 0.0, 10.0, 0.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    widget.adModel.adName,
                    style: const TextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: 14.0,
                    ),
                  ),
                  const Padding(padding: EdgeInsets.symmetric(vertical: 2.0)),
                  Text(
                    widget.adModel.location,
                    style: const TextStyle(fontSize: 10.0),
                  ),
                  const Padding(padding: EdgeInsets.symmetric(vertical: 1.0)),
                  Text(
                    '${widget.adModel.category} views',
                    style: const TextStyle(fontSize: 10.0),
                  ),
                ],
              ),
            ),
          ),
          const Icon(
            Icons.more_vert,
            size: 16.0,
          ),
          Container(
            height: 80,
            width: 80,
            decoration: BoxDecoration(
              image: DecorationImage(image: NetworkImage(widget.adModel.adImage),
              fit: BoxFit.fill),
            ),
          )
        ],
      ),

    );   
  }
}

It looks like you are never using AdTile, you are using CustomAdTile.看起来您从未使用过 AdTile,您正在使用 CustomAdTile。 You need to add the onTap inside CustomAdTile.您需要在 CustomAdTile 中添加 onTap。

Here is the updated code for CustomAdTile.这是 CustomAdTile 的更新代码。

class _CustomAdTileState extends State<CustomAdTile> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 5.0),
      child: InkWell(
        ///Add onTap here
        onTap: () {
          print('card pressed');
          Navigator.push(
              context, new MaterialPageRoute(builder: (context) => AdView()));
        },
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
              flex: 3,
              child: Padding(
                padding: const EdgeInsets.fromLTRB(0.0, 0.0, 10.0, 0.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      widget.adModel.adName,
                      style: const TextStyle(
                        fontWeight: FontWeight.w500,
                        fontSize: 14.0,
                      ),
                    ),
                    const Padding(padding: EdgeInsets.symmetric(vertical: 2.0)),
                    Text(
                      widget.adModel.location,
                      style: const TextStyle(fontSize: 10.0),
                    ),
                    const Padding(padding: EdgeInsets.symmetric(vertical: 1.0)),
                    Text(
                      '${widget.adModel.category} views',
                      style: const TextStyle(fontSize: 10.0),
                    ),
                  ],
                ),
              ),
            ),
            const Icon(
              Icons.more_vert,
              size: 16.0,
            ),
            Container(
              height: 80,
              width: 80,
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: NetworkImage(widget.adModel.adImage),
                    fit: BoxFit.fill),
              ),
            )
          ],
        ),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM