本文介绍了如何使用SizeChangedLayoutNotifier?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的SliverAppBar中的flexibleSpace内有一个Wrap.现在,当Wrap的大小(动态更改)时,我需要获取通知,以便可以在SliverAppBar中相应地更改我的flexibleSpace的高度.

I got a Wrap inside my flexibleSpace in my SliverAppBar. Now when the size of my Wrap changes (dynamically) I need to get a notification, so I can change the height of my flexibleSpace in my SliverAppBar accordingly.

我将文档阅读到 SizeChangedLayoutNotifier ,但我没有真的不知道如何使用它.我将孩子的Wrap包裹在SizeChangedLayoutNotifier中,但对我来说,还不清楚如何使用NotificationListener<SizeChangedLayoutNotification>捕获通知.我找不到任何代码示例.

I read the docs to the SizeChangedLayoutNotifier but I don't really understand how to use it. I wrapped my Wrap as a child in a SizeChangedLayoutNotifier but for me it is very unclear how to catch the notification with the NotificationListener<SizeChangedLayoutNotification>. I couldn't find any code example.

非常感谢您的帮助:)谢谢!

I would really appreciate your help :) Thanks!

推荐答案

我终于弄清楚了,并将在这里将解决方案发布给其他有相同问题的人.

I finally figured it out and will post the solution here for others, having the same problem.

我这样将我的Wrap放在里面:

I put my Wrap inside like this:

new NotificationListener<SizeChangedLayoutNotification>(
   onNotification: gotNotification,
   child: SizeChangedLayoutNotifier(
       key: _filterBarChangeKey,
       child: Wrap( // my WRAP stuff)
   )
);

并进行如下回调:

bool gotNotification(SizeChangedLayoutNotification notification) {
    // change height here
    _filterBarChangeKey = GlobalKey();
}

我还从此处找到了另一种解决方案,根本没有使用SizeChangedLayoutNotification解决我的问题.对我来说,这甚至更好.我只是将我的小部件包装在提供onSizeChanged回调的MeaserSize小部件内.

I also found another solution from here not using SizeChangedLayoutNotification at all to solve my problem. For me this was even better. I just wrapped my Widget inside an MeaserSize widget which provides an onSizeChanged callback.

typedef void OnWidgetSizeChange(Size size);

class MeasureSize extends StatefulWidget {
  final Widget child;
  final OnWidgetSizeChange onChange;

  const MeasureSize({
    Key key,
    @required this.onChange,
    @required this.child,
  }) : super(key: key);

  @override
  _MeasureSizeState createState() => _MeasureSizeState();
}

class _MeasureSizeState extends State<MeasureSize> {
  var widgetKey = GlobalKey();

  @override
  Widget build(BuildContext context) {

    WidgetsBinding.instance
        .addPostFrameCallback((_) =>  widget.onChange(widgetKey.currentContext.size));

    return Container(
      key: widgetKey,
      child: widget.child,
    );
  }
}

这篇关于如何使用SizeChangedLayoutNotifier?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:46