First Counter App in Flutter

in this post we are learning first counter app in flutter.

First Counter App in Flutter

main():

  • main() is function.
  • The main() function will run the MyApp() constructor, like any other programming language where the main function is the entry point to start an application.
  • The main() method calls the runApp() function, which takes a Widget as a parameter.
  • MyApp() is a widget as it extends StatelessWidget. We override the build method to return a MaterialApp().

runApp():

  • The main function calls runApp function which creates a tree of widgets with a root widget.
  • The runApp() function takes the given Widget and makes it the root of the widget tree.
  • MaterialApp is the root widget in the widget tree.

build(): build is the main “rendering logic” for any widget we’ll create, returns what should be displayed on the screen.

BuildContext: BuildContext is unique to each and every widget as it is used to locate the widget inside the widget tree.

MaterialApp:

  • Apply the theme of application.
  • Material is a design system created by Google.
  • Flutter comes bundled with Material Design widgets which provides convenient components to use within the app that will work across Android, iOS, Flutter, and the web. Using MaterialApp() is a common way to build an app, and building without Material Design is not recommended!
  • Does a lot of ‘behind – the – scenes’ setup work for your app.

home:

  • First screen which is launch/show.
  • “home” is a parameter we can set on MaterialApp() and it sets the default route or the ‘MyHomePage’ for the app.

setState:

  • setState is one of state management pattern in flutter.
  • Calls setState() to update the UI. If remove the setState can not data refresh your screen.
  • The State is the information that can be read synchronously when the widget is built and might change during the lifetime of the widget.

Text:

  • Text is widget in flutter.
  • That is read only show text default 0 and floatingActionButton press the button counter is increment called the build method.
  • New value display the Text widget.

floatingActionButton: Round circle button. That is visible bottom right side.

Main 2 widgets in flutter:

StatelessWidget:

  • A stateless widget never changes data.
  • For example: Icon, IconButton, and Text are examples of stateless widgets.
  • Stateless widgets subclass StatelessWidget.
  • These widgets are immutable once they are built i.e any amount of change in the variables, icons, buttons, or retrieving data can not change the state of the app.

StatefulWidget:

  • A stateful widget is dynamic.
  • For example: It can change its appearance in response to events triggered by user interactions or when it receives data.
    Checkbox, Radio, Slider, InkWell, Form, and TextField are examples of stateful widgets. Stateful widgets subclass StatefulWidget.
  • A widget’s state is stored in a State object, separating the widget’s state from its appearance. The state consists of values that can change, like a slider’s current value or whether a checkbox is checked. When the widget’s state changes, the state object calls setState(), telling the framework to redraw the widget.

Scaffold:

  • Used as a frame for a page in your app.
  • Provides a background, app bar, navigation tabs, etc.
  • only use one scaffold per page!.
  • Center:
  • Center is widget display the child widget center text.
  • Column:
  • Column put the widget multiple.
  • For example: Multiple widgets likes Column, Row, Stack, Wrap.
  • Column display widget vertical.

First Counter App in Flutter :

main.dart

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(primarySwatch: Colors.blue),
        home: MyHomePage(title: 'Flutter Demo Home Page'));
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  // 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;

  @override
  _MyHomePageState createState() => _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 Scaffold(
        appBar: 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: Text(widget.title)),
        body: Center(
            // Center is a layout widget. It takes a single child and positions it
            // in the middle of the parent.
            child: Column(
                // Column is also a 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 painting" (press "p" in the console, choose the
                // "Toggle Debug Paint" action from the Flutter Inspector in Android
                // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
                // 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>[
              Text('You have pushed the button this many times:'),
              Text('$_counter', style: Theme.of(context).textTheme.headline4)
            ])),
        floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons
                .add)) // This trailing comma makes auto-formatting nicer for build methods.
        );
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in