qhd-prevention-flutter/lib/pages/my_appbar.dart

48 lines
1.2 KiB
Dart
Raw Normal View History

2025-07-03 09:45:15 +08:00
import 'package:flutter/material.dart';
class MyAppbar extends StatelessWidget implements PreferredSizeWidget {
final String title;
final VoidCallback? onBackPressed;
final Color backgroundColor;
final Color textColor;
final List<Widget>? actions; // 👉 新增参数:右侧按钮
const MyAppbar({
Key? key,
required this.title,
this.onBackPressed,
this.backgroundColor = Colors.blue,
this.textColor = Colors.white,
this.actions,
}) : super(key: key);
@override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
@override
Widget build(BuildContext context) {
return AppBar(
backgroundColor: backgroundColor,
automaticallyImplyLeading: false,
centerTitle: true,
title: Text(
title,
style: TextStyle(
color: textColor,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
leading: _buildBackButton(context),
actions: actions, // 👉 设置右侧按钮
);
}
Widget _buildBackButton(BuildContext context) {
return IconButton(
icon: const Icon(Icons.arrow_back_ios, color: Colors.white, size: 20),
onPressed: onBackPressed ?? () => Navigator.of(context).pop(),
);
}
}