Flutter: How to use HapticFeedback - flutter

I have imported
import 'package:flutter/services.dart';
and then called
HapticFeedback.lightImpact();
nothing happens. What do I need to do to get it working?
I am testing with the latest Flutter version 1.6.6 on a Galaxy S8 running Android 9.0

Please make sure that Vibration feedback is enabled on the android device, and make sure to add
<uses-permission android:name="android.permission.VIBRATE" />
to the AndroidManifest.xml.
See more info here: https://github.com/flutter/flutter/issues/33750

import 'package:flutter/services.dart';
import the services package
then inside onTap method
onTap: () {
Clipboard.setData(ClipboardData(text: data));
HapticFeedback.heavyImpact();
}
it worked for me.

Please also check whether, Vibration feedback setting was enabled on the device or not . I faced same issue but later found out that Haptic Feedback vibration was off on my device (I was testing it in Android 9) so, make sure to enable it.
To enable it,
Go to settings>sound&vibration>touch vibration

Try using this for Android:
Feedback.forTap(context);
More info: https://api.flutter.dev/flutter/material/Feedback-class.html

This is helped me
First Go to AndroidManifest and add
<uses-permission android:name="android.permission.VIBRATE" />
If the Flutter app is running stop it and run it again.
Then use this code
import 'package:flutter/services.dart';
And in Ontap or OnPressed Method
Clipboard.setData(ClipboardData());
HapticFeedback.heavyImpact();
That's all

Reading the function description it says:
"On Android, this uses HapticFeedbackConstants.KEYBOARD_TAP."
If the vibrations for the keyboard or 'haptic feedback for tap' are turned off (in the phone's settings), calling HapticFeedback.mediumImpact(); will not have an effect.
To fix the problem one would have to turn on Touch vibration in the phone's settings.

If only activating touch vibration on phone doesn't solve the issue , try switching between Vibrate on tap modes.Default setting is Off.
Vibrate on tap (Settings -Off(default),Ligh,Medium,Strong)

Related

How to restart app with button in flutter

I have a setting screen, when I change FontFamily it needs to restart the app to change all app fonts, And it restarts after the app closed, but I want to restart the app with a button instead of closing the app. does there any way to do this ?
The best solution... using "Restart App" module
Instal from your teminal:
flutter pub add restart_app
Import from your dart file:
import 'package:restart_app/restart_app.dart';
Call "Restart App" from your Button:
onPressed: () {
Restart.restartApp();}
Have a nice day...
Use this package https://pub.dev/packages/flutter_phoenix and call Phoenix.rebirth(context); when you want to reload the app
I have implemented the https://pub.dev/packages/flutter_phoenix package in my app, to restart the app wrap widget with Phoenix on the line of code:
runApp(Phoenix(child: MyApp()))
wherever you want to restart the app just implement this line of code
Phoenix.rebirth(context);
hope this help

image_picker: ^0.7.2+1 makes the app crash

i am using image_picker: ^0.7.2+1 here in my app
i am using this code for opening the camera
File _image;
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}
also Added these Permissions in android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET"
after using above package when my camera is opening immediately the app crashes and in terminal there is no error message it just says device disconnected. (by app crash i mean app restarts (from splash screen )automatically and captured image is also lost)
this above app crashing issue comes only in android 10 or above android version also in android 10 or above (android 11) also this packages does not asks for the permission of camera in android 10 or 11 which i guess can be the actual problem behind this issue
while i use android 8 or device with android version less then android 10 the image_picker: ^0.7.2+1 here works totally fine with same above given code (no changes are made in code just changed the device from android 11 to android 8) the app asks for permission as expected and then camera opens and successfully pic is captured and hence everything works fine nut same thing fails in android 10 or android 11
note : i also tried using permission_handler plugin with image_picker but it also didn't worked for me
can anyone please help me to fix this issue
also if some one know any substitute code or package for image_picker can please tell me.. i just want to take pic from from camera in flutter
I too went through all of this, setting up permission handling, doing the cache fix, and a few other bits I found online, and after a week could not solve it. Image_picker still crashed my app as soon as I took the shot. My solution in the end was to use camera_camera 2.0.1. It is a terrible library name, and it doesn't include compression, but the camera interface is nicer, and it works perfectly out of the box.
import 'package:camera_camera/camera_camera.dart';
onPressed: (){
Navigator.push( context, MaterialPageRoute(
builder: (_) => CameraCamera(
onFile: (file) {
// Do what you like with File file
// I convert to base64 ready to upload
Navigator.pop(context);
},
)))
},
Add android:requestLegacyExternalStorage="true" as an attribute to the <application> tag in AndroidManifest.xml. The attribute is false by default on apps targeting Android Q.
///// SOLVED /////
All you have to do to fix this is add these 6 lines to ios/Runner/Info.plist
<key>NSPhotoLibraryUsageDescription</key>
<string>Allow access to photo library</string>
<key>NSCameraUsageDescription</key>
<string>Allow access to camera to capture photos</string>
<key>NSMicrophoneUsageDescription</key>
<string>Allow access to microphone</string>
I had the same problem on several devices.
And since I used android:launchMode="singleTask" in AndroidManifest.xml everything works fine for me..
After stuck of 2 weeks finding solutions all round, I found my solution to give profile access to my users.
The solution is I used two alternative packages that work well.
For Getting Image Type
file_picker 5.2.2
For Getting Camera Image
camera 0.10.0+4
Now,it is working very well.
Also,we need to give kinda to Flutter as it is trying its best though having platform issue in some cases.

