How to override Widget function in flutter

274

Solution 1

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

Solution 2

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);
  }
Share:
274
Laila Mattar
Author by

Laila Mattar

Updated on December 07, 2022

Comments

  • Laila Mattar
    Laila Mattar over 1 year

    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 ?