在 flutter 中添加到 AppBar 时,部分图像未显示

Part of image is not showing when added to AppBar in flutter

当我尝试将图像添加到 Flutter 中的 Appbar 时,如下所示:

appBar: PreferredSize(
        preferredSize: Size.fromHeight(100.0),
        child: AppBar(
          centerTitle: true,
          title: Image.asset('images/Ataxx.jpg'),
        ),
      )

我得到以下输出:

如您所见,图片顶部缺失了一部分!如何纠正这个?谢谢

首先,我认为没有必要使用 PreferredSizeWidget 因为 AppBar 本身已经实现了 PreferredSizeWidget.

此外,我使用 FittedBox 将图像放在应用栏中 - 请参阅示例代码了解如何使用它。

正如@rgisi 所建议的那样,你真的不需要使用 PreferredSize() 你只需要在标题中传递你的图像。

appBar: AppBar(
    titleSpacing: 0,
    elevation: 0,
    title: Image.network(
      'https://images-eu.ssl-images-amazon.com/images/I/71ZFcWRAX7L.png',
      fit: BoxFit.cover,
    ),
  ),

对于你的用例,如果你真的想使用 PreferredSize() 你可以像这样使用它。

appBar: PreferredSize(
    preferredSize: Size.fromHeight(100),
    child: Container(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Image.network(
          'https://images-eu.ssl-images-amazon.com/images/I/71ZFcWRAX7L.png',
          fit: BoxFit.cover,
        ),
      ),
    ),
  ),

你会得到这样的东西