繁体   English   中英

在 StreamBuidler 中使用 ViewModelWidget?

[英]Use a ViewModelWidget inside a StreamBuidler?

你好呀

我正在尝试在 StreamBuilder 中使用 ViewModelWidget(已在 ViewModelWidget 中使用)。 当我使用 StatelessWidget 并在参数中传递 viewModel 时,它工作正常。 但是当我使用 ViewModelWidget 时,我的打印方法总是返回 false 并且 animation 不起作用,即使我的 viewModel 似乎获取并返回了良好的数据。

我做错什么了吗? 还是不可能像这样实现它?

class HoverButton extends ViewModelWidget<AccessDialogViewModel> {
  const HoverButton({Key? key, required this.textKey}) : super(key: key);

  final String textKey;

  @override
  Widget build(BuildContext context, AccessDialogViewModel viewModel) {
    return StreamBuilder(
        stream: viewModel.isHovered,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            ButtonOnHover buttonHovered = snapshot.data as ButtonOnHover;
            if (buttonHovered.key == textKey) {
              return AlertButton(
                buttonOnHover: ButtonOnHover(
                    key: textKey, isHovered: buttonHovered.isHovered),
              );
            } else {
              return AlertButton(
                buttonOnHover: ButtonOnHover(key: textKey, isHovered: false),
              );
            }
          } else {
            return AlertButton(
                buttonOnHover: ButtonOnHover(key: textKey, isHovered: false),
           );
          }
        });
  }

class AlertButton extends ViewModelWidget<AccessDialogViewModel> {
  const AlertButton(
      {Key? key, required this.buttonOnHover})
      : super(key: key);

  final ButtonOnHover buttonOnHover;

  @override
  Widget build(BuildContext context, AccessDialogViewModel viewModel) {
    return MouseRegion(
      onEnter: (e) {
        _sendDataToViewModel(
            viewModel, ButtonOnHover(key: buttonOnHover.key, isHovered: true));
        print(buttonOnHover.isHovered); //==> always returning false
      },
      onExit: (e) {
        _sendDataToViewModel(
            viewModel, ButtonOnHover(key: buttonOnHover.key, isHovered: false));
        print(buttonOnHover.isHovered); //==> always returning false
      },
      child: Padding(
        padding: const EdgeInsets.all(5.0),
        child: Container(
          height: 40,
          width: 100,
          color: buttonOnHover.isHovered
              ? Theme.of(context).primaryColorLight
              : Colors.white,
          padding: const EdgeInsets.all(8),
          child: Center(
            child: Text(buttonOnHover.key, TextStyle(fontSize: 16, color: Theme.of(context).primaryColor)),
          ),
        ),
      ),
    );
  }

非常感谢你的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM