InheritedWidget继续调用重建操作,即使updateShouldNotify返回false吗?

时间:2019-12-28 03:27:50

标签: flutter widget inherited inherited-widget

我想问一下InheritedWidget,因为我对此仍然不了解

我刚刚使用StatelessWidget构造了一个InheritedWidget,然后将其放在StatefulWidget上。

当我在StatefulWidget中调用setState()时,StatelessWidget会继续调用重建方法,即使updateShouldNotify返回false。有人可以解释吗?

StatefulWidget代码:

@override
Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.black,
  body: SafeArea(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      mainAxisSize: MainAxisSize.max,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        SizedBox(width: double.infinity, height: 40,
          child: Stack(children: <Widget>[
            AlignPositioned(
              alignment: Alignment.centerLeft,
              child: IconButton(
                icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
                onPressed: (){
                  Navigator.pop(context);
                },
              ), 
            ),
            AlignPositioned(
              alignment: Alignment.center,
              child: Text("Now Playing", style: mayorTextStyle.copyWith(fontSize: 22), overflow: TextOverflow.ellipsis),
            ),
          ]
        )),
        BodyPlayerPageIH(widget.music, BodyPlayerPageUI()),
        FooterPlayerPageIH(playIcon, _playerState, widget.playlist != null && widget.playlist.list.length > 1, repeat, () => _pause(), () => _resume(), () => _repeat(), FooterPlayerPageUI()),
      ],
    )
  ),
);}

InheritedWidget代码:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.max,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            SizedBox(width: double.infinity, height: 40,
              child: Stack(children: <Widget>[
                AlignPositioned(
                  alignment: Alignment.centerLeft,
                  child: IconButton(
                    icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
                    onPressed: (){
                      Navigator.pop(context);
                    },
                  ), 
                ),
                AlignPositioned(
                  alignment: Alignment.center,
                  child: Text("Now Playing", style: mayorTextStyle.copyWith(fontSize: 22), overflow: TextOverflow.ellipsis),
                ),
              ]
            )),
            BodyPlayerPageIH(widget.music, BodyPlayerPageUI()),
            FooterPlayerPageIH(playIcon, _playerState, widget.playlist != null && widget.playlist.list.length > 1, repeat, () => _pause(), () => _resume(), () => _repeat(), FooterPlayerPageUI()),
          ],
        )
      ),
    );
  }

StatelessWidget代码:

class FooterPlayerPageUI extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    debugPrint("called");
    final FooterPlayerPageIH footer = FooterPlayerPageIH.of(context);

    final IconData playIcon = footer.playIcon;
    final PlayerState _playerState = footer._playerState;

    final bool isPlaylist = footer.isPlaylist;
    final bool isRepeat = footer.isRepeat;

    final void Function() _pause = footer.pause;
    final void Function() _resume = footer.resume;
    final void Function() _repeat = footer.repeat;

    return Align(
      alignment: FractionalOffset.bottomCenter,
      child: SizedBox(width: double.infinity, height: 90,
        child: Stack(children: <Widget>[
          AlignPositioned(
            alignment: Alignment.centerLeft,
            child: isPlaylist ? IconButton(
              icon: const Icon(Icons.shuffle, color: Colors.white),
              onPressed: (){

              },
            ) : SizedBox(),
          ),
          AlignPositioned(
            alignment: Alignment.center,
            child: SizedBox(width: 162, child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                IconButton(
                  icon: const Icon(Icons.fast_rewind, color: Colors.white),
                  onPressed: (){

                  },
                ),
                IconButton(
                  iconSize: 50,
                  icon: Icon(playIcon, size: 50,color: Colors.white),
                  onPressed: (){
                    if(_playerState == PlayerState.PLAYING) {
                      _pause();
                    } else {
                      _resume();
                    }
                  }
                ),
                IconButton(
                  icon: Icon(Icons.fast_forward, color: Colors.white),
                  onPressed: (){

                  },
                ),
              ]),
            ),
          ),
          AlignPositioned(
            alignment: Alignment.centerRight,
            child: IconButton(
              icon: Icon(Icons.repeat, color: isRepeat ? Colors.green : Colors.white),
              onPressed: (){
                _repeat();
              },
            ),
          ),
        ]),
      ),
    );
  }
}

0 个答案:

没有答案
相关问题