簡體   English   中英

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

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

我在下面為 ListView.Builder 使用 ExpansionTileCard 小部件,我可以使用它生成列表,但是如果用戶按下列表中的項目然后執行 FlutterToast 操作,我無法添加該功能,我試圖使用 flutter package 找到解決方案,但尚未成功

請指導我如何做到這一點。

  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'),

              ],
            ),
          ),

        ],
      ),
    ],
  ),

嘗試使用 GestureDetector 進行包裝。

只需將要注冊輕擊或按下手勢的小部件放入GestureDetector小部件和 select 所需手勢事件的適當回調

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

有關更多詳細信息和手勢選項: https://api.flutter.dev/flutter/widgets/GestureDetector-class.html

如果您想要在點擊小部件時產生漣漪效果,您應該將小部件包裝在InkWell

這是如何 -

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM