Laila Mattar
Laila Mattar

Reputation: 361

How to override Widget function in flutter

I have this Widget function :

static Widget mainButton(String title){
    return MaterialButton(
      textColor: Colors.white,
      splashColor: Colors.white54,
      elevation: 8.0,
      child: Container(
        width: 244,
        height: 66,
        decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage('assets/images/home_button_bg.png'),
              fit: BoxFit.cover),
        ),
        child: Center(
          child: Text(
            title,
            style: TextStyle(
              fontSize: 20.0,
            ),
          ),
        ),
      ),
      onPressed: (){
        print('pressed');
      },
    );
  }

and I want to write onPressed() function differently each time I want to call mainButton() function. how can I write it ?

Upvotes: 2

Views: 1140

Answers (2)

Ali Mohammadzadeh
Ali Mohammadzadeh

Reputation: 686

You can use Function for input of this method like code below:

static Widget mainButton(String title, Function function) {
    return MaterialButton(
        textColor: Colors.white,
        splashColor: Colors.white54,
        elevation: 8.0,
        child: Container(
          width: 244,
          height: 66,
          decoration: BoxDecoration(
          image: DecorationImage(
             image: AssetImage('assets/images/home_button_bg.png'),
             fit: BoxFit.cover),
              ),
          child: Center(
            child: Text(
              title,
              style: TextStyle(
                fontSize: 20.0,
              ),
            ),
          ),
        ),
        onPressed: function);
  }

Upvotes: 1

DJafari
DJafari

Reputation: 13545

Widget mainButton(String title, VoidCallback onPressed){
    return MaterialButton(
      textColor: Colors.white,
      splashColor: Colors.white54,
      elevation: 8.0,
      child: Container(
        width: 244,
        height: 66,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/home_button_bg.png'),
            fit: BoxFit.cover),
        ),
        child: Center(
          child: Text(
            title,
            style: TextStyle(
              fontSize: 20.0,
            ),
          ),
        ),
      ),
      onPressed: onPressed,
    );
  }

example :

mainButton("Test", () {
  print('Test');
});

Upvotes: 2

Related Questions