'Flutter same constructor parameters for different widgets

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:

  1. 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,));
        }
        }
    
  2. 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 ?



Solution 1:[1]

Here's the problem: your constructor argument does not have a type (hence is dynamic) and is not being assigned to the field that comes from the mixin.

Here is one way to fix it:

class HTextButton extends StatelessWidget with ButtonFeatures{

  HTextButton(title,{Key? key, required Function() onPressed, required style, buttonStyle}) : super(key: key) {
    this.onPressed = onPressed;
  }

Since it's in a mixin we cannot just say required this.onPressed and thanks to it being late we are allowed to assign to it in the constructor body.

Later you can use it like this:

  child: HTextButton(onPressed: () => print('hello!'), 
    // other parameters...
  ),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1