GithubHelp home page GithubHelp logo

Comments (3)

stargazing-dino avatar stargazing-dino commented on June 26, 2024

I see what you mean :\ . Yes, the tooltip will update for every change in the state of the ValueNotifier. I don't like that either. Flutter likes to promote data classes and immutability but they don't really offer a great way to drill into and listen on individual property changes..

My first idea was to change the controller from ValueNotifier<ControllerState> to rxDart's BehaviorSubject<ControllerState> and then I would create the value streams by mapping the events and calling distinct but I dunno. I don't see a lot of people turning BehaviorSubjects into controllers.

class JustTheController {
  JustTheController({ControllerState? state})
      : controller = BehaviorSubject<ControllerState>.seeded(
          state ?? ControllerState.empty(),
        );

  @protected
  final BehaviorSubject<ControllerState> controller;

  ControllerState get state => controller.value;

  // These could somehow be [ValueStream]s with the ability to get the last value too
  Stream<bool> get isShowing => controller.map((event) {
        return event.status == AnimationStatus.completed &&
            event.action == ControllerAction.none;
      }).distinct();

  Stream<bool> get isHidden => isShowing.map((event) => !event);

  Stream<bool> get isAnimating => controller.map(
        (event) {
          return event.status == AnimationStatus.forward ||
              event.status == AnimationStatus.reverse;
        },
      ).distinct();

  /// Shows the tooltip. Completes when the tooltip is fully visible.
  Future<void> showTooltip({bool immediately = false}) async {
    if (controller.value.action == ControllerAction.show) return;

    final completer = Completer<void>();

    controller.value = controller.value.copyWith(
      action: ControllerAction.show,
      completer: completer,
      immediately: immediately,
    );

    return completer.future;
  }

  /// Hides the tooltip. Completes when the tooltip is fully hidden.
  Future<void> hideTooltip({bool immediately = false}) async {
    if (controller.value.action == ControllerAction.hide) return;

    final completer = Completer<void>();

    controller.value = controller.value.copyWith(
      action: ControllerAction.hide,
      completer: completer,
      immediately: immediately,
    );

    return completer.future;
  }
}

I could also bring in a third party dep such as propertyChangeNotifier but I don't like relying on other packages that aren't defacto standard. I could as you do create ValueNotifier for one specific property but I feel like since I already have the concept of a controller, I'd rather everything be available inside it.

I'll think this over some more on the weekend but feel free to offer any more thoughts

from just_the_tooltip.

zmhbh avatar zmhbh commented on June 26, 2024

@Nolence Yep. I definitely understand. In my understanding, what we really want to expose are these three: 1. the open/close status of tooltip. 2. openTooltip() function 3. closeTooltip() function. So maybe we can refactor the controller and only put those 3 things. In addition, creating an inner private controller, which is not available to user. And put the intermediate states into it.

from just_the_tooltip.

stargazing-dino avatar stargazing-dino commented on June 26, 2024

Okay. So. After WAY too many hours on this problem trying everything I could and looking at just about every other controller out there I feel like I've got an okay solution. It's gonna blow your socks off.

I have show/hide function fields on the controller that I overwrite when the controller mounts to a tooltip. That's it.

class JustTheController extends ValueNotifier<TooltipStatus> {
  JustTheController({TooltipStatus? value})
      : super(value ?? TooltipStatus.hidden);

  @mustCallSuper
  void attach({
    required ShowTooltip showTooltip,
    required HideTooltip hideTooltip,
  }) {
    this.showTooltip = showTooltip;
    this.hideTooltip = hideTooltip;
  }

  late ShowTooltip showTooltip;

  late HideTooltip hideTooltip;
}

// You would listen to the open/close status of the tooltip by listening to this valuenotifier

It looks anti-pattern but I don't actually think it is for a couple reasons. One, it doesn't actually expose the state of the State<JustTheTooltip>. Another reason I think it's fine is that I got the idea from a FocusNode which does a very similar thing.

What about the hidden showing states? Well, those are actually incredi-easy cause I just set the state of the valueNotifier controller inside the real showTooltip. I'd say the only bad thing about this setup is that you can't directly follow or inspect the showTooltip functions.

Either way, I don't think it's a breaking change but let me know. Also let me know if you spot anything weird.

It's hidden behind 0.0.10-dev so you have to opt into it by putting that version in your pubspec.yaml

Edit

I definitely did try out your suggestions and PR too so thanks for those ! In the end I decided not to go with them for one reason or another. Was just really weird the way I had the controller initially

from just_the_tooltip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.