import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; class CustomAppBar extends StatelessWidget implements PreferredSizeWidget { final String title; final bool showBackButton; final List? actions; final VoidCallback? onBack; const CustomAppBar({ super.key, required this.title, this.showBackButton = true, this.actions, this.onBack, }); @override Widget build(BuildContext context) { return AppBar( title: Text(title), centerTitle: true, leading: showBackButton ? IconButton( icon: const Icon(Icons.arrow_back), onPressed: () { if (onBack != null) { onBack!(); } else if (context.canPop()) { // 如果可以返回,则返回上一页 context.pop(); } else { // 如果无法返回,则导航到首页 context.go('/'); } }, ) : null, actions: actions, ); } @override Size get preferredSize => const Size.fromHeight(kToolbarHeight); }