I've been using Flutter for about 8 weeks.
It's awesome, no question.
However, there has been something bugging me since I've started, and that is, why do some widgets have an onTap method whilst others have onPressed? Google has not been able to give me any answers.
If this is not relevant or on-topic enough for Stack Overflow, sorry about that, please point me to the right community to ask.
In my experience, onTap() is used for any gesture capture and onPressed() especially for buttons.
Just like in real life, when you have to use a button, you'll press it.
Hope this will solve your troubles even though I'm not totally sure about that.
Currently they both are same. Just use onPressed with Buttons and onTap with other widgets.
Flutter is still new so there are lots of overlaps. May be in future updates they will make it more streamline . Like they will merge many widgets which are almost same.
The Source of differents between onTap and onPressed is GestureDetector used in buttons.
onTap should give you response in the first moment touching the screen.
onPressed should be equvalent to .onTapDown property - the last moment touching the screen.
Related
Looking for a widget similar to the answer by NiklasPor in this question here. The whole idea fits my needs - have a button that changes border drawn based on how long the button is held. The problem is that the CircularProgressIndicator is used here. I need on hold feature and border drawing to be compatible with the TextButton widget, for example. Basically, I want a TextButton to execute a function if it is held long enough. Any ideas how to start with this? I am thinking of also using the GestureDetector widget.
Have you tried this package: square_percent_indicator
This effect
I couldn't find any ways to produce this type of home screen card slide menu effect or any combination of widgets to reproduce this, sure could use some help.
This type of custom Drawer done with Stack, AnimatedBuilder and Transform. Most difficult part there is gesture behavior, that could be copied from original Drawer flutter source code.
Marcin SzaĆek represented this implementation, alongside with few other features on Flutter Europe.
At this video he describes how he done it.
And this is link to his github with this feature code implemented.
Goal: Switching between two very simple Widgets containing simple Text with the help of AnimatedSwitcher.
Below is my code:
Whenever i press the button which should trigger the animation from the first widget to the second widget, nothing happens.
Both prints appear in the console : 'before' and 'after'.
Is this some kind of bug or why isn't it working?
Since both of your widgets are pretty much the same, flutter needs some kind of differentiation between them. The most straight forward approach would be using a UniqKey() on both of your containers.
...
Container(key: UniqKey(), ...),
I would like to hide a Text widget when a user is currently tapped into a TextFormField to type. And when they aren't using it, I would like for it to be visible again. Only problem is I don't see a way to accomplish this. Any help is appreciated!
The easiest way is to check whether the user is using TextField or not is by detecting whether the keyboard is visible or not. For that follow this stack-overflow post. Once you know whether keyboard is visible or not, you can simply update the state(setState()) and decide whether to hide the widget or not. Follow this stack-overflow post as a reference to hide and un-hide widgets in flutter.
First of all, I'm a real beginner with Flutter, so I have probably not even grasped the basic design patterns yet. My problem is probably simple for someone who understands flutter. Here goes:
I have a tabbed GUI in my app and a floating action button. Each tab has a list that I want to update using that action button (FAB). The easy way to do this seemed to have one action button for each tab, but I read that you should not do this in flutter. So I made the button "global" and call a method on the current tab when the button is pressed. The method then adds some data that should be shown in the list, but the problem is that the list of course isn't actually updated just because I add data. The setState() is never called inside that list widget.
All the exampels I've found are very simple with the FAB being inside the list having access to its setState() method. But in my case I'm outside the list. How do I solve this? Notifier? Stream? I'm lost...
I think the correct solution is to use a "Stack" and "Positioned" widged like:
- Stack
- Scaffold
- TabBar
- Positioned
- FloatingActionButton