Material App Widget and Attributes

Material App Widget and Attributes

ยท

2 min read

In Flutter, the MaterialApp widget is the top-level widget that defines the basic material design structure of your application. It sets up the default configurations, themes, and navigation for your app. Here are some key attributes and properties of the MaterialApp widget:

Attributes and Properties:

  1. home: The widget that is the home of the application, typically a Scaffold.

  2. title: The title that appears on the device's system bar.

  3. theme: The overall theme of the application, defining colors, typography, and other visual aspects.

  4. routes: A map of named routes used for navigation within the app.

  5. initialRoute: The initial route the app should navigate to when it starts.

  6. onGenerateRoute: A callback function that allows dynamic generation of routes.

  7. navigatorKey: A key to use when accessing the Navigator from the BuildContext.

  8. navigatorObservers: A list of observers for the Navigator.

  9. debugShowMaterialGrid: A property that shows a grid overlay for material design visual debugging.

Example:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: Text(
          'Welcome to My App!',
          style: TextStyle(fontSize: 20),
        ),
      ),
    );
  }
}

In this example, the MaterialApp widget is used to define the title and theme of the application. The home property is set to an instance of MyHomePage, which is a simple Scaffold with an AppBar and a Center containing a welcome message.

Example:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App Example',
      theme: ThemeData(
        primarySwatch: Colors.green,
        accentColor: Colors.orange,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
      routes: {
        '/second': (context) => SecondPage(),
      },
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Welcome to Material App Example!',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/second');
              },
              child: Text('Go to Second Page'),
            ),
          ],
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Page'),
      ),
      body: Center(
        child: Text(
          'This is the Second Page!',
          style: TextStyle(fontSize: 20),
        ),
      ),
    );
  }
}

In this full example, the MaterialApp widget is used with additional properties such as routes and theme. Two pages (MyHomePage and SecondPage) are defined, and navigation between them is demonstrated using the Navigator.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย