どうも、ちょげ(@chogetarou)です。
ウェジェットに長押しの処理を追加する方法を紹介します。
方法

ウェジェットを長押しできるようにするには、GestureDetectorもしくはInkWellを使います。
まず、ウェジェットをGestureDetectorもしくはInkWellでラップします。
そして、引数「onLongPress」に長押しで呼び出す関数を指定します。
GestureDetector( //InkWellでも同じ
onLongPress: (){/*長押しの処理*/},
child: Widget(),
),
GestureDetectorもしくはInkWellを使うことで、特定のウェジェットを長押しできるようにすることが出来ます。
使用例
以下は、使用例です。
bool _isBlue = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: InkWell(
onLongPress: () {
setState(() {
_isBlue = !_isBlue;
});
},
child: Container(
height: 100,
width: 300,
color: _isBlue ? Colors.blue : Colors.pink,
),
),
),
);
}
コメント