English 中文(简体)
Creating Simple Application in Android Studio
  • 时间:2024-09-08

Creating Simple Apppcation in Android Studio


Previous Page Next Page  

In this chapter, let us create a simple Flutter apppcation to understand the basics of creating a flutter apppcation in the Android Studio.

Step 1 − Open Android Studio

Step 2 − Create Flutter Project. For this, cpck File → New → New Flutter Project

New Flutter Project

Step 3 − Select Flutter Apppcation. For this, select Flutter Apppcation and cpck Next.

Flutter Apppcation Next

Step 4 − Configure the apppcation as below and cpck Next.

    Project name: hello_app

    Flutter SDK Path: <path_to_flutter_sdk>

    Project Location: <path_to_project_folder>

    Description: Flutter based hello world apppcation

Project Name

Step 5 − Configure Project.

Set the company domain as flutterapp.tutorialspoint.com and cpck Finish.

Step 6 − Enter Company domain.

Android Studio creates a fully working flutter apppcation with minimal functionapty. Let us check the structure of the apppcation and then, change the code to do our task.

The structure of the apppcation and its purpose is as follows −

Structure Apppcation

Various components of the structure of the apppcation are explained here −

    android − Auto generated source code to create android apppcation

    ios − Auto generated source code to create ios apppcation

    pb − Main folder containing Dart code written using flutter framework

    ib/main.dart − Entry point of the Flutter apppcation

    test − Folder containing Dart code to test the flutter apppcation

    test/widget_test.dart − Sample code

    .gitignore − Git version control file

    .metadata − auto generated by the flutter tools

    .packages − auto generated to track the flutter packages

    .iml − project file used by Android studio

    pubspec.yaml − Used by Pub, Flutter package manager

    pubspec.lock − Auto generated by the Flutter package manager, Pub

    README.md − Project description file written in Markdown format

Step 7 − Replace the dart code in the pb/main.dart file with the below code −


import  package:flutter/material.dart ;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
   // This widget is the root of your apppcation.
   @override
   Widget build(BuildContext context) {
      return MaterialApp(
         title:  Hello World Demo Apppcation ,
         theme: ThemeData(
            primarySwatch: Colors.blue,
         ),
         home: MyHomePage(title:  Home page ),
      );
   }
}
class MyHomePage extends StatelessWidget {
   MyHomePage({Key key, this.title}) : super(key: key);
   final String title;

   @override
   Widget build(BuildContext context) {
      return Scaffold(
         appBar: AppBar(
            title: Text(this.title),
         ),
         body: Center(
            child:
            Text(
                Hello World ,
            )
         ),
      );
   }
}

Let us understand the dart code pne by pne.

    Line 1 − imports the flutter package, material. The material is a flutter package to create user interface according to the Material design guidepnes specified by Android.

    Line 3 − This is the entry point of the Flutter apppcation. Calls runApp function and pass it an object of MyApp class. The purpose of the runApp function is to attach the given widget to the screen.

    Line 5-17 − Widget is used to create UI in flutter framework. StatelessWidget is a widget, which does not maintain any state of the widget. MyApp extends StatelessWidget and overrides its build method. The purpose of the build method is to create a part of the UI of the apppcation. Here, build method uses MaterialApp, a widget to create the root level UI of the apppcation. It has three properties - title, theme and home.

      title is the title of the apppcation

      theme is the theme of the widget. Here, we set blue as the overall color of the apppcation using ThemeData class and its property, primarySwatch.

      home is the inner UI of the apppcation, which we set another widget, MyHomePage

    Line 19 - 38MyHomePage is same as MyApp except it returns Scaffold Widget. Scaffold is a top level widget next to MaterialApp widget used to create UI conforming material design. It has two important properties, appBar to show the header of the apppcation and body to show the actual content of the apppcation. AppBar is another widget to render the header of the apppcation and we have used it in appBar property. In body property, we have used Center widget, which centers it child widget. Text is the final and inner most widget to show the text and it is displayed in the center of the screen.

Step 8 − Now, run the apppcation using, Run → Run main.dart

Main Dart

Step 9 − Finally, the output of the apppcation is as follows −

Home Page Advertisements