flutter - Flutter 中带有图像背景的普通 Appbar

标签 flutter dart appbar

我是新手。如果这个问题很奇怪,请不要介意。我需要 AppBar 中的背景图片。我在互联网上找到了一个小部件 SliverAppBar 的解决方案。我需要正常显示的图像背景图像。我发现 AppBar 中没有图像或背景图像的属性。谁能帮我这个?
谢谢!达山。

最佳答案

欢迎使用 stackoverflow。
为了理解,我之前做了这个演示。请遵循以下示例代码。

import 'package:flutter/material.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: AppBarWithImage(),
    );
  }
}

class AppBarWithImage extends StatefulWidget {
  @override
  _AppBarWithImageState createState() => _AppBarWithImageState();
}

class _AppBarWithImageState extends State<AppBarWithImage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('App Bar with image background'),
        flexibleSpace: Image(
          image: NetworkImage(
            "https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350",
          ),
          fit: BoxFit.cover,
        ),
        backgroundColor: Colors.transparent,
      ),
      body: Center(
        child: Text("Sample Text"),
      ),
    );
  }
}

输出:
enter image description here

结论
名为“flexibleSpace”的属性就是您要查找的背景图像。

关于flutter - Flutter 中带有图像背景的普通 Appbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61634386/

相关文章:

flutter - 如何在 flutter 中让 Appbar 的 IconButton 居中

dart - 尝试在 https ://pub. dartlang.org 上查找软件包 cupertino_icons 时出现 TLS 错误

firebase - Flutter-StreamBuilder不会更新UI

go - 无法将 websocket 数据从 flutter 应用程序发送到服务器

flutter - 我想在 Flutter 中将我的行分成 1/4 的比例

dart - 如何在 Flutter/Dart 中将参数从命令行传递到 main?

flutter - 如何在 Flutter 的文本输入字段中配置自动大写行为?

firebase - 为什么在onPressed时此AlertDialog不起作用?

windows-phone - 使用 Universal App 中的资源中的 FontIcon 时出现 XamlParseException

android - 如何在 Flutter 中将我的 AppBar 放在一个单独的文件中,同时仍然显示小部件?