flutter标题栏Appbar的封装(最简调用:BackAppBar(context,title))

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-21 12:19   82   0

百度搜到的写法:

//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);
}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP