import 'package:flutter/material.dart';
class IconButtonDemo extends StatefulWidget {
@override
State<IconButtonDemo> createState() => _IconButtonDemoState();
}
class _IconButtonDemoState extends State<IconButtonDemo> {
String message = 'Press the icon';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('IconButton Demo'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(Icons.favorite),
color: Colors.red,
iconSize: 48.0,
onPressed: () {
setState(() {
message = 'You pressed the heart!';
});
},
tooltip: 'Favorite',
),
SizedBox(height: 16),
Text(
message,
style: TextStyle(fontSize: 18),
),
],
),
),
);
}
}We use a StatefulWidget to keep track of the message text. The IconButton shows a heart icon in red color and a bigger size for visibility. When the user taps the IconButton, the onPressed callback runs, updating the message state to 'You pressed the heart!'. The Text widget below shows the current message. The UI is centered vertically and horizontally using Center and Column with mainAxisSize.min to keep content compact.