MaterialApp class in Flutter (original) (raw)

Last Updated : 28 Feb, 2025

**MaterialApp Class: MaterialApp is a predefined class or widget in flutter. It is likely the main or core component of a flutter app. The MaterialApp widget provides a wrapper around other Material Widgets. We can access all the other components and widgets provided by Flutter SDK like Text widget, DropdownButton widget, AppBar widget, Scaffold widget, ListView widget, StatelessWidget, StatefulWidget, IconButton widget, TextField widget, Padding widget, ThemeData widget, and many more. Using this widget, we can make an attractive app that follows the Material Design guidelines.

Here is the constructor of the MaterialApp class

**Constructor of MaterialApp class

const MaterialApp(
{
Key key,
GlobalKey navigatorKey,
Widget home,
Map<String, WidgetBuilder> routes: const <String, WidgetBuilder>{},
String initialRoute,
RouteFactory onGenerateRoute,
InitialRouteListFactory onGenerateInitialRoutes,
RouteFactory onUnknownRoute,
List navigatorObservers: const [],
TransitionBuilder builder,
String title: '',
GenerateAppTitle onGenerateTitle,
Color color,
ThemeData theme,
ThemeData darkTheme,
ThemeData highContrastTheme,
ThemeData highContrastDarkTheme,
ThemeMode themeMode: ThemeMode.system,
Locale locale,
Iterable localizationsDelegates,
LocaleListResolutionCallback localeListResolutionCallback,
LocaleResolutionCallback localeResolutionCallback,
Iterable supportedLocales: const [Locale('en', 'US')],
bool debugShowMaterialGrid: false,
bool showPerformanceOverlay: false,
bool checkerboardRasterCacheImages: false,
bool checkerboardOffscreenLayers: false,
bool showSemanticsDebugger: false,
bool debugShowCheckedModeBanner: true,
Map<LogicalKeySet, Intent> shortcuts,
Map<Type, Action> actions}
)

**Properties of MaterialApp widget

Property Description
**actions This property takes in _Map<Type, Action> as the object. It controls intent keys.
checkerboardRasterCacheImages This property takes in a boolean as the object. If set to true it turns on the checkerboarding of raster cache images.
**color It controls the primary color used in the application.
**darkTheme It provided theme data for the dark theme for the application.
**debugShowCheckedModeBanner This property takes in a _boolean as the object to decide whether to show the debug banner or not.
**debugShowMaterialGird This property takes a boolean as the object. If set to true it paints a baseline grid material app.
**highContrastDarkTheme It provided the theme data to use for the high contrast theme.
**home This property takes in _widget as the object to show on the default route of the app.
**initialRoute This property takes in a _string as the object to give the name of the first route in which the navigator is built.
**locale It provides a locale for the _MaterialApp.
**localizationsDelegates This provides a delegate for the locales.
**navigatorObservers This property holds _List as the object to create a list of observers for the navigator.
**onGenerateInitialRoutes This property takes in _InitialRouteListFactory typedef as the object to generate initial routes.
**onGenerateRoute The _onGenerateRoute takes in a _RouteFactory as the object. It is used when the app is navigated to a named route.
**onGenerateTitle This property takes in _RouteFactory typedef as the object to generate a title string for the application if provided.
**onUnknownRoute The _onUnknownRoute takes in _RouteFactory typedef as the object to provide a route in case of failure in other method.
**routes The routes property takes in L__ogicalKeySet class as the object to control the app's topmost level routing.
**shortcuts This property takes in _LogicalKeySet class as the object to decide the keyboard shortcut for the application.
**showPerformanceOverlay The _showPerformanceOverlay takes in a _boolean value as the object to turn on or off performance overlay.
**showSemanticsDebugger This property takes in a _boolean as the object. If set to true, it shows some accessible information.
**supportedLocales The _supportedLocales property keeps hold of the locals used in the app by taking in _Iterable class as the object.
**theme This property takes in _ThemeData class as the object to describe the theme for the MaterialApp.
**themeMode This property holds _ThemeMode enum as the object to decide the theme for the material app.
**title The _title property takes in a _string as the object to decide the one-line description of the app for the device.

**Simple Material App Example:

Here is very simple code in dart language to make a screen that has an AppBar title as GeeksforGeeks.

Dart `

import 'package:flutter/material.dart';

// The main function is the entry point of the application void main() { runApp(const GFGapp()); }

// GFGapp is a stateless widget that represents the application class GFGapp extends StatelessWidget { const GFGapp({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { return MaterialApp( // Title of the application title: 'GeeksforGeeks', // Theme of the application theme: ThemeData(primarySwatch: Colors.green), // Dark theme of the application darkTheme: ThemeData(primarySwatch: Colors.grey), // Color of the application color: Colors.amberAccent, // Supported locales for the application supportedLocales: {const Locale('en', ' ')}, // Disable the debug banner debugShowCheckedModeBanner: false,

  // Home screen of the application
  home: Scaffold(
    appBar: AppBar(
      // Title of the app bar
      title: const Text('GeeksforGeeks'),
      // Background color of the app bar
      backgroundColor: Colors.green,
    ),
  ),
);

} }

`

**Output:

Material_App

**Output explanation:

Conclusion

In this article we looked at MaterialApp widget. It directly and indirectly wraps all other Material widgets such as Scaffold, AppBar, Drawer, etc. You can create an app with Material Design Specifications using the MaterialApp widget. We also looked at the constructor and properties of the MaterialApp widget in order to understand what we can achieve using the MaterialApp widget in Flutter.