在 Flutter 中,您可以通过定义一个扩展内置AppBar类的新类来使用自定义 AppBar。下面是如何在 Flutter 中创建自定义 AppBar 的示例:
import 'package:flutter/material.dart';
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
final String title;
final Color backgroundColor;
final List<Widget> actions;
CustomAppBar({required this.title, required this.backgroundColor, required this.actions});
@override
Widget build(BuildContext context) {
return AppBar(
title: Text(title),
backgroundColor: backgroundColor,
actions: actions,
);
}
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
在此示例中,我们创建了一个新类CustomAppBar,它扩展AppBar并实现了PreferredSizeWidget. 我还定义了一些自定义属性,例如title、backgroundColor,并且actions可以在创建自定义 AppBar 的新实例时传入。
要在您的 Flutter 应用程序中使用此自定义 AppBar,您只需创建该类的新实例CustomAppBar并传入所需的属性即可。这是一个例子: