...

/

Running a Sample Project in Android Widget

Running a Sample Project in Android Widget

Selecting flutter framework in android widget

In this lesson, we will set up an Android widget to run a basic Flutter App. In the previous lesson, it was discussed that we have 5 supported frameworks.

Hence the first step is to select the Flutter framework after opening an Android widget. As by default Java is selected.

Available Frameworks
Available Frameworks

Setting up our application

After selecting Flutter you will see a slight change in the Editor view of the Android Widget. Now instead of Java we see "Add Hello World for Flutter"

Selecting Sample App from Android Widget Editor View
Selecting Sample App from Android Widget Editor View

By default, we are in our project's root directory. We can add files and folders as required, and can also add a custom run script, a docker job, and other options that can be explored too.

Upon pressing the "Add Hello World for Flutter" button at the top you will see multiple folders and files appear.

package com.example.helloworld;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}
Hello World Sample App Code

Application details

The main.dart file and the  pubspec.yaml  file seems to appear in the code editor. These are the two main files in Flutter in this sample application. main.dart contains the Flutter Hello World message within the application, a text box in the middle of the main screen with the text 'Number of clicks: ' and a  +  sign button and the bottom right to increment this text box. pubspec.yaml is the file that is included in every Flutter application and it is where we add the metadata of our application, as each pub package need to define its dependencies and that is included in this file.

Let’s run this code

package com.example.helloworld;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}
Hello World Flutter Sample Application

Access this course and 1200+ top-rated courses and projects.