如何动态更改SliverAppBar的高度

时间:2018-10-03 08:19:45

标签: flutter

我显示了来自网络的图像,该图像在滚动时会缩小。我想显示整个图像而没有填充或裁切。但是,如果我用ExpandedHeight注释行-没有图像-只有带有其高度的appbar。有没有可以根据上传图片的大小更改其大小的小部件?

CustomScrollView(
  controller: controller,
  key: listKey,
  slivers: <Widget>[
    SliverAppBar(
    //  expandedHeight: 200.0,
      flexibleSpace: FlexibleSpaceBar(
        background: getHeroWidget(
            _conference.dbId,
            FadeInImage.assetNetwork(
              placeholder: conf_img_placeholder,
              image: _conference.info.image,
              fit: BoxFit.cover,
            )),
        title: Text(conference_title),
        centerTitle: true,
      ),
      pinned: true,
    ),

1 个答案:

答案 0 :(得分:0)

最后,我创建了自定义SliverAppBar。出现另一个问题-状态栏的宽度在计算时必须考虑在内。

import 'dart:ui' as ui;

class _HeaderBar extends StatefulWidget {    
  @override
  State<StatefulWidget> createState() => _HeaderState();
}

class _HeaderState extends State<_HeaderBar> {
  @override
  Widget build(BuildContext context) {
    Image image = Image.network(...);
    Completer<ui.Image> completer = new Completer<ui.Image>();
    image.image.resolve(ImageConfiguration()).addListener((ImageInfo info, bool _) {
      completer.complete(info.image);
    });
    final double statusBarHeight = MediaQuery.of(context).padding.top;
    return FutureBuilder(
        future: completer.future,
        builder: (context, AsyncSnapshot<ui.Image> snapshot) {
          return SliverAppBar(
                  expandedHeight: snapshot.hasData
                ? MediaQuery.of(context).size.width / snapshot.data.width.toDouble() * snapshot.data.height.toDouble() -
                    statusBarHeight
                : 0.0,
...