|
| 1 | +--- |
| 2 | +title: Custom Files |
| 3 | +sidebar_position: 4 |
| 4 | +--- |
| 5 | +# Custom Files |
| 6 | + |
| 7 | +We allow you to add a custom code directly into the `*main.dart*` file of your app. This means that you can easily implement specific functionalities and customize your app as per your requirements without downloading the entire project and manually modifying the file. |
| 8 | + |
| 9 | +By default, the file you want to edit is in 'read-only' mode. However, using our editor, you can add a code snippet in the form of [custom actions](/customizing-your-app/custom-functions/custom-actions). |
| 10 | + |
| 11 | +:::warning[Please note] |
| 12 | +You can only add custom actions that have no arguments (including *BuildContext*). |
| 13 | +::: |
| 14 | + |
| 15 | +## Edit *main.dart* |
| 16 | + |
| 17 | +The *main.dart* is a critical file in your project, as it serves as the entry point for the application. This file contains the `main()` function, which is responsible for starting the application by running the code that builds the UI and initializes any other necessary components. |
| 18 | + |
| 19 | +You can edit the *main.dart* file to include anything in `main()` function that we don't yet support. For example, initializing third-party plugins or libraries and setting up system-level configurations, such as changing the status bar color or orientation. |
| 20 | + |
| 21 | +Let's see an example of how you can add a code in *main.dart* file to change the status bar color for the mobile app. Here's how it looks: |
| 22 | + |
| 23 | +<figure> |
| 24 | +  |
| 25 | + <figcaption class="centered-caption">Changing the status bar color for the mobile device</figcaption> |
| 26 | +</figure> |
| 27 | + |
| 28 | +To do so, you can edit *main.dart* file by following the steps below: |
| 29 | + |
| 30 | +2. Create a [custom action](/customizing-your-app/custom-functions/custom-actions#adding-custom-action) for the code you want to include in a *main.dart* file. For this example, here's code in a custom action named 'setStatusbarColor'. |
| 31 | + |
| 32 | +```dart |
| 33 | +// Automatic FlutterFlow imports |
| 34 | +import '/backend/backend.dart'; |
| 35 | +import '/flutter_flow/flutter_flow_theme.dart'; |
| 36 | +import '/flutter_flow/flutter_flow_util.dart'; |
| 37 | +import '/custom_code/actions/index.dart'; // Imports other custom actions |
| 38 | +import '/flutter_flow/custom_functions.dart'; // Imports custom functions |
| 39 | +import 'package:flutter/material.dart'; |
| 40 | +// Begin custom action code |
| 41 | +// DO NOT REMOVE OR MODIFY THE CODE ABOVE! |
| 42 | +
|
| 43 | +import 'package:flutter/services.dart'; |
| 44 | +
|
| 45 | +Future setStatusbarColor() async { |
| 46 | + // Add your function code here! |
| 47 | +
|
| 48 | + // Set status bar color |
| 49 | + SystemChrome.setSystemUIOverlayStyle( |
| 50 | + SystemUiOverlayStyle( |
| 51 | + statusBarColor: Colors.redAccent, // <-- see here |
| 52 | + statusBarIconBrightness: |
| 53 | + Brightness.dark, //<-- For Android see here (dark icons) |
| 54 | + statusBarBrightness: Brightness.light, //<-- For iOS see here (dark icons) |
| 55 | + ), |
| 56 | + ); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +2. Now click on **Custom Functions** from the [*Navigation Menu*](/getting-started/ui-builder/navigation-menu) (left side of your screen) and open the **Custom Files > main.dart**. |
| 61 | +5. Determine where to place the code (i.e., **Initial Action** or **Final Action**), click the **+** button, and select the custom action. |
| 62 | +8. Click **Save**. |
| 63 | + |
| 64 | +<div class="video-container"><iframe src="https://www.loom. |
| 65 | +com/embed/e8e12fad4fce42bba29080f83a1f4b74?sid=3f01f3b7-38f0-4f01-8bec-6442db66fe07" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 66 | + |
| 67 | + |
| 68 | +## More examples |
| 69 | + |
| 70 | +Let's see some more examples of adding code to the *main.dart* file to solidify understanding and use it in real-world scenarios. |
| 71 | + |
| 72 | +### Example 1: Lock device orientation |
| 73 | + |
| 74 | +You might want to lock the device orientation in portrait or landscape mode to prevent it from rotating the screen automatically when the user tilts or rotates the device. This is useful in scenarios where the layout of the app is designed to work only in one specific orientation or when you want to ensure that the app is always displayed in a consistent manner. |
| 75 | + |
| 76 | +To set the device orientation in landscape-only mode, [create a custom action](/customizing-your-app/custom-functions/custom-actions#adding-custom-action) with the following code and [add it to a *main.dart*](/customizing-your-app/custom-functions/custom-files#edit-main.dart) file. |
| 77 | + |
| 78 | +```dart |
| 79 | +// Automatic FlutterFlow imports |
| 80 | +import '/backend/backend.dart'; |
| 81 | +import '/flutter_flow/flutter_flow_theme.dart'; |
| 82 | +import '/flutter_flow/flutter_flow_util.dart'; |
| 83 | +import '/custom_code/actions/index.dart'; // Imports other custom actions |
| 84 | +import '/flutter_flow/custom_functions.dart'; // Imports custom functions |
| 85 | +import 'package:flutter/material.dart'; |
| 86 | +// Begin custom action code |
| 87 | +// DO NOT REMOVE OR MODIFY THE CODE ABOVE! |
| 88 | +
|
| 89 | +import 'package:flutter/services.dart'; |
| 90 | +
|
| 91 | +Future setLandscapeMode() async { |
| 92 | + // Add your function code here! |
| 93 | + await SystemChrome.setPreferredOrientations([ |
| 94 | + DeviceOrientation.landscapeLeft, |
| 95 | + DeviceOrientation.landscapeRight, |
| 96 | + ]); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +<figure> |
| 101 | +  |
| 102 | + <figcaption class="centered-caption">Edit main.dart file to lock the device orientation</figcaption> |
| 103 | +</figure> |
| 104 | + |
| 105 | + |
| 106 | +### Example 2: Getting app lifecycle callback |
| 107 | + |
| 108 | +If you want to ensure that your app appropriately manages its lifecycle and handles any necessary actions when it transitions between different states, such as 'resumed' and 'paused,' you can add the `AppLifecycleObserver` in the *main.dart* file. |
| 109 | + |
| 110 | +To do so, [create a custom action](/customizing-your-app/custom-functions/custom-actions#adding-custom-action) with the following code and [add it to a *main.dart*](/customizing-your-app/custom-functions/custom-files#edit-main.dart) file. |
| 111 | + |
| 112 | + |
| 113 | +```dart |
| 114 | +// Automatic FlutterFlow imports |
| 115 | +import '/backend/backend.dart'; |
| 116 | +import '/flutter_flow/flutter_flow_theme.dart'; |
| 117 | +import '/flutter_flow/flutter_flow_util.dart'; |
| 118 | +import '/custom_code/actions/index.dart'; // Imports other custom actions |
| 119 | +import '/flutter_flow/custom_functions.dart'; // Imports custom functions |
| 120 | +import 'package:flutter/material.dart'; |
| 121 | +// Begin custom action code |
| 122 | +// DO NOT REMOVE OR MODIFY THE CODE ABOVE! |
| 123 | +
|
| 124 | +Future onAppBackground() async { |
| 125 | +// Add your function code here! |
| 126 | +WidgetsBinding.instance.addObserver(AppLifecycleObserver()); |
| 127 | +} |
| 128 | +
|
| 129 | +class AppLifecycleObserver with WidgetsBindingObserver { |
| 130 | +@override |
| 131 | +void didChangeAppLifecycleState(AppLifecycleState state) { |
| 132 | +if (state == AppLifecycleState.resumed) { |
| 133 | +// Do something when app is resumed |
| 134 | +print('App is in foreground'); |
| 135 | +} else if (state == AppLifecycleState.paused) { |
| 136 | +// Do something when app is paused |
| 137 | +print('App is in background'); |
| 138 | +} |
| 139 | +} |
| 140 | +} |
| 141 | +``` |
| 142 | +<figure> |
| 143 | +  |
| 144 | + <figcaption class="centered-caption">Edit main.dart file to get lifecycle callback</figcaption> |
| 145 | +</figure> |
0 commit comments