创建一个通用的应用栏小部件类
enum ButtontType {
back,
menu
}
class topBarWidget {
static AppBar createTopBar(
{
String title,
double elevation = 1.5,
TextStyle titleStyle,
Widget titleWidget,
List<ButtontType> leftIcons = const [],
List<ButtontType> rightIcons = const [],
ButtonTapCallback action,
EdgeInsetsDirectional padding = const EdgeInsetsDirectional.only(start: 10, end: 10.0),
Color backgroundColor,
Color iconColor,
bool centerTitle = true}) {
var titleText = titleWidget;
if (titleText == null) {
titleText = Text(
title,
softWrap: true,
style: txtStyle,
),
);
}
var leftmenuWidget;
List<Widget> rightRowChildern = [];
final size = 18.0;
final tapAreasize = 32.0;
if (leftIcons.length > 0) {
List<Widget> mainRowChildern = [];
mainRowChildern.add(Padding(padding: EdgeInsets.only(left: 10.0)));
for (int i = 0; i < leftIcons.length; i++) {
final menuIconImage = Image.asset(_getImageName(type: leftIcons[i]),color: Colors.black,);
final menuIconImageSizeBox = SizedBox(child: menuIconImage, width: size, height: size,);
final menuIconAction = Container(color: Colors.transparent,child: InkWell(onTap: () {(action != null) ? action(leftIcons[i]) : null;},child: ClipOval(child: Container(alignment: Alignment.center,width: tapAreasize,height: tapAreasize,color: Colors.transparent,child:menuIconImageSizeBox,),),),);
mainRowChildern.add(menuIconAction);
}
leftmenuWidget = Row(children: mainRowChildern, mainAxisAlignment: MainAxisAlignment.start,);
}
if (rightIcons.length > 0) {
for (int i = 0; i < rightIcons.length; i++) {
Widget menuIconImage = Image.asset(_getImageName(type: rightIcons[i]),color: Colors.black,);
if(_getImageName(type: rightIcons[i]) == _getImageName(type: ButtontType.notificationUnread)){
menuIconImage = Image.asset(_getImageName(type: rightIcons[i]),);
} else{
menuIconImage = Image.asset(_getImageName(type: rightIcons[i]),color: Colors.yellow,);
}
var menuIconImageSizeBox;
menuIconImageSizeBox = SizedBox(child: menuIconImage, width: size * 1.5, height: size * 1.5,);
final menuIconAction = InkWell(child: menuIconImageSizeBox, onTap: () => (action != null) ? action(rightIcons[i]) : null,);
rightRowChildern.add(menuIconAction);
if (i != (rightIcons.length - 1)) {
rightRowChildern.add(Padding(padding: EdgeInsets.only(right: 12.0)));
}
}
rightRowChildern.add(Padding(padding: EdgeInsets.only(right: 16.0)));
rightRowChildern = [
Row(
children: rightRowChildern,
)
];
}
var topBar = AppBar(
elevation: elevation,
brightness: Brightness.light,
backgroundColor: backgroundColor ?? Colors.white,
leading: leftmenuWidget ?? SizedBox(),
actions: rightRowChildern,
title: titleText,
centerTitle: centerTitle,
titleSpacing: 0,
);
return topBar;
}
static String _getImageName({ButtontType type}) {
var iconName;
if (type == ButtontType.back) {
iconName = 'images/back.png';
} else if (type == ButtontType.menu) {
iconName = 'images/menu.png';
}
return iconName;
}
}
现在是关于
如何使用的示例。
final appBar = topBarWidget.createTopBar(
titleStyle: txtStyle,
title: "App Bar",
leftIcons: [ButtontType.back],
backgroundColor: Colors.white,
action: (ButtontType type) {
Navigator.of(context).pop();
});
var scaffold = Scaffold(
appBar: appBar,
body: scaffoldBuilder,
);
希望这可以帮到您!
onPressed
属性设置为null
,它将被禁用并呈禁用样式(您可以尝试使用基本的RaisedButton
)。我个人在使用 UI 中的每个按钮时首先编写onPressed: (){}
。 - Augustin R