Forcing portrait mode makes Flutter app not responsive - flutter

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()),
);
}

Related

Is there any way to show splashScreen when app is in the background or switching tabs. In ionic 6?

I use privacy-screen plugin to hide content when app is in the background, but it shows just gray background. I need to set splashScreen instead. Any suggestions?
I tried splashScreen plugin, but it is not working for me.
I don't know if it's the best way but I think that using Capacitor appStateChange from #capacitor/app (documentation) you could show and hide a screen with your logo when app is in background or foreground.
import { App } from '#capacitor/app';
App.addListener('appStateChange', ({ isActive }) => {
console.log('App state changed. Is active?', isActive);
if(!isActive) showLogoScreen();
else hideLogoScreen();
});
I hope it helps :)
For iOS, I did it simply in native code: just find the "applicationWillResignActive" method in AppDelegate and insert a view on top of the window.
For Android, sadly I couldn't find a better solution. The "gray background" you described is likely the implementation of Hide screen in 'Recent Apps List', but allow screenshots

how to change the status bar color in flutter web app?

I am designing a simple web app and the use case requires the status bar color to change as soon as the browser opens.
I tried using the below mentioned code but it seems to work only on android. I want it to work in flutter web. Any solutions or ideas would be appreciated.
void main() {
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
statusBarColor: Color(0xffff3838),
));
configureApp();
runApp(const MyApp());
}
But the results come as :
I want to change this blue color to orange.
Thank you!

Flutter Integration Testing- How to go back to previous screen when no back button available on screen

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();

Load Data during Splash Screen in Flutter

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.

In Flutter widget testing, how to pause app to find suggested Finder?

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();
});