在以下情況下,如何將 onClick 或 onTap 事件添加到不同的小部件圖示?
child: RightPanel(
likes: "${widget.likes}",
comments: "${widget.comments}",
shares: "${widget.shares}",
views: "${widget.popularity}",
popularity: "${widget.popularity}",
),
我試圖包裝每個圖示,例如
Inkwell(
child: RightPanel(
onTap: (){
like: "${widget.likes}",
},
comments: "${widget.comments}",
shares: "${widget.shares}",
views: "${widget.popularity}",
popularity: "${widget.popularity}",
),
但我得到一個編譯錯誤
下面我包含了上面代碼片段參考的 RightPanel 類。
class RightPanel extends StatelessWidget {
final String likes;
final String comments;
final String shares;
final String views;
final String popularity;
const RightPanel({
Key ?key,
required this.likes,
required this.comments,
required this.shares,
required this.views,
required this.popularity,
}) : super(key: key);
final Size size;
@override
Widget build(BuildContext context) {
return Expanded(
child: Container(
height: size.height,
child: Column(
children: <Widget>[
Container(
height: size.height * 0.25,
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
getIcons(Icons.heart, likes, 35.0),
getIcons(Icons.chat_bubble, comments, 35.0),
getIcons(Icons.reply, shares, 25.0),
getIcons(Icons.views, popularity, 30.0),
],
))
],
),
),
);
}
}
uj5u.com熱心網友回復:
child: RightPanel(
likeOnTap: (){},
commentOnTap: (){},
shareOnTap: (){},
viewOnTap: (){},
likes: "${widget.likes}",
comments: "${widget.comments}",
shares: "${widget.shares}",
views: "${widget.popularity}",
popularity: "${widget.popularity}",
),
class RightPanel extends StatelessWidget {
final String likes;
final String comments;
final String shares;
final String views;
final String popularity;
final VoidCallback likeOnTap;
final VoidCallback commentOnTap;
final VoidCallback shareOnTap;
final VoidCallback viewOnTap;
const RightPanel({
Key? key,
required this.likes,
required this.comments,
required this.shares,
required this.views,
required this.popularity, required this.likeOnTap, required this.commentOnTap, required this.shareOnTap, required this.viewOnTap, required this.size,
}) : super(key: key);
final Size size;
@override
Widget build(BuildContext context) {
return Expanded(
child: Container(
height: size.height,
child: Column(
children: <Widget>[
Container(
height: size.height * 0.25,
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
InkWell(onTap: likeOnTap,child: getIcons(Icons.heart, likes, 35.0),),
InkWell(onTap: commentOnTap,child: getIcons(Icons.chat_bubble, comments, 35.0),),
InkWell(onTap: shareOnTap,child: getIcons(Icons.reply, shares, 25.0),),
InkWell(onTap: viewOnTap,child: getIcons(Icons.views, popularity, 30.0),),
],
))
],
),
),
);
}
}
uj5u.com熱心網友回復:
你可以InkWell
在你的RightPanel
類中添加,如下所示:
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
InkWell(
onTap : onClick,
child: getIcons(Icons.heart, likes, 35.0),
),
getIcons(Icons.chat_bubble, comments, 35.0),
getIcons(Icons.reply, shares, 25.0),
getIcons(Icons.views, popularity, 30.0),
],
),
)
并onClick
像這樣通過建構式:
class RightPanel extends StatelessWidget {
final String likes;
final String comments;
final String shares;
final String views;
final String popularity;
final Function onClick;
const RightPanel({
Key ?key,
required this.likes,
required this.comments,
required this.shares,
required this.views,
required this.popularity,
required this.onClick,
}) : super(key: key);
并像這樣使用它:
child: RightPanel(
onClick:(){
print("click");
}
likes: "${widget.likes}",
comments: "${widget.comments}",
shares: "${widget.shares}",
views: "${widget.popularity}",
popularity: "${widget.popularity}",
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515997.html
標籤:扑镖颤振布局
上一篇:為什么有些類提供了一種使用常量建構式創建編譯時物件的方法,為什么有些不呢?這背后有什么原因嗎?
下一篇:當單擊按鈕小部件移動時