|
百度搜到的写法:
//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);
}
|