繁体   English   中英

如何在flutter中覆盖Widget功能

[英]How to override Widget function in flutter

我有这个小部件功能:

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

我想写onPressed()我想打电话给每次不同的功能mainButton()函数。 我该怎么写?

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

例子 :

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

您可以使用 Function 输入此方法,如以下代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM