Call a function when user press a Key from Keyboard in Flutter - flutter

how can I call a function when the user presses a key from the keyboard ?
This is for Desktop development .

You can do it using Actions and shortcuts in flutter. These widgets work something like this :
To get a more detailed information, please refer to the flutter docs here. These come under Advanced UI concepts.

I imagine you're using a TextField? It has the following property onChanged: ((newText) => {})
You can go ahead and use it like this:
onChanged: ((newText) => { yourFunction; }),
or
onChanged: ((newText) => { setState((){yourFunction;}); }),
Just to clarify, this will only update when the text changes. #Divyam Dhadwal seems to have a wild answer that might be more useful
Good luck my man! xx

Related

Flutter Global Keyboard Shortcuts

I am writing an application that should support searching some content. For this I implemented a search TextField. Now I would like to focus said TextField when the key combination ctrl + F is pressed.
In order to implement this, I wrapped my view in both a Shortcuts and Actions component as follows:
return Scaffold(
body: Shortcuts(
shortcuts: {
LogicalKeySet(LogicalKeyboardKey.keyF, LogicalKeyboardKey.control): MTCSearchIntent(),
},
child: Actions(
actions: {
MTCSearchIntent: MTCFocusAction(null),
},
child: ...
),
),
);
This implementation works if the search bar is already focused, but that kind of defeats the purpose of focusing it.. So I was wondering, how can I apply the Shortcut listener to the entire screen? Or asked differently, what determines the scope of the interactions, the Shortcut component, the Action component, something related to the FocusNode?
The example on flutter.dev don't seem to work globally either. In fact they don't seem to work at all for me.
I reproduced the example using a minimal example here: https://dartpad.dev/?id=55902a179be137d9025ea02170d749a7
When the TextField is focused, the ArrowUpand ArrowDown actions work as intended. However, if any other part of the application is highlighted, the Intent is never triggered.

Flutter web update url/route base on state

I'm new in flutter and I just want to know if this scenario/feature is possible in flutter/flutter web. I want to update the url/route base on the state of the screen. For example, in 9gag.com, there are buttons for Hot, Trending, and Fresh. Then, if I clicked the Hot button its url will be updated to 9gag.com/hot and also its contents, the same goes for Trending and Fresh buttons. Is this possible in flutter/flutter web? If so, how do I implement this feature?(much better if the implementation uses bloc)
Totally possible and is very easy to achieve just use url_launcher Package on onpressed/ontap
Example: Add this code somewhere like in the beginning of your stateful management or at the end of your code
_launchURL() async {
const url = 'https://flutter.io';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Call it somewhere like this
RaisedButton(
onPressed: _launchURL,
child: new Text('Show Flutter homepage'),
),
),
Now if you have some 9gag/hot button or container whatever you are going with just put that url and when user taps it, it will open that url. If you want to open it inside the app webview that's also possible but slightly different.
If I understood correctly It's pretty straightforward use the default way of navigation and routing.
I'm not sure with your approach like you have mentioned
I want to update the url/route base on the state of the screen
and
Then, if I clicked the Hot button its url will be updated to 9gag.com/hot and also its contents
If I'm not wrong there is a inbuilt feature of navigating to other screens and once you land on the desired screen, you can see the refreshed content. If you want to try material design the code will be like this
//within your stateful or stateless widget
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => YourPage()),
Instead of using a package you can try this inbuilt way of doing the same job. But it's upto you which one you want to use. Checkout the official flutter documentation https://flutter.dev/docs/cookbook/navigation
I hope this approach also helps to answer your question. If so happy to help in your flutter journey, if not share that how did you solved it.

Does anyone know how to make an http request in flutter?

I need to make an app in which the user needs to press an InkWell or a button and then it should take them to a website, so does anyone know how to do this?
There is a really nice plugin for this, called url_launcher. Usage is as follows:
InkWell(
onTap: () => launch(websiteUrl),
...
)
You can also use canLaunch to test if launching will work. More on that in README.md.

LWUIT menuBar refreshTheme not working?

I'd like to dynamically change the text of a Command depending on some state, so normally I went to Google and LWUIT blogs said that using refreshTheme() on MenuBar should do the trick.
So I used the following code, but it sadly didn't work
if (isPlaying) {
playButton.setCommandName("Pause");
}else{
playButton.setCommandName("Play");
}
this.getMenuBar().refreshTheme();
Is there something wrong with my code? Or did I misunderstand something?
It won't refresh. The text of the button is set when the command is placed so you can't do that.
You will need to use removeCommand(cmd) followed by addCommand(newCmd).
Furthermore, refreshTheme() has absolutely nothing to do with anything.

jqueryui autocomplete doesn't show dropdown for same input

I found that when you enter the same term into the jQuery autocomplete the second time the dropdown doesn't appear. Does anybody know how to fix this problem?
IF you write again then it will show the dropdown but if you select from form auto suggest then it will not as because it will see no diference between previous and current.
After the autocomplete configuration, I'm trying to use something like
$("#myTextBox").autcomplete(...);
$("#myTextBox").keypress(
function () {
var term = $("#myTextBox").val();
if (term && term.length >= $("#myTextBox").autocomplete("option", "minLength"))
$("#myTextBox").autocomplete('search');
});
But is working a bit slow (multiple calls to search), maybe I should implement a time delay, but I think is too much work for a simple workaround and my lack of jquery knowledge
In jQuery UI 1.8.19 is fixed http://blog.jqueryui.com/2012/04/jquery-ui-1-8-19/ but I can't see the description in the changelog so maybe was fixed earlier