Flutter model_viewer plugin for iOS not showing anything

I tried to build an AR app using Flutter and model_viewer plugin. It works just fine in Android but when I tried to test it using iOS it is just showing a blank page. I tried to follow the documentation provided in https://pub.dev/packages/model_viewer by adding :
<key>io.flutter.embedded_views_preview</key>
<true/>
but nothing has changed. Can you explain why?
Did you try to insert YES instead of true, beetwen a balise string like?
io.flutter.embedded_views_preview
YES

Flutter show app icon badge on push notification

I'm looking for a way to show an app icon badge when a user receives a push notification.
I'm using the Firebase_messaging plugin for flutter for the handling of the push notifications and flutter_app_badger for the app icon badges.
But I want to combine the two so that the number is set on the icon without opening the app. Is it possible to make this happen? Or am I overlooking something obvious from the firebase_messaging plugin?
Sorry for the horrible explanation. I hope someone can help me with this issue.
App icon badge depends on the Application Launcher
Some of the Android Application Launcher includes this functionality by default, You can check this on Settings->Apps->Notification
Attached image
FYI
How to show notification count on app icon like Facebook?
How does Facebook add badge numbers on app icon in Android?
adding notification badge on app icon in android
My suspicion is that you have an icon that isn't compatible with the icon guidelines in your device. I suggest trying https://pub.dev/packages/flutter_launcher_icons to make icons for your device.
I think you try to create stream when run app to real time listen doc changes in Firestore. Read here How to listen for document changes in Cloud Firestore using Flutter?
And the icon can be update in background but without opening app is impossible I think.
I don't have a direct answer because I haven't encountered this issue before, but this might fix your problem
Ensure that you have defined the icon for your application properly - for flutter, here are the steps you should have:
Import the package into your pubsec.yaml file - it should be called flutter_launcher_icons: "^0.8.0" and imported under the dev-dependencies section of the pubspec.yaml file.
After the dev-dependencies section, add a new section for flutter icons as so:
flutter_icons:
ios: true
android: true
image_path_ios: "{Icon File Path}"
image_path_android: "{Icon File Path}"
Hopefully this helps, and good luck with fixing your issue!
Using https://pub.dev/packages/firebase_messaging for notifications ,
for Android Background notifications you can add this in your AndroidManifest.xml file in your application tag. And make sure you have androidlogo.png (this is example name) present in your drawable folder.
<application>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/androidlogo" />
</application>
In case of foreground notifications, you must be using Flutter Local Notifications, so you can provide the same while initializing like
var initializationSettingsAndroid = new AndroidInitializationSettings('androidlogo');
flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: onNotificationClicked);
And for application icon you can use this library https://pub.dev/packages/flutter_launcher_icons which is also suggested by answers above.
In case of iOS, your launcher icon will your notification icon.
And for display of badge icon you can use https://pub.dev/packages/flutter_app_badger. (For supported phones).
So using all this you will get both app icon with badge and notification icon as your app icon (or specified by you).
You can control the badge count from the notification payload.
If you send a notification manually from Firebase Console, you can set the badge count on Additional options step:
If you send it programmatically, add this payload (iOS example):
{"aps":{"alert":"Enter your message","badge":1,"sound":"default"}}
For more payload options, check out FCM documentation

Flutter app shows a black screen before loading the app

I have noticed this problem with flutter apps, when I open a flutter app from cold boot, I see a black screen popping before the actual app is loaded. I have seen the problem with the Newsvoice production app and as well as with a test app I installed.
Check the video here: https://www.youtube.com/watch?v=zszud6UWzps
Is it a bug in the Flutter SDK?
This issue was fixed recently. If you are using a version of Flutter that has this engine fix, you won't see the black frame. (The fix should be on the Flutter master branch by now, but not the alpha branch.)
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="#drawable/my_splash"
/>
AndroidManifest.xml check the FlutterActivity and add this code
It's not issue, this for hot reload. Don't worry about that. When you run with release, you can't see this.
if you want to be sure try ->
flutter run --release
It's not a bug. That's the way it behaves normally. You can replace the loading black screen with an image:
In AndroidManifest.xml, here is where you can change your splash image.
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="#drawable/launch_background" />
Find the files:
android\app\src\main\res\drawable\launch_background.xml
android\app\src\main\res\drawable-v21\launch_background.xml
Change the files to add your own custom image:
<item>
<bitmap android:gravity="center" android:src="#drawable/splash_image" />
</item>
Your splash image should be stored in the drawable folders:
android\app\src\main\res\drawable\splash_image.png
app\src\main\res\drawable-v21\splash_image.png
I can solve this issue, after removing FutureBuilder when using Firebase. just initialize Firebase app on main() like this
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
instead of using FutureBuilder on your build method
The black screen is something which comes for a variety of reasons is what I have gathered from looking at different responses on the net. In my case I realized on thing, that my Firebase.initializeApp() was returning an error in the snapshot of my FutureBuilder due to which no widgets were being rendered. I tried everything to no avail. Finally I moved my project to a completely new folder as described here
and then my black screen issue got resolved.