Flutter GridViewの要素をタップできるようにするサンプル

環境
Windows11 pro 64bit
Flutter 3.3.7

構文
GestureDetector(
onTap: (){
//処理コード
},
GridViewの要素をタップできるようにするには、GestureDetectorを使います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
GridView.count(
crossAxisCount: /*Count*/,
children: <Widget>[
GestureDetector(
onTap: (){
//処理コード
},
child: Widget(),
),
xxx
],
),
GridView.count( crossAxisCount: /*Count*/, children: <Widget>[ GestureDetector( onTap: (){ //処理コード }, child: Widget(), ), xxx ], ),
GridView.count(
  crossAxisCount: /*Count*/,
  children: <Widget>[
    GestureDetector( 
      onTap: (){
        //処理コード
      },
      child: Widget(),
    ),
    xxx
  ],
),

説明
GridViewの要素をGestureDetectorでラップします。
GestureDetectorの引数「onTap」に、要素をタップで呼び出す関数を指定します。

Flutter

Posted by arkgame