I am working on a new app and unfortunately it annoyed me that I have to add Lock Device Orientation every time by downloading the code locally to my Macbook. But from now on, it's over!I invite you to my next quick tutorial.
Step 1: Create Custom Action[1ststeplock.png] Step 2: Complete the Action name, eg "lockOrientation", Click on "View Boilerplate Code" and select "Copy to Editor", Paste the code into the text box.
Code:
import 'package:flutter/services.dart';
Future lockOrientation() async {
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
}
[Screenshot at May 28 19-48-24.png] Step 3: Compile and Save Step 4: Click on the first layer of your Widget Tree (main app view) and select "+ Add Action".[step 4.png] Step 5: Select Type of Action "On Page Load" and select "Custom Action"[Screenshot at May 28 20-26-38.png] Step 6: Choose the action we created![Screenshot at May 28 20-27-17.png] And that's all!Once you build the app, you can test the orientation on your device. Choose the appropriate lock for you and replace the code: await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]); await SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]); await SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeRight]); Next year I am getting married with my sweetheart, if you like my tutorial, you can support me and buy me a coffee. I promise I will do more tutorials! https://www.buymeacoffee.com/dziedzic