Flutter 中的自定义 AppBar

在 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并传入所需的属性即可。这是一个例子:

猜你喜欢

转载自blog.csdn.net/iCloudEnd/article/details/130089443
今日推荐