0

2 つのボタンを作成しましたが、共通のコンストラクター パラメーターがあります。二度と同じパラメータを書きたくありません。ミックスインまたはクラスからすべてのボタンのパラメーターを呼び出したい。以下の私のボタン:

  1. カスタム テキスト ボタン:

     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. 送信ボタン:

    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);
     }
    }
    

この問題を解決するために mixin を作成しました:

mixin ButtonFeatures {
 late final TextStyle style;
 late final Function() onPressed;
 late final ButtonStyle? buttonStyle;
 late final String title;
 late final Function() onLongPressed;
}

これは私のカスタムボタンの使用例です:

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,));
 }
}

ご覧のとおり、 title は string です。しかし、以下のようなボタンを使用してもエラーは発生しませんでした:

HTextButton(12, onPressed: "aaaaa",)

この問題を解決するにはどうすればよいですか?

4

1 に答える 1