How to make flutter audio_players library works fine on web? - flutter

I am developing a Flutter App that has sound effects when dragging and dropping some widgets; when I was testing it on localhost web and mobile devices, everything works fine; but once I've deployed it on a remote web server, sounds won't play as they're suppossed to:
Sometimes, it just takes a while to load and then it would play fine.
Sometimes, it takes A LOT, by this I mean, sound would play 10 or 20 seconds after widget was pressed.
Sometimes, it won't play any sound and just show this error on console: ยด
The implementation is just as told on the documentation, already tested with local assets and with URL sounds too.
How can I make it work fine on web ?
RELEVANT CODE
import 'package:audioplayers/audioplayers.dart';
class _ResultsScreenState extends State<ResultsScreen>
with SingleTickerProviderStateMixin {
final player = AudioPlayer();
#override
Widget build(BuildContext context) {
player.setReleaseMode(ReleaseMode.stop);
playDrums();
}
playDrums() async {
drumsArePlaying = true;
await player.play(AssetSource('sounds/drumroll.wav'));
}
}

Related

When image picker opens local authentication popup appears in flutter

Im using both image picker and local auth plugins in a flutter project. when im picking images with image_picker plugin (technically it pauses app lifecycle) , local authentication is required to continue. because i've set as follows
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
authenticateUser();
_navigateToHome();
}
}
and also i've faced this issue when picking suggested phone number in textfields.
is there any solution?

Flutter precacheImage not working for AssetImage in Android/iOS when support multiple dimensions

I have been following posts of people trying to use precacheImage to avoid flickering when loading AssetImage first time. Some key code below:
late Image logo;
#override
void initState() {
super.initState();
logo = Image.asset("path_to_logo");
}
#override
void didChangeDependencies() {
precacheImage(logo.image, context);
super.didChangeDependencies();
}
Then later logo is used as a child widget:
Widget build(BuildContext context) {
...
logo
...
}
I also tried to use precacheImage before current page loads in main.dart but that didn't do any difference. The image is empty for a brief while when first time loaded and then all content is shifted to correct position once the asset image loads.
Using sdk: '>=2.19.0-168.0.dev <3.0.0'
Any ideas what could be wrong?
EDIT:
I found out that since I have support for multiple device pixel ratios, the precaching caches only 1x dimensions because I am using the generic path, but the device can be e.g. using 3x and ImageCache also seems to have some internal scale property when it caches images.
This ticket might be about the issue I am facing: https://github.com/flutter/flutter/issues/3905

In background flutter app works only for 1 minute after that it stops it's working, is Dart-Isolates good to try?

made a simple counter app with shake package to increment counter var by shaking phone, things works well when app is running in front (in active state) but opening other app stops this shake feature after one minute, it works only for one minute but not after one minute, i have tried to implement isolate but i couldn't do so, If someone can show me how to implement isolate in following code i will be very thanks full to him,
Thank you
Code is here:
import 'package:flutter/material.dart';
import 'package:shake/shake.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _counter = 0;
ShakeDetector detector;
#override
void initState() {
detector = ShakeDetector.autoStart(onPhoneShake: () {
setState(() {
_counter++;
});
});
super.initState();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(child: Text('$_counter')),
),
);
}
}
Your app is not running in the background. This is not a desktop operating system, where the windows can overlap and be in the background and the process is still running regardless.
Mobile device operating systems are all optimized for foreground app handling. Once your app is no longer the app, it can be removed by the operating system at any time. The operating system will keep a screenshot of your app, to make you think it's still there, but if the system deems it necessary, your app will be shut down and selecting the last picture of your app (which you probably interpret as "get this running app into the foreground" as you know it from desktop operating systems) will actually start a new instance of your app, since the old instance is long gone.
Running code in the background is more complicated than it seems from a desktop mindset that we all have. You can find a good start in the Flutter documentation on running background processes. It might seem overly complicated, because running something in the background is not the norm for mobile operating systems.
You could try this package: https://pub.dev/packages/flutter_background
Just be careful to use dependencies that allow background execution. Location for example should have permissions for "Always Use Location"

Flutter, how to take full screenshot

