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

时间:2021-06-05 15:49:04

标签: image flutter flutter-appbar

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

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

我得到以下输出:

enter image description here

如您所见,顶部缺少部分图像!如何纠正这个?谢谢。

2 个答案:

答案 0 :(得分:1)

正如@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,
        ),
      ),
    ),
  ),

你会得到这样的东西

enter image description here

答案 1 :(得分:0)

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

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