I need to know is there any way to perform a task during splash screen is visible in flutter. I know how to add splash screen in flutter but I don't know how to perform background operations during splash screen is visible. I can't find anything on the internet, please help.
Yes, yes you can. The main() function can actually be tagged as async, so you can do whatever you need to do before running runApp(...) in the main() method body, even asynchronously. This way, the splash screen will be shown until your asynchronous result is retrieved, before calling runApp(...). For example:
Future<void> main() async {
// Do whatever you need to do here
final home = await setHomeWidgetDependingOnLoginStatus();
return runApp(MyApp(home: home));
}
The animated_splash_screen package does this worderfully. add it to your pubspec.yml
animated_splash_screen: ^1.1.0
AnimatedSplashScreen.withScreenFunction(
splash: 'images/splash.png',
screenFunction: () async{
// Your code here
return MainScreen();
},
splashTransition: SplashTransition.rotationTransition,
pageTransitionType: PageTransitionType.scale,
)
This will load and hold you splash screen until your function is complete.
Your function has to return a widget, in this case is your home screen. This also allows for dynamic routing depending on your data eg, is user logged in.
Related
want to create a screen that launch when a firebase notification is received while the app is in the background or terminated in flutter (like package callKeep ).please anyone help me create that.
You just need to handle its background handler method it will works.You need to initialize this method in main() method.
Future<void> backgroundHandler(RemoteMessage message) async {
debugPrint(message.toString());
debugPrint(message.data.toString());
debugPrint(message.notification!.title);
}
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
I was trying to force portrait mode in my flutter app.
For that, I found a piece of code around StackOverflow that I tried to use:
in main.dart:
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp,DeviceOrientation.portraitDown])
.then((_) => runApp(MyApp()),
);
This resulted on the app becoming totally not responsive, i.e, no button taps, no response when clicking on Tab to change tab, etc.
I verified that the code above caused the problem since, once removed, the app works as normal.
Any better option or suggestion for that.
I had the same problem but solved by this, I hope it can help you,
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]).then(
(_) => runApp(MyApp()),
);
}
How to go back to previous screen when no back button available on screen?
Additional Information: I am trying to automate integration testing for flutter app. There is no back button on screen which I can use to go back screen.
Additional information: I am writing integration tests, for same I need to go back to previous screen. And due to design there is no back button on screen, I need to do by scripts only.
This is what worked for me for OpenContainers from the animations package:
final NavigatorState navigator = tester.state(find.byType(Navigator));
navigator.pop();
await tester.pump();
It's used in the flutter package's tests:
https://github.com/flutter/packages/blob/63040337424f345452a5e07632774e6585329ac0/packages/animations/test/open_container_test.dart#L234
If the purpose of the test is to stress and verify the WillPopScope callbacks as well, the following approach can be used:
final dynamic widgetsAppState = _._tester.state(find.byType(WidgetsApp));
await widgetsAppState.didPopRoute();
This is used in the Flutter official repository for testing: https://github.com/flutter/flutter/blob/master/packages/flutter/test/material/will_pop_test.dart
Note that this approach also works to simulate a back button pressed on Android.
Well, it's maybe a little bit late, however there are two possible ways to achieve this:
final backButton = find.byTooltip('Back');
await driver.tap(backButton);
or:
await driver.tap(find.pageBack());
you can use like this as ios provided
theme: ThemeData(
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
}
)
),
My emulador defaulf locale is "en_US" so I use the word "Back" as tooltip and it is working.
final backButton = find.byTooltip("Back");
await tester.tap(backButton);
await tester.pumpAndSettle();
I have a screen in my app with an image as the background, this image is on my asset folder. When I navigate to this screen the background is blank for an instant and then the image is loaded and it appears. This makes my screen look weird for a half a second or less, but enough for the user to notice.
I wanted to be able to only navigate to that screen once the image is loaded (I can wait on the previous screen), so I can avoid this behavior and have the screen as a whole when the user navigates there.
I know can make the image fade in, but that's not the experience that I want.
You need to use ImageProvider.resolve method
So suppose you have some image registered in you assets.
Then you may obtain the image provider as
_imageProvider = Image.asset("your_asset").image.
Then you may add such method:
import 'dart:ui';
Image _image;
bool _resolved;
void _resolveImageProvider() {
ImageStream stream = _imageProvider.resolve(createLocalImageConfiguration(context));
stream.addListener(ImageStreamListener((info, _) {
setState(() {
//here you may set some flags or anything which indicates the image is in memory now.
_resolved = true;
_image = info.image;
});
}));
}
Then you can call this method on the "previous screen", wait till the listener is triggered and only then navigate to the next view.
I am following flutter widget testing as https://flutter.io/testing/#widget-testing.
It says that
simply flutter run test/widget_test.dart to see your test run in your preferred runtime environment such as a simulator or a device. During a flutter run session on a widget test, you can also interactively tap parts of the screen for the Flutter tool to print the suggested Finder.
I am using flutter run test/widget_test.dart so that i can tap and get suggested finders. But I am not able to pause the test so that i can actually tap. The test runs and automatically ends. So that there are no widgets on the screen except Text('Test Finished').
Thank you very much!
I know it's been almost 2 years since the question was asked, but anyway. tester.idle() helped me to pause an app.
testWidgets('idle', (WidgetTester tester) async {
await tester.pumpWidget(
//
);
await tester.tap(find.byIcon(Icons.bluetooth));
await tester.pump();
tester.idle();
});