GestureDetector
sample.packages.flutter.lib.src.widgets.gesture_detector.142.
This example uses a [Container] that wraps a [GestureDetector] widget which detects a tap.
Since the [GestureDetector] does not have a child, it takes on the size of its
parent, making the entire area of the surrounding [Container] clickable. When
tapped, the [Container] turns yellow by setting the _color
field. When
tapped again, it goes back to white.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State {
Color _color = Colors.white;
@override
Widget build(BuildContext context) {
return Container(
color: _color,
height: 200.0,
width: 200.0,
child: GestureDetector(
onTap: () {
setState(() {
_color == Colors.yellow
? _color = Colors.white
: _color = Colors.yellow;
});
},
),
);
}
}
SHARE: