onTapUp isn't called after onLongPress - uigesturerecognizer

I'm trying to create a context menu that is displayed after a long press whilst keeping your finger held on the object, afterwards you would select an option (by dragging your finger) and lift it to initiate the action.
I noticed something that to me is a little strange. So first, the code:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
body: GestureDetector(
onLongPress: () {
print("onLongPress");
},
onTapUp: (TapUpDetails details) {
print("onTapUp");
},
child: Center(
child: FlutterLogo(),
),
),
),
));
}
So if I run this, after long pressing the icon, then the handler for onLongPress is called but after lifting your finger onTapUp isn't called. If I comment out the handler for onLongPress (all 3 lines) then after a long press onTapUp is called. I would like to handle both onTapUp and onLongPress.
Any ideas why this is happening?

You can wrap in another gesture detector that will handle tap up.
I am not sure if current behavior is a bug, so you might want to submit it to issue tracker.

I don't know if the PR will be pass but I've solved it with a new gesture onLongPressUp.
Here is it https://github.com/flutter/flutter/pull/18949

Related

flutter bloc and navigation drawer

New to Bloc and love having a structured app!
I used the firebase login example:
Flutter Firebase Login Tutorial
Works great and great example. Building upon this example I decided to do it step by step (avoid big mistakes).
I created a bottom navigation with an appbar that has a logout icon. Clicked on logout and it works.
Now I wanted to move the logout to the navigation drawer. I created a statelessWidget dart file called DrawerItems and then I call it from my bottom navigation file (within my scaffold). Here is part of the code of the DrawerItems:
DrawerListItem(
navListIcon: const Icon(Icons.logout_sharp),
strTitle: "Logout",
myFunction: () => context.read<AppBloc>().add(AppLogoutRequested()),
),
When I click on the logout icon, it doesn't do anything. Do I really need to make this statefulWidget? the whole point of bloc is to use stateful widgets as little as possible no?
any insight will be great!
Thank you in advance :D
Ok I figured it out... not really happy about this answer (I like having different files for everything), so if any of you know why this is happening feel free to drop a comment!
I removed the "myFunction" that I was passing and put the read bloc line directly in the drawer stateless class and it worked.
so this is my final code (I kept the old code there too FYI):
Widget build(BuildContext context) {
const drawerHeader = UserAccountsDrawerHeader(
accountName: Text("Account Name"),
accountEmail: Text("Account Email"),
currentAccountPicture: CircleAvatar(
child: FlutterLogo(size: 42.0),
),
);
return ListView(
children: [
drawerHeader,
// DrawerListItem(
// navListIcon: Icon(Icons.account_circle_sharp),
// strTitle: "Account information",
// ),
// DrawerListItem(
// navListIcon: Icon(Icons.filter_list_sharp),
// strTitle: "Filter",
// ),
ListTile(
leading: const Icon(Icons.logout_sharp),
title: const Text("Logout"),
onTap: () {
context.read<AppBloc>().add(AppLogoutRequested());
Navigator.pop(context);
},
),
// DrawerListItem(
// navListIcon: const Icon(Icons.logout_sharp),
// strTitle: "Logout",
// myFunction: () => context.read<AppBloc>().add(AppLogoutRequested()),
// ),
],
);
}

How can I improve the performance of Wrap Widget with multiple child?

When I move from tabs in the UI it's takes too long to load the chips under the tabs.
I've used the AutomaticKeepAliveClientMixin<> but it broke my Scrollbar and still takes time to load large Chips children from the Wrap Widget. What I would need is a Wrap.builder() but doesn't exist. Is there something I can do to optimize the code to load smoothly?
I have an interactive sample code of the current problem:
https://dartpad.dev/009e9ccae07175074cb77d7792c3692b
Try moving the tabs left to right in the sample to see the performance issue.
If it can't be fixed, I wonder if I can detect when the widget is rendering to show a circular progress indicator while switching tabs.
Any Ideas?
Thanks
Technically the idea of Wrap.builder wouldn't be a solution here because the issue persists even when all the items are in the displayed (nothing to display on demand)
You can make the swiping experience better by delaying the rendering of the tab content until after the swiping animation is done
class _ChipsContentState extends State<ChipsContent> {
bool visible = false;
final List<String> _filters = <String>[];
#override
void initState() {
super.initState();
Future.delayed(kTabScrollDuration).then((value) {
if (mounted) {
setState(() => visible = true);
}
});
}
#override
Widget build(BuildContext context) {
if (!visible) {
return Center(child: CircularProgressIndicator());
}
return ListView(
children: [
if (visible)
Wrap(
spacing: 8.0,
children: widget.chipData.map((chipName) => ChipFilter(chipName: chipName, filter: _filters)).toList(),
),
],
);
}
}
A Wrap.builder() is indeed missing from Flutter. A GitHub issue was opened about this but then closed without a satisfying solution.
I can think of a few workarounds, like using a swiper/carousel, a GridView.builder(), or a List.builder() with Rows inside it. Of course, they're far from being as convenient as a Wrap in this case.
Try Changing your filterChips and "build" methods as follows,
List<Widget> get filterChips {
List<Widget> widgets = [];
for (final chipName in widget.chipData) {
widgets.add(ChipFilter(chipName: chipName, filter: _filters));
}
return widgets;
}
#override
Widget build(BuildContext context) {
// super.build(context);
return Scrollbar(
child: SingleChildScrollView(
child: Wrap(//IS THERE A Wrap.builder()? OR ALTERNATIVE TO DESIRED LAYOUT?
spacing: 8.0,
children: filterChips,
),),
);
}

Can I run "image.dart" functions in background

I want to display a loading widget while compressing the image selected.
I am using the "image.dart" package to select an image, using ImagePicker.GetImage().
Then I want to display a loading widget while the image is being compressed, and when it's finished display the compressed image.
My code fundamentally works, my loading widget is cool (thanks spinkit), I can select any image, compress it and display the new one, but there is a small freeze (2-3s) while the image is being compressed.
So I'm trying to put a loading widget to not make the user panic and tap 10 000 times to select the image wanted, but I'm not completely comfortable with asynchronous code yet.
Here's my code :
import 'package:image/image.dart' as Img;
#override
Widget build(BuildContext context) {
if (loading == true) {
return LoadingScreen();
} else {
return Scaffold(
backgroundColor: Colors.brown[300],
body: Column(
children: [
SizedBox(
height: 50,
),
RaisedButton(
child: Text('Select an image'),
onPressed: () async {
await getImage();
// You can see here that I'm trying to trigger the loading widget
setState(() {
loading = true;
});
compressImage();
// And I want to disable the loading widget here, after the compression
setState(() {
loading = false;
});
},
),
SizedBox(
height: 10,
),
pickedImage != null ? printSelectedImage() : Center(child: Text('Please select an image')),
],
),
);
}
}
getImage() async {
pickedFile = await picker.getImage(source: ImageSource.gallery);
}
compressImage() async {
Img.Image selectedImage = Img.decodeJpg(File(pickedFile.path).readAsBytesSync());
if (selectedImage.width > selectedImage.height) {
Img.Image compressedImage = Img.copyResize(selectedImage, width: 500);
File(pickedFile.path).writeAsBytesSync(Img.encodePng(compressedImage));
} else {
Img.Image compressedImage = Img.copyResize(selectedImage, height: 500);
File(pickedFile.path).writeAsBytesSync(Img.encodePng(compressedImage));
}
if (pickedFile.path != null) {
setState(() {
pickedImage = pickedFile.path;
});
}
}
But the result is the same, the screen is still stuck in the file selector while compressing, and then directly display the compressed image.
I started learning dart/Flutter 2 weeks ago, so am I missing some fundamental principles of the dart language ? Am I not seeing something obvious ?
Thanks for reading
Making something async doesn't move it into some magical background thread. Dart uses isolates which are basically an execution context in which dart code can run. Flutter has a single isolate which runs your app, if you do too much work in it, your app gets slow or skips frames.
You can create other isolates to do calculations and offload work and communicate between isolates by passing messages with primitive content back and forth. The drawback in Flutter is, that other isolates don't have access to Flutter plugins thus a lot of things don't work there.
There are ways to work around this but they are pretty involved.
The easiest way to start an isolate is the compute(callback, message) function.
Spawn an isolate, run callback on that isolate, passing it message, and
(eventually) return the value returned by callback.
I am not sure if the image/image.dart library uses a plugin or some other features that are not available (dart:ui for example), you can try offloading your image compression into the callback. If it fails with a MissingPluginException or something similar, then it won't work.
Remember, you can only pass primitive messages so you can not path the pickedFile, only the pass as argument.
For your problem however there may be an easier solution. The image picker accepts parameters for max width/height and quality. If you need the image to be encoded as PNG however then you need to create a complex solution yourself.

Hero animation starting from side menu drawer and ending on destination screen in Flutter

I'm trying to do a Hero animation that starts from side menu drawer's item (it's Hero widget) and ends up on new screen's Hero widget. For example, on image shown below, a pie-chart icon should animate to the final widget when destination route is shown:
Each screen contains it's own scaffold with shared drawer widget instance:
Scaffold(
...
drawer: MyAppDrawer();
)
So for each of the screens and corresponding drawer items, I have the Hero tag on the screen that equals to the side menu drawer item Hero tag.
The destination screen is replacing the current screen like so:
Navigator.of(context).pushReplacementNamed("/");
After trying to push replacement screen on menu item action, instead of hero animation, I'm getting the following error:
There are multiple heroes that share the same tag within a subtree.
I guess it's because each of the screens already contains identical MyAppDrawer (with the same Hero tags)... But I can't omit the drawer from other screens because i need that menu to be globally accessible.
Is Hero animation from the common app side menu drawer item to destination screen Hero widget possible?
Any experiences with showing Hero animation from the drawer to the screen?
Thanks!
The problem is that you are not sharing the drawer with all the screens you are for each screen creating a new MyAppDrawer. Thinking fast here you could create the MyAppDrawer somewhere else and import the same object onto every screen
I have already facing this hero problem in my project.
If you put same hero tag like
tag:1 , tag :1, tag :1
then you will get this kind of error
tag:2 , tag:3 , tag:4
You will get your perfect solution through this trick.
and remembar that every hero tag must be unique in one screen.
And If you have using flotingActionButton then you have to put define tag inside button.
GestureDetector(
onTap: () => Navigator.push(
context,
PageRouteBuilder(
transitionDuration:
Duration(milliseconds: 700),
pageBuilder: (_, __, ___) => StoryInside(
tag: "Story $index",
))),
child: Hero(
tag: 'Story $index',
child: Material(
animationDuration: Duration(seconds: 20),
color: Colors.transparent,
child: ListTile(
title: Text(
'Story $index ',
style: style,
),
),
),
)),
Hero(
tag: '${widget.tag}',
transitionOnUserGestures: true,
child: Text(
'${widget.tag}',
style: style,
),
),

Flutter - How to use a FadeTransition inside a StreamBuilder?

I am trying to make a colour transition animation inside a StreamBuilder which is inside a stateless widget. I have no idea how to perform that since all the examples and tutorials about this subject use a Stateful Widget.
I thought about using FadeTransition Widget but and maybe store the state in my Bloc that controls that view.
Please give me any suggestions if you have, Thank you.
To perform a fade in transition in a streambuilder, it's easy, simply use a widget called AnimatedSwitcher :
#override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
return AnimatedSwitcher(
duration: Duration(seconds: 1),
child: _getMainWidget(snapshot, context),
);
},
);
}
Here we use an AnimatedSwitcher to animate a transition when the child of the AnimatedSwitcher change's, the default animation is a fade animation, but you could add your custom animation by passing the widget a TransitionBuilder as an argument

Resources