I'm trying to increase the speed of my change in text by pressing a button. So my text is a number.toString() and is increasing by 1 (++) every time I click my GestureDetector that I've created. But I'm trying to use the property onLongPress to increase the speed by which my text increases when I hold the button down. But nothing I've tried has worked successfully!
I've scoured the internet for info and all I found was how to slow it down one by one, which is not only the opposite of what I need but also it doesn't continue decreasing in count when I hold down the button, it only decreases the speed at which the button is pressed per number. The code I used was:
import 'package:flutter/scheduler.dart' show timeDilation;
...
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
GestureDetector(onTap: () => age >= 100 ? null : setState(() {
age++; }),
child: Icon(Icons.add_circle_outline, size: 40.0, color:
Colors.white,),
onLongPress: () { setState(() {
timeDilation = 10.0;
weight++;
});
},
), //GestureDetector
I expected the animation time to decrease, but I didn't expect to have to repeat tapping the button each time to allow for this decrease. I really want to increase the animation time and keep it increasing without having to constantly repeat pressing the button.
Although onLongPress in the GestureDetector is used for longer taps, it's still designed for a single press, not for contnual presses. What you're looking for though can be accomplished with the Listener widget, using onPointerDown, which would allow you to continuously process a tap down. There's a simple example here.
Related
I need that when I press a button on the screen of my mobile device the screen is relocated in a specific part, that is to say I am in a scroll and when I press a button, I need that the screen goes up at the moment of pressing the button.
I need to relocate the screen to a specific place.
you have to use a scroll controller final ScrollController _controller = ScrollController(); and when you press the button you call this function:
void _animateToIndex(int index) {
_controller.animateTo(
index * _height,
duration: Duration(seconds: 2),
curve: Curves.fastOutSlowIn,
);}
I want to calculate percent of double type data in text widget.
Padding(
padding: EdgeInsets.fromLTRB(5, 1, 5, 10),
child: Text( data[numberdata].toString(), //I want to calculate 5 percent of this numberdata
style: GoogleFonts.montserrat(color: Colors.black87, fontSize: 18 , textStyle:
TextStyle(fontWeight: FontWeight.w400)),
textAlign: TextAlign.center,
),
),
A bit tricky to be sure what you are after. But my best guess is:
Text((data[numberdata]*0.05).toString()),
...
child: Text(double.tryParse(data[numberdata].toString())*(5/100).toString()),
or
child: Text((double.tryParse(data[numberdata].toString())*0.5).toString()),
You just need to multiple it by the percent
For example in your text widget replace the code with
${data[numberdata] * 0.05}
This will show 5 percent of the value.
// let us say your number was 100 :
if you want to add a increment + %5 to your number use this :
Text((data[numberdata]*1.05).toString()),
// this will give you 105 .
if you want to add a decrement - %5 to your number use this :
Text((data[numberdata]*0.95).toString()),
// this will give you 95 .
I'm trying to make a 'custom button' for my app.
I have an image of egg with transparent backgroung and I need this image to be a button that ignore tapping on the transparent part.
Right now , I use gesturedetcetor , I also tried inkwell ,they are both detect tapping all the whole image, include the transparent back.
Align(
alignment: Alignment(0, 0.75),
child: Container(
width: swidth * 0.75,
height: sheight * 0.6,
child: GestureDetector(
onTap: () {
setState(() {
pushes--;
});
},
child: Image.asset(
'images/egg.png',
fit: BoxFit.fill,
)),
))
Welcome maor ts in Stackoverflow and Flutter dev.
I was very interesting with your question and your problem, so I decided to solve it with a new package you can use.
This package is transparent_image_button which I recently coded it.
You can use it as this example
TransparentImageButton.assets( "assets/images/egg.png",
width: 200,
onTapInside: () => print("You tapped the image."),
onTapOutside: () => print("You tapped outside the image."),
)
As you can see, you can set function when you tapped on image and another function when you tapped outside the image.
I hope other people can benefit from it also. Thanks for giving me the idea with your question.
In the way, you are implementing it will detect the tap on all over the image.
I will suggest you add the transparent view on the image in the fill area and then add GestureDetector on that view. In this way, you can avoid the click on the transparent area.
Try this code may solved your issue
Row(children: <Widget>[
GestureDetector(child: Container(
width: swidth * 0.75,
height: sheight * 0.6,
),onTap: (){
setState(() {
pushes--;
});
},),
Image.asset(
'images/egg.png',
fit: BoxFit.fill,
)
],)
The purpose of this question is to find the best way to display a lot of images (20 or more) from the gallery.
I need to display all the photos on the on the device. As you can see there could be a lot of images to be displayed, and when I try to display them all using different Image.file() widget, My phone (LG G2) gets slow and then the app completely crashes.
I think the Problem Is that I am Loading a lot of 4K Images (More Than 100) on a 5 years old Device. The Images actually displayed on the Screen at the same time are actually around 15, but I need to have something like a gallery, so all the images in a GridView.
So, I don't think it's the best option, maybe there is a function to downscale the image to like 100x100 px. But looking online I couldn't find anything. So, Is there a better way?
I get all the Image Paths and their folder using a Platform Channel to Android.
I get them all in a list like this:
[["Camera", "storage/emulated/0/downloads/1.png"], etc]
This is The UI I got: (Sorry For the Awful design)
There Are three Main components on the Screen, The IconButton With The Two Arrows:
The DropDownMenu and The GridView.
The IconButton requeries all the paths from android. in the List as stated above.
The DropDown Menu has a function on onChanged which creates all the Cards in the List used by gridView.
This is The Funciton:
void _onChanged(String value) {
setState(() {
_currFolder = value;
_photoCards = calculatePhotoCards();
});
}
And the calculatePhotoCards is this:
List<Widget> calculatePhotoCards() {
List<Widget> list = new List();
for (int v = 0; v < _foldersAndPaths.length; v++) {
if (_foldersAndPaths[v][0] == _currFolder) {
list.add(Card(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20.0))),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: new ClipRRect(
borderRadius: new BorderRadius.circular(8.0),
child: Image.file(
File(_foldersAndPaths[v][1]),
fit: BoxFit.cover,
),
),
),
));
}
}
print(list);
return list;
}
Where _foldersAndPaths is the List containing all the paths which their respective folders (as stated above), while _currFolder is the Selected Folder in DropDownMenu.
You need to add cacheWidth and cacheHeight
example:
Image.network(
shop.shopPic,
width: 46,
height: 46,
cacheWidth: 46,
cacheHeight: 46,
)
I decided to implement this to see if it was something to do with image sizing etc, as there have been a few questions about that in the past for remote images.
When I implemented it, I ran into the exact same problem as you. I thought that flutter did caching of images scaled to a reasonable size but that doesn't seem to be the case. It's actually loading up each image into memory, which is why you're getting a crash. It doesn't take that many images to run out of memory as by the time they're decoded into pixels they're pretty huge.
Here and here are bugs about this (I raised one myself) against flutter.
So unfortunately, I don't think there's a pure flutter way of doing this at the moment. There is a image package on pub that can scale down images but it does it directly in dart code so isn't all that performant.
Until those bugs have been fixed, you may have to look for an android-specific (and iOS-specific if you're interested in that) solution to this. That would entail using the android thumbnail creator to create a bitmap, then either saving that bitmap and passing it's URL back to flutter, or passing the bitmap's bytes directly.
Sorry I don't have a better answer than that!
I'm using CachedNetworkImage and I had an issue with loading multiple Images and the app got to over 3GB ram because of it the solution was
CachedNetworkImage(
height: height,
width: width,
memCacheHeight: height, //add this line
memCacheWidth: width, //add this line
imageUrl: imageUrl,
),
It reduced the memory usage from3GB to 60MB
While testing out an application on Android I noticed something funky going on. A double click event handler has been triggering without any double clicks occurring on that particular item.
Trying to isolate the issue I discovered that pretty much every chain of clicks rapid as a double click on regardless what two objects would cause the second click on the second object to register as a double click, when in fact it is just a single click.
Below is an example consisting of a row of 3 randomly colored rectangles, each one with a mouse area inside of it. The double click of each mouse area is rigged to set the parent rectangle's color to a different random color. Clicking rapidly two different rectangles under android triggers a double click and a color change for the second. This does not happen on Windows or Ubuntu Linux.
Window {
id: main
visible: true
width: 400
height: 400
title: qsTr("Hello World")
Row {
Rectangle {
width: main.width * .33
height: main.height
color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
border.color: "black"
border.width: 2
MouseArea {
anchors.fill: parent
onDoubleClicked: parent.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
}
}
Rectangle {
width: main.width * .33
height: main.height
color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
border.color: "black"
border.width: 2
MouseArea {
anchors.fill: parent
onDoubleClicked: parent.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
}
}
Rectangle {
width: main.width * .33
height: main.height
color: Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
border.color: "black"
border.width: 2
MouseArea {
anchors.fill: parent
onDoubleClicked: parent.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
}
}
}
}
It looks as if the "previous click" or whatever property that's supposed to be used to detect double clicks is shared between different mouse areas instead of being per mouse area. The issue manifests in both Qt 5.7 and 5.7.1.
It definitely looks like my 10th discovered Qt bug this year, but I still feel like asking on the odd chance someone knows what's going on and how to fix it, because I need this fixed, and the Qt bugreport process is not speedy. So any ideas are more than welcome.
Until there is a better answer with an actual solution, it may be useful to know that it is possible to somewhat mitigate the devastating effect this issue has on user experience by reducing the global interval for double click detection.
By default it is the rather lethargic 500 msec. I found out that by reducing it to 250 msec helps to avoid over 90% of the incorrect double clicks:
QGuiApplication app(argc, argv);
app.styleHints()->setMouseDoubleClickInterval(250);
Additionally, there is a quick and hacky qml-only way to create a "fixed" copy of MouseArea:
// MArea.qml
Item {
id: main
property alias mouseX : ma.mouseX
property alias mouseY : ma.mouseY
property alias acceptedButtons: ma.acceptedButtons
// etc aliases
signal clicked(var mouse)
signal doubleClicked(var mouse)
// etc signals, function accessors
MouseArea {
id: ma
property real lClick : 0
anchors.fill: parent
onClicked: {
var nc = Date.now()
if ((nc - lClick) < 500) main.doubleClicked(mouse)
else main.clicked(mouse)
lClick = nc
}
}
}
This one actually works as intended and can be made almost entirely "plug and play" compatible with the original one.