Open In App

Getting Started with Cross-Platform Mobile Application using Flutter

Flutter is an open-source mobile application development SDK created by Google to develop cross-platform mobile applications. Flutter makes it extremely easy and fast for even novice programmers (An computer programmers who is not experienced at any programming languages) .to build high-quality and responsive native mobile apps. No prior app development experience is required! .This article will show you how to build a simple Hello Flutter application and then run it on your own Android device!

Why use Flutter over native Java development?

Flutter has some unique features compared to native Java and other SDK such as:

Setting Up Flutter

Here it is extracted the folder to the C:\ drive

  1. Note: It is recommended to not install Flutter in a directory which may require admin privileges, like ‘C:\Program Files\’

The VS Code Extensions to be installed

  1. Install Git Bash: Optional, but a recommended command prompt. Git Bash provides many common unix commands that are useful for some quick tasks. Download Git Bash for Windows here: Git Downloads
  2. Run Flutter Doctor: Flutter doctor is an inbuilt tool by flutter that can be used to check the status of the Flutter installation. After setting up the PATH variable, you can open up the Command Prompt and execute:
flutter doctor
  1. This will check everything required for Flutter to run and give a detailed report of any error it encounters.

Upon successful setup, flutter doctor would show no errors

Creating an empty template project

  1. Navigate to the place where you want your project to be created. Open a command prompt (you can also use the context ‘Git Bash here’ by right clicking, to open Git Bash in this location) and type in the command for creating a new project:
 flutter create project_name 
  1. For example: for a project named ‘helloflutter’ executing
flutter create helloflutter
  1. will create a new Flutter project with name helloflutter

The new project named helloflutter is created

  1. Open this folder in VS Code. You can right-click and use the context menu to open directly into VS Code, or start VS Code first and then open this folder as a project.

Context menu to open folder in VS Code

  1. The large panel on the left that displays all the files and folders is known as the Explorer Panel. Navigate to ‘lib’ folder and select the ‘main.dart’ file. This file is the entry point from where the app starts its execution.

Locating the main.dart file in the lib folder

  1. The code that opens up is that of the template application. Try running this simple app right away!

Saying Hello Flutter!

  1. Delete the MyHomePage Widget.

Delete the MyHomePage Widget

  1. Create a new Stateless Widget and name it HelloFlutter. Stateless Widgets are used to define Widgets which don’t have to deal with changes to its internal state. They are mostly used to build components which once drawn, are not required to update. 




class HelloFlutter extends StatelessWidget {
  const HelloFlutter({Key? key}) : super(key: key);
 
@override
Widget build(BuildContext context) {
    return Container(
         
    );
}
}

Added the new Stateless Widget HelloFlutter

  1. Replace the Container widget with a Scaffold widget: A Scaffold implements basic material design visual layout structure. This Widget provides APIs for showing drawers, appbars and the body of the app. The body property of the Scaffold will be used here to display the contents of the app. 




class HelloFlutter extends StatelessWidget {
  const HelloFlutter({Key? key}) : super(key: key);
 
  @override
  Widget build(BuildContext context) {
    return const Scaffold(
       
    );
  }
}

  1. Declare a Container Widget in the body of the Scaffold. 
    A Container Widget is a useful widget which combines common painting, positioning, and sizing widgets. You can wrap any widget with a Container and control the above mentioned properties. 




class HelloFlutter extends StatelessWidget {
  const HelloFlutter({Key? key}) : super(key: key);
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
 
      ),
    );
  }
}

  1. The Container widget has an alignment property which will help to position the Widget to the center of the screen. Set the alignment with the Alignment class:
 alignment: Alignment.center 
  1. In the child property of the Container Widget, declare a Text Widget: The Text Widget deals with displaying and handling text. After creating the Text Widget, put in ‘Hello Flutter’ between the parentheses in single quotes. Whatever is put in between the single quotes is displayed by the Text Widget. 




class HelloFlutter extends StatelessWidget {
  const HelloFlutter({Key? key}) : super(key: key);
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.center,
        child: const Text('Hello Flutter!'),
      ),
    );
  }
}

  1. Finally, in the home property of the main My App class above, change it from MyHomePage(…) to HelloFlutter(). This allows the main MyApp class to refer to the Hello Flutter just created.

Changing the home property to call the Widget we made

  1. Now run the app using the ‘flutter run‘ command.

Running the App

  1. A text ‘Hello Flutter!’ will appear written in the middle of the screen.

Running the HelloFlutter App

  1. Connect a physical device to the PC and enable Developer Mode. If the device is successfully recognised by the PC, the device name would appear in the lower-right corner of VS Code.

The device name will be shown here

  1. If you would like to setup an emulator instead, see: Set up the Android emulator. The emulated device would also show up here similarly.
  2. Open the integrated terminal by pressing the key combination [CTRL + `] (Control key + backtick).
  3. Run the command:
flutter run
  1. Wait for a few minutes. As this is first run, some downloads and installation take place in the background related to gradle. Subsequent compilations would be a lot faster.

Executing ‘flutter run’ and compilation of the default app

  1. After compilation, the app will get installed and run on the connected device or emulator automatically.

Running the App

  1. Stop the app by pressing ‘d’ in the terminal. This is what the compilation and running of any app will be like.

Reference: Complete code


Article Tags :