starwork_flutter/lib/common/widgets/custome_app_bar_wdiget.dart

64 lines
1.8 KiB
Dart
Raw Normal View History

2025-09-12 15:25:36 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class CustomAppBarWidget extends StatelessWidget implements PreferredSizeWidget {
final String title;
final List<Widget>? actions;
final Widget? leading;
final bool centerTitle;
final Color? backgroundColor;
final Color? titleColor;
final Color? backIconColor;
2025-09-12 15:25:36 +08:00
final double? elevation;
2025-09-19 15:05:08 +08:00
final bool? showBackButton;
2025-09-12 15:25:36 +08:00
const CustomAppBarWidget({
super.key,
required this.title,
this.actions,
this.leading,
this.centerTitle = true,
this.backgroundColor = Colors.white,
this.titleColor = Colors.black,
this.backIconColor = Colors.black,
2025-09-12 15:25:36 +08:00
this.elevation,
2025-09-19 15:05:08 +08:00
this.showBackButton = true,
2025-09-12 15:25:36 +08:00
});
@override
Widget build(BuildContext context) {
// 判断是否需要显示返回按钮
bool showBackButton = ModalRoute.of(context)?.canPop ?? false;
return AppBar(
title: Text(
title,
style: TextStyle(
fontSize: 18.sp,
fontWeight: FontWeight.w500,
color: titleColor,
2025-09-12 15:25:36 +08:00
),
),
actions: actions,
2025-09-19 15:05:08 +08:00
automaticallyImplyLeading: this.showBackButton ?? true,
2025-09-12 15:25:36 +08:00
// 根据是否有自定义leading来决定使用什么
leading: leading ??
2025-09-19 15:05:08 +08:00
(showBackButton && this.showBackButton == true
2025-09-12 15:25:36 +08:00
? IconButton(
icon: Icon(
Icons.arrow_back_ios_new_rounded,
color: backIconColor,
),
2025-09-12 15:25:36 +08:00
onPressed: () => Navigator.of(context).pop(),
)
: null),
// 当有leading时标题默认左对齐否则按centerTitle参数决定
centerTitle: false,
backgroundColor: backgroundColor,
elevation: elevation,
);
}
@override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}