I created two buttons but they have common constructor parameters. I don't want to write same parameters again. I want to call all buttons' parameters from mixin or class. My buttons below:
Custom text button:
class HTextButton extends StatelessWidget { final TextStyle style; final Function() onPressed; final ButtonStyle? buttonStyle; final String title; const HTextButton(this.title,{Key? key, required this.onPressed, required this.style, this.buttonStyle}) : super(key: key); @override Widget build(BuildContext context) { return TextButton( style: buttonStyle, onPressed: onPressed, child: Text(title,style: style,)); } }
Submit button:
class SubmitButton extends StatelessWidget { final TextStyle style; final Function()? onPressed; final Function()? onLongPressed; final ButtonStyle buttonStyle; final String title; const SubmitButton(this.title, {Key? key, required this.onPressed, required this.onLongPressed, required this.style, required this.buttonStyle}) : super(key: key); @override Widget build(BuildContext context) { return ElevatedButton( style: buttonStyle, child: Text(title, style: style), onPressed: onPressed, onLongPress: onLongPressed); } }
I have created mixin to solve this problem :
mixin ButtonFeatures {
late final TextStyle style;
late final Function() onPressed;
late final ButtonStyle? buttonStyle;
late final String title;
late final Function() onLongPressed;
}
This is sample usage of my custom button :
class HTextButton extends StatelessWidget with ButtonFeatures{
HTextButton(title,{Key? key, required onPressed, required style, buttonStyle}) : super(key: key);
@override
Widget build(BuildContext context) {
return TextButton(
style: buttonStyle,
onPressed: onPressed, child: Text(title,style: style,));
}
}
As you see, title is a string . But I didn't get any error when I used button like below:
HTextButton(12, onPressed: "aaaaa",)
How can I solve this problem ?