| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- 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<Widget>? 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);
- }
|