0

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 ?

emowise
  • 121
  • 1
  • 14

1 Answers1

0

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...
  ),
Gazihan Alankus
  • 11,256
  • 7
  • 46
  • 57
  • Actually I want to call constructor parameters from outside of the widget. If I give a type to my constructor parameters, It is like to define parameters in the widget. Is it possible to use constructor parameters from outside of the widget without using defining types? – emowise Mar 02 '22 at 07:05
  • I edited my answer to show how you can use it. I'm not sure what it is exactly that you want. The user of this widget and this widget has to agree on a type for it to work well. Maybe if you show some code about how you want to use it I can help. – Gazihan Alankus Mar 02 '22 at 07:08