Code
import 'package:flutter/material.dart';
class ToastButton extends StatefulWidget {
const ToastButton({super.key});
@override
State<ToastButton> createState() => _ToastButtonState();
}
class _ToastButtonState extends State<ToastButton> {
bool _isPressed = false;
void _showToast(BuildContext context, String message) {
final scaffold = ScaffoldMessenger.of(context);
scaffold.showSnackBar(
SnackBar(
content: Text(message),
behavior: SnackBarBehavior.floating,
duration: const Duration(seconds: 3),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
margin: const EdgeInsets.all(16),
),
);
}
void _handleTap() {
_showToast(context, 'Get Started button clicked!');
// Add your navigation or other logic here
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTapDown: (_) => setState(() => _isPressed = true),
onTapUp: (_) {
setState(() => _isPressed = false);
_handleTap();
},
onTapCancel: () => setState(() => _isPressed = false),
child: AnimatedScale(
scale: _isPressed ? 0.97 : 1.0,
duration: const Duration(milliseconds: 100),
child: ElevatedButton(
onPressed: _handleTap,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF4F46E5),
foregroundColor: Colors.white,
elevation: 0,
padding: const EdgeInsets.symmetric(
horizontal: 24,
vertical: 14,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: const Text(
'Get Started',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
letterSpacing: 0.1,
),
),
),
),
);
}
}Preview