Comments (3)
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.
@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.
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)
- Get rid of JustTheInterface
- make tooltip not disappear when hovering over the actual tooltip widget
- isModal:true makes the tooltip persistant (on previous page) when a GestureDetector of higher level pushes the navigator to a new route...
- Tooltip doesn't show up, dry layout errors. HOT 2
- Prevent the tooltip from closing on clicking on it HOT 2
- onShow not triggering correctly
- Gesture Detector in other page wont work if the page is redirected from tooltip HOT 1
- isModal does not work, if child is gestureDetector
- Just showing a simple tooltip by default?
- Bad state: This controller has not been attached to a tooltip yet. HOT 5
- Tooltip with Outline Border HOT 1
- clickable content HOT 2
- uses-material-design: true is unnecessary HOT 1
- The size of the content is too big.
- cannot show in nested navigation
- LateInitializationError: Field 'axisDirection' has not been initialized.
- Allow to pass a overlay state to the constructor.
- Controller Issue HOT 3
- Tooltip location issue when page size isn't full width HOT 1
- auto close is not work HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from just_the_tooltip.