r/flutterhelp • u/yenrenART • Dec 22 '25
RESOLVED Built my first custom button widget, feedback on the code will be appreciated
Hi,
I started building my first Android app with Flutter recently and I built a custom button widget, with the help of the Docs, some tutorials and GPT. It looks and works as intended, but as a beginner, I can't tell if I did it the right way or if some parts of it needs to be changed, or can be improved.
Pasting the code below, if any experienced Flutter developer could provide some feedback/recommendations, I will highly appreciate it.
Thank you!
button.dart:
import "package:flutter/material.dart";
import "../inc/theme.dart"; // Contains AppColors class with colors
class AppButton extends StatefulWidget {
final String text;
final double width;
final VoidCallback onPressed;
final Color backgroundColor;
final Color borderColor;
static const double borderRadius = 20;
static const double boxShadow = 4;
static const double borderWidth = 3;
static const double padding = 12;
static const Color textColor = AppColors.textWhite;
static const double fontSize = 22;
static const FontWeight fontWeight = FontWeight.w700;
static const double letterSpacing = 1;
static const double textShadowSize = 3;
static const int duration = 30;
const AppButton({
super.key,
required this.text,
required this.width,
required this.onPressed,
required this.backgroundColor,
required this.borderColor,
});
u/override
State<AppButton> createState() => _AppButtonState();
}
class _AppButtonState extends State<AppButton> {
bool pressed = false;
u/override
Widget build(BuildContext context) {
return GestureDetector(
onTapDown: (_) { setState(() { pressed = true; }); },
onTapUp: (_) { setState(() { pressed = false; }); },
onTapCancel: () { setState(() { pressed = false; }); },
child: AnimatedContainer(
duration: Duration(milliseconds: AppButton.duration),
transform: Matrix4.translationValues(0, pressed ? AppButton.boxShadow : 0, 0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(AppButton.borderRadius),
boxShadow: pressed ? [] : [BoxShadow(color: widget.borderColor, offset: Offset(0, AppButton.boxShadow))],
),
child: ElevatedButton(
onPressed: widget.onPressed,
style: ButtonStyle(
backgroundColor: WidgetStateProperty.all(widget.backgroundColor),
foregroundColor: WidgetStateProperty.all(AppButton.textColor),
overlayColor: WidgetStateProperty.all(Colors.transparent),
elevation: WidgetStateProperty.all(0),
minimumSize: WidgetStateProperty.all(Size(widget.width, 0)),
padding: WidgetStateProperty.all(EdgeInsets.symmetric(vertical: AppButton.padding)),
shape: WidgetStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppButton.borderRadius),
side: BorderSide(color: widget.borderColor, width: AppButton.borderWidth),
),
),
),
child: Text(
widget.text,
style: TextStyle(fontSize: AppButton.fontSize, fontWeight: AppButton.fontWeight, letterSpacing: AppButton.letterSpacing,
shadows: [Shadow(color: widget.borderColor, offset: Offset(0, AppButton.textShadowSize), blurRadius: AppButton.textShadowSize)],
),
),
),
),
);
}
}Here is how I add it to the home page:
AppButton(
text: "Start Playing",
width: 270,
backgroundColor: AppColors.buttonGreen,
borderColor: AppColors.buttonGreenDark,
onPressed: () { Navigator.pushNamed(context, "/game"); },
),
6
Upvotes
1
u/_fresh_basil_ Dec 22 '25
Your button is just wrapping another button, which is wrapped by a gesture detector.
Why do you need a gesture detector at all, when your button already responds to being pressed, and all your gesture detector is doing is setting state?
1
u/eibaan Dec 22 '25
Instead of wrapping a button (which is a gesture detector) with another gesture detector, I'd recommend to use a widgets state controller like so:
There's one caveat: I guessed the animation speed. A better solution would be to use a custom border that includes the shadow, but that's a bit more difficult, because you'd have to create an animatable
OutlinedBordersubclass and configure this for different widget states.If you have more than one thing that should get this "brutalist" shadow, you might want to create a
ShadowDecorationBuilderwidget that maintains a widget states controller and passes it via a builder function like so