简体   繁体   English

如何在 flutter 中将 onTap() 或 onPressed() 添加到 ExpansionTileCard?

[英]How to add onTap() or onPressed() to ExpansionTileCard in flutter?

I am using below ExpansionTileCard widget for the ListView.Builder, I am able to generate the list using this, but I am not able add the functionality if the user presses the item in the list and then to perform a FlutterToast Action, I tried to find the solution with the flutter package but no success yet我在下面为 ListView.Builder 使用 ExpansionTileCard 小部件,我可以使用它生成列表,但是如果用户按下列表中的项目然后执行 FlutterToast 操作,我无法添加该功能,我试图使用 flutter package 找到解决方案,但尚未成功

Please guide me how to do that.请指导我如何做到这一点。

  child:  ExpansionTileCard(

    leading: CircleAvatar(child: Image.network(widget.Picurl)),
    title: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text("text1"),
        Text('text2'),
      ],
    ),
    subtitle: Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Text("Text1"),
            SizedBox(width: 5,),
            Text("Text2")
          ],
        ),

      ],
    ),
    children: <Widget>[
      Divider(
        thickness: 1.0,
        height: 1.0,

      ),
      Align(
        alignment: Alignment.centerLeft,
        child: Padding(
          padding: const EdgeInsets.symmetric(
            horizontal: 16.0,
            vertical: 8.0,
          ),
          child: Text(
          widget.postcontent,
            style: Theme.of(context)
                .textTheme
                .body1
                .copyWith(fontSize: 16),
          ),
        ),
      ),
      ButtonBar(
        alignment: MainAxisAlignment.spaceAround,
        buttonHeight: 52.0,
        buttonMinWidth: 90.0,
        children: <Widget>[
          FlatButton(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4.0)),
            onPressed: () {


               },
            child: Column(
              children: <Widget>[
                Icon(Icons.star),
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 2.0),
                ),
                Text('Button1'),
              ],
            ),
          ),
          FlatButton(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4.0)),
            onPressed: () {



            },
            child: Column(
              children: <Widget>[
                Icon(Icons.open_in_browser),
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 2.0),
                ),
                Text('Button2'),

              ],
            ),
          ),

        ],
      ),
    ],
  ),

Try wrapping with a GestureDetector.尝试使用 GestureDetector 进行包装。

Just put the widget you want to register tap or pressed gestures inside a GestureDetector widget and select the appropriate callback for needed gesture events只需将要注册轻击或按下手势的小部件放入GestureDetector小部件和 select 所需手势事件的适当回调

GestureDetector(
   onTap: () {
       // your code here
   },
   child: ExpansionTileCard(
       ........
   ),
),

For more details and gesture options: https://api.flutter.dev/flutter/widgets/GestureDetector-class.html有关更多详细信息和手势选项: https://api.flutter.dev/flutter/widgets/GestureDetector-class.html

If you want the ripple effect when the widget is being tapped, you should wrap the widget inside InkWell如果您想要在点击小部件时产生涟漪效果,您应该将小部件包装在InkWell

Here is how -这是如何 -

 InkWell(
   onTap: () {
       // your code here
   },
   child: ExpansionTileCard(
       ........
   ),
),

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

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