我正在嘗試與 Dart Flutter 建立介面。我做了一個這樣的界面:

我希望這些專案是可點擊的。點擊后,我會采取行動。我該怎么做才能點擊專案?
代碼:
body: Center(
child: GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: subjects.length,
itemBuilder: (BuildContext context, int index) {
return Container(
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(subjects[index].subjectImage, fit: BoxFit.cover, height: 50, width: 50,),
SizedBox(height: 10,),
Text(subjects[index].subjectName, style: TextStyle(fontSize: 20),),
],
),
),
);
},
),
)
uj5u.com熱心網友回復:
您可以為此使用 GestureDetector 或 Inkwell。并在 onTap() 中撰寫您的 onpress 操作代碼。
GestureDetector(
onTap: () {
// To do
},
child: Card(
),
),
要么
InkWell(
onTap: () {
// To do
},
child: Card(),
),
uj5u.com熱心網友回復:
您可以使用 GestureDetector 或 Inkwell 使用 onTap() 函式在顫動中按下您的任何小部件
GestureDetector(
onTap: () {
// Write your code here
},
child: Container(),
),
或使用墨水瓶
InkWell(
onTap: () {
// Write your code here
},
child: Container(),
),
uj5u.com熱心網友回復:
試試這個 - GestureDetector 或 InkWell 對您的情況很有用......
body: Center(
child: GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: subjects.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () {
// call click event
},
child: Container(
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(subjects[index].subjectImage, fit: BoxFit.cover, height: 50, width: 50,),
SizedBox(height: 10,),
Text(subjects[index].subjectName, style: TextStyle(fontSize: 20),),
],
),
),
),
);
},
),
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431591.html
