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;
|
2025-09-20 16:03:20 +08:00
|
|
|
|
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,
|
2025-09-20 16:03:20 +08:00
|
|
|
|
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,
|
2025-09-20 16:03:20 +08:00
|
|
|
|
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(
|
2025-09-20 16:03:20 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|