Like I said in the title, I would like to simulate a keyup event in Dart. The problem is that I have not found how create a new KeyboardEvent object.
The only way that I've found is to use the Event(String type) constructor and then dispatch it on window object. But that doesn't work because of my "keyup" handler who takes a KeyboardEvent in parameter. Example:
window.on.keyUp.add((KeyboardEvent e){
print('keyUp handler');
});
KeyboardEvent e = new Event("KeyboardEvent");
window.on.keyUp.dispatch(e);
Is there a way to create and dispatch a KeyBoardEvent to simulate a "keyup" in Dart?
Also, I should mention that I tried too to trigger the event in JavaScript, thanks to js-interop library, but it only fires the JS handlers.
Try the following code:
window.on.keyUp.add((Event event){
print('keyUp handler');
if(event is KeyboardEvent)
{
KeyboardEvent keyEvent = event as KeyboardEvent;
//do stuff with keyEvent if needed
}
});
window.on.keyUp.dispatch(new Event("keyup"));
I think the parameter on the Event constructor must be the type of the event. Also, you can check if the event is a keyboardEvent, so you can handle KeyIdentifier, for example.
It's possible now to Dispatch a KeyBoardEvent from window, please see discussion on Google Group for more information: https://groups.google.com/a/dartlang.org/d/topic/misc/mgnd1TUGn68/discussion
Related
Taking this example from the doc:
Stream<List<Hero>> heroes;
// ยทยทยท
void ngOnInit() async {
heroes = _searchTerms.stream
.transform(debounce(Duration(milliseconds: 300)))
.distinct()
.transform(switchMap((term) => term.isEmpty
? Stream<List<Hero>>.fromIterable([<Hero>[]])
: _heroSearchService.search(term).asStream()))
.handleError((e) {
print(e); // for demo purposes only
});
}
Say I want to "trigger" the stream at ngOnInit().
After some tests, I find this can be done by calling void search(String term) => _searchTerms.add(term); just after this:
await Future.delayed(Duration(milliseconds: 1));
Seems that the _searchTerms call inside ngOnInit() is not await.
Could anyone explain why this works that way, or what I am doing wrong?
It isn't exactly clear what you are trying to do here, but some background that might help.
Angular lifecycle methods can't be interrupted. They also will not wait for async actions. They are simply callbacks that will be called at the point of the angular lifecycle. Asking angular to wait for you to do work at some unknown point to it is not possible. What if the action never completed? What happens to the rest of the subtree? You would be in some weird state that would invalidate much of the logic and the app.
Instead we use change detection/variables to change the state of the component to handle these async actions. So you could show a progress indicator using a boolean variable until the rpc comes back and then show the results by flipping the variable.
I have a basic implementation of a custom render I will be using for handling Long Press.. It's all really based from this code http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/
In my "GestureContainerView" I have an event that I called "OnHeldDown",
How do I raise this "OnHeldDown" event if (in my Android) detected a "LongPress" ?
I tried looking up in google but couldn't find an example.
----------- UPDATE ------- (found a solution)
in PCL
in Android
Just create a method which checks if anyone is subscribes to the event handler and invoke it, if anyone is.
For example, create a methode like this:
private void RaiseOnHeldDown()
{
if (OnHeldDown != null)
OnHeldDown(this, EventArgs.Empty);
// Or even do the null propagation way
// OnHeldDown?.Invoke(this, EventArgs.Empty);
}
Of course if you'd like you can supply EventArgs.
Now in the event where you detect the LongPress you just call this method.
I have a dart function:
_addSelection(HtmlElement ele){
ele.classes.add("selection");
}
I would either want 1 or 2 things to occur, either A) execute an on-tap and on-track function given the selection class.... OR Dynamically add the on-tap and on-track attributes referencing reflected dart functions.
I have 2 functions:
#reflectable
onTap(CustomEventWrapper cew, params){
//...
}
#reflectable
onTrack(CustomEventWrapper cew, params){
//...
}
I was looking at the HtmlElement class and documentation and I wasnt quite understanding how to do this.
Edit if I were using jQuery and Javascript, I would be doing something as simple as:
$(document).on("tap", function(){});
$(document).on("track", function(){});
Edit2 Added Angular Dart because both designs leverage Dart backend and markup front end.
You could do:
_addSelection(HtmlElement ele) {
ele.classes.add("selection");
ele.onTouchEnd.listen((TouchEvent touch) {
...
});
}
That would give you something close to the tap event. If you wanted to get really fancy you could also listen for onTouchStart and only call your onTap handler when the time between the start and end is small.
I'm very new to Dart, and I'm encountering a problem with this code:
DivElement badge = querySelector('.badge');
badge.onClick.listen(onBadgeClick);
The event handler looks like this:
void onBadgeClick(MouseEvent e){
print(e.relatedTarget);
}
I get this exception
Exception: Unsupported operation: Cannot call matchingTarget if this
Event did not arise as a result of event delegation.
How can I get the element that the click is associated with?
e.target should you give the element that created the event. If you set a breakpoint in DartEditor the code execution halts on the line with the breakpoint and you can investigate the properties of the e instance.
I made a custom component which basically wraps a d3 line chart. Now I want to be able to register a callback for clicks on the lines in the chart.
I gave the component a #NgCallback parameter, which I then send events to:
class NetworkSummaryComponent implements NgShadowRootAware {
#NgCallback('callback')
Function callback;
void onShadowRoot(ShadowRoot shadowRoot) {
...
chart.callMethod('listen', ['line-click', (ev) {
var name = ev.callMethod('getLineName');
print(name);
callback({'name': name});
}]);
}
}
When using the component, I specify a function of my controller as callback:
<network-summary
...
callback="ctrl.lineClicked">
</network-summary>
However, that function is never actually called, put I know the callback arrives from the JS side because the print in the first snippet is executed.
If I instead specify the attribute as callback="ctrl.lineClicked()" I get a strange exception:
Closure call with mismatched arguments: function 'call'
I could not find any official documentation on how to properly do callbacks, so I'm not exactly sure what I'm doing wrong.. Any ideas?
It turns out that I had to explicitly name the expected arguments in the attributes:
<network-summary
...
callback="ctrl.lineClicked(name)">
</network-summary>
Hope this is useful to the next person having this problem.