i am trying to find a way to take a screenshot of entire screen. Flutter library is only allowing me to make a screenshot of a widget, but i want a screenshot of entire android.
I did find this link:
How to programmatically take a screenshot on Android?
and this is resolwing my problem, but i tried to use that java code as it is in documentation:
https://flutter.dev/docs/development/platform-integration/platform-channels?tab=android-channel-java-tab
sadly i am not able to run that code, maybie im making some kind of mistakes? i find a error, while i whas following docs instruction, after that program whas working (not exacly, java whas not able to find my battery, but code whas compiling), and then i copy first answer from that stack link and i tried to pase it in "MainActivity" folder, sadly program whas not working, my "old" java code whas comipilng, from the docs, not the new one.
If you know the solution how to make a photo of entire android screen using some kind of buttor in flutter, or how to implement code from that link into my MainActivity.java folder, please help
Try this package native_screenshot
https://pub.dev/packages/native_screenshot
You can simply run this function to take the screenshot of your phone:
Future<void> _capturePng() async {
String path = await NativeScreenshot.takeScreenshot();
print(path);
}
You can learn more about this package here
It work fine with Android and IOS
Hope it will be useful
You can use screenshot plugin:https://pub.dev/packages/screenshot to take a widget screenshot.
A screenshot is a simple plugin to capture widgets as Images. This plugin wraps your widgets inside RenderRepaintBoundary
Use this package as a library
Add this to your package's pubspec.yaml file:
dependencies:
screenshot: ^0.2.0
Now in your Dart code, Import it:
import 'package:screenshot/screenshot.dart';
This handy plugin can be used to capture any Widget including full-screen screenshots & individual widgets like Text().
Create Instance of Screenshot Controller
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
File _imageFile;
//Create an instance of ScreenshotController
ScreenshotController screenshotController = ScreenshotController();
#override
void initState() {
// TODO: implement initState
super.initState();
}
...
}
Wrap the widget that you want to capture inside Screenshot Widget. Assign the controller to screenshotController that you have created earlier
Screenshot(
controller: screenshotController,
child: Text("This text will be captured as image"),
),
Take the screenshot by calling the capture method. This will return a File
screenshotController.capture().then((File image) {
//Capture Done
setState(() {
_imageFile = image;
});
}).catchError((onError) {
print(onError);
});
Duplicate: How to take a screenshot of the current widget - Flutter

Flutter widget vs Flutter driver

I'm writing tests for a mobile App written in Flutter.
I followed this Flutter Cookbook on testing Flutter apps, to learn how to write widget and integration tests.
This tutorial works perfectly, but I'm still stuck with my own Integration tests.
To simplify, let's assume I have an app, containing only a TextField:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyAppp',
home: Scaffold(
body: Center(child: TextField()),
),
);
}
}
I want to write a test for this app. For instance, I want to test the following scenario:
Open the app
Check that the TextField is empty
Select the TextField
Enter "hello, world!"
Check that TextField contains "hello, world!"
I wrote the following Widget test, which works fine:
void main() {
testWidgets('TextField behavior', (WidgetTester tester) async {
// Create app
await tester.pumpWidget(MyApp());
// Find TextField, check there is 1
final textFieldFinder = find.byType(TextField);
expect(textFieldFinder, findsOneWidget);
// Retrieve TextField Widget from Finder
TextField textField = tester.widget(textFieldFinder);
// Check TextField is empty
expect(textField.controller.text, equals(""));
// Enter text
await tester.enterText(textFieldFinder, "hello, world!");
// Check TextField contains text
expect(textField.controller.text, equals("hello, world!"));
});
}
This test passes, but I wanted to write an Integration test, doing more or less the same, to be able to test it on a real device.
Indeed if this Widget test passes, it will probably pass on all device. But in my app I have more complex widgets and interactions between them, I want to be able to launch tests on both Android and iOS.
I tried to write integration tests using Flutter driver, but I did not find what I wanted in the documentation and examples.
How can I check Widget properties, to verify that my App behaves as expected?
I wrote the following sample:
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
void main() {
group('TextField', () {
final textFieldFinder = find.byType('TextField');
FlutterDriver driver;
setUpAll(() async {
driver = await FlutterDriver.connect();
});
tearDownAll(() async {
if (driver != null) {
driver.close();
}
});
test('TextField behavior', () async {
// ??? how to check that the TextField is empty
await driver.tap(textFieldFinder);
await driver.enterText("hello, world!");
// ??? how to check that the TextField contains hello, world!
});
});
}
The Flutter Cookbook tutorial explains how to retrieve an object by its Text, this would help to check if the Text is present, but for instance is it possible to check that a Container color is red?
Where is the limit between Widget and Integration tests?
Writing Widget tests is pretty straighforward, but I didn't find many examples or documentation about how to write more complexe integration tests using Flutter driver.
If anyone is interested:
To be able to test what I want (not only the presence of widgets, but also theirs states, their properties,...) test driver was not enough for me.
What I did in my project, is to use the flutter_test to write widget tests and check the properties I want.
To test it on a real device (Android or iOS), I used the integration_test package (previously e2e package) available here, which adapts flutter_test results to a format compatible with flutter drive.
To use integration_test, add this line in the main of your widget tests:
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
Then create a file (e.g. integration_test.dart) in your test_driver folder with this content:
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();
Then you can launch these driver test by running:
flutter drive \
--driver=test_driver/integration_test.dart \
--target=test/widget_test/widget_test.dart
With your widgets tests wrote in the file test/widget_test/widget_test.dart.
It really helped me, since some widgets did not have the same behavior on Android and iOS.
Today I use both flutter_test (launched on real device with integration_test) and real flutter_driver tests:
I write widget tests to check a single widget, or a single page,
I use flutter driver to write more sophisticated scenarios to test the whole application.
I think there's a difference between flutter widget and flutter driver. In that, flutter widgets is for widget tests and flutter driver is for integration tests. I'm not exactly sure what that means on a practical standpoint but I think integration test are easier to run tests on application interactions that would be made by a user since integration tests run on an actual device as compared to widget tests. Will update if I find more information on this.