Skip to content

Instantly share code, notes, and snippets.

@RyouMon
Last active November 6, 2022 11:42
Show Gist options
  • Select an option

  • Save RyouMon/6f9212e439f7c64a2f34cca9cd18fb2b to your computer and use it in GitHub Desktop.

Select an option

Save RyouMon/6f9212e439f7c64a2f34cca9cd18fb2b to your computer and use it in GitHub Desktop.
Manage self states
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Tap Box A',
home: Tapbox(),
);
}
}
class Tapbox extends StatefulWidget {
const Tapbox({super.key});
@override
State<Tapbox> createState() => _TapboxState();
}
class _TapboxState extends State<Tapbox> {
bool _active = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _handleTap,
child: Container(
width: 200.0,
height: 200.0,
decoration: BoxDecoration(
color: _active ? Colors.lightGreen[700] : Colors.grey[600]),
child: Center(
child: Text(
_active ? 'Active' : 'Inactive',
style: const TextStyle(fontSize: 32.0, color: Colors.white),
),
),
),
);
}
void _handleTap() {
setState(() {
_active = !_active;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment