百度搜到的写法:

//example of use: CommonAppBar().backBar(context,titleName)
class CommonAppBar {
   backBar(
      {BuildContext context, String titleName, bool isCenterTitle = true}) {
    return AppBar(
      title: Text(titleName),
      leading: IconButton(
          icon: Icon(
            Icons.arrow_back,
            color: Colors.white,
          ),
          onPressed: () => Navigator.pop(context)),
      centerTitle: isCenterTitle,
    );
  }
}

可以将 barkBar方法用static修饰,调用能省下一个括号,调用为 :

CommonAppBar.backBar(context,titleName)

个人觉得这样还是不是最简洁的,最终写法:

//example of use: BackAppBar(context,titleName)
class BackAppBar extends AppBar {
  BackAppBar(BuildContext context, String titleName,
      {bool isCenterTitle = true})
      : super(
            title: Text(titleName),
            leading: IconButton(
                icon: Icon(
                  Icons.arrow_back,
                  color: Colors.white,
                ),
                onPressed: () => Navigator.pop(context)),
            centerTitle: isCenterTitle);
}
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