Start with Flutter – Build Android and iOS Apps Instantly.

By | September 23, 2018

In this course we will learn how to Start with Flutter, Google’s own cross platform solution.

 

 

So I am assuming that you have the flutter SDK and other stuff installed. if not please go to https://flutter.io and download it and setup.

Android Studio

If you are using Android Studio as your primary editor, then you should install the flutter plugins first.

For that

1. Open Android Studio Preferences
2. Go to Plugins.
3. Search for Flutter.
4. Install Flutter
5. Restart Android Studio.
6. Create new Flutter Project.
7. Click Run and your flutter app should be running in the iOS or Android Emulator.

VS Code

If you are using Android Studio as your primary editor, then you should install the flutter and Dart plugins first.

For that

1. Open Extension from the left Menu.
2. Search for ‘Flutter’ and ‘Dart’ and install both.
3. Restart VS Code editor.
4. Go to ‘View’ menu and Select ‘ Command Palette
5. Type ‘>Flutter: New Project’ and Give a name for the Project.
6. Click the Debug icon on the left and click the ‘Settings’ icon in the debug.
7. Select Flutter.
8. Click Run and your flutter app should be running in the iOS or Android Emulator.

Advantages of Flutter

1. Cross Platform – One source code for Android and iOS.
2. Native Compiled code.
3. Hot Reload – Instantly reload the app when you hit ‘Save’ in your editor.
4. Faster Performance like native apps and better performant than other cross platform technologies.

Let’s Start

Once you have created a new Flutter Project, you may have already have a template.

Lets break into simple understandable parts.

Flutter apps always have a main() function where the app starts in the main.dart file.

Lets see how the main.dart file looks like

import 'package:flutter/material.dart';
import 'Pages/init_page.dart';

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

So always look for the main.dart file when you want to see the entry point of the application.

Lets create a new file named init_page.dart in Pages folder.

import 'package:flutter/material.dart';
import 'home.dart';

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or press Run > Flutter Hot Reload in IntelliJ). Notice that the
        // counter didn't reset back to zero; the application is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

The above code is the main entry Widget of the application.

Now we will create out Home page in a new file named home.dart.
You can read through the code to see how the application works.


import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
 
  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  MyHomePage({Key key, this.title}) : super(key: key);

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return new Scaffold(
      appBar: new AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: new Text(widget.title),
      ),
      body: new Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: new Column(
          // Column is also layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug paint" (press "p" in the console where you ran
          // "flutter run", or select "Toggle Debug Paint" from the Flutter tool
          // window in IntelliJ) to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Available Flutter commands:

Command Description
analyze Analyze the project’s Dart code.
attach Attach to a running application..
bash-completion Output command line shell completion setup scripts..
build Flutter build commands..
channel List or switch flutter channels..
clean Delete the build/ directory..
config Configure Flutter settings..
create Create a new Flutter project..
devices List all connected devices..
doctor Show information about the installed tooling..
drive Runs Flutter Driver tests for the current project..
emulators List, launch and create emulators..
format Format one or more dart files..
fuchsia_reload Hot reload on Fuchsia..
help Display help information for flutter..
install Install a Flutter app on an attached device..
logs Show log output for running Flutter apps..
packages Commands for managing Flutter packages..
precache Populates the Flutter tool’s cache of binary artifacts..
run Run your Flutter app on an attached device..
screenshot Take a screenshot from a connected device..
stop Stop your Flutter app on an attached device..
test Run Flutter unit tests for the current project..
trace Start and stop tracing for a running Flutter app..
upgrade Upgrade your copy of Flutter..

One thought on “Start with Flutter – Build Android and iOS Apps Instantly.

  1. Pingback: How to do Unit Test of functions and Widgets in Flutter? – CODERZHEAVEN

Leave a Reply

Your email address will not be published. Required fields are marked *