How to make Increment Counter App In Flutter (Hybrid App) (IOS)
Firstly Install the Flutter Setup and start new project .
Right Click on Lib folder and make new Dart File .
Use Widget to start .
Paste the following code:
//Library
import 'package:flutter/material.dart';
//Main function Start
void main() {
runApp(const MyApp());
}
//Using State less widget
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
//Start the material App
return MaterialApp(
//remove the debug line
debugShowCheckedModeBanner: false,
//give name to website with title widget
title: 'Flutter Demo',
//apply theme to app
theme: ThemeData(
primarySwatch: Colors.yellow,
),
//link the file with MyHomePage and title to AppBar
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
//Using the State ful widget to set the state of app in frontend
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
//initialize the title variable
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
//initialize the counter variable
int _counter = 0;
//make a function to set the state of counter
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
//Start building App with Scaffold
return Scaffold(
//make a appbar
appBar: AppBar(
//give title to app bar
title: Text(widget.title),
),
//centralize the content by Center widget
body: Center(
child: Column(
//mainaxisalignment is used to centralize the text or content
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
//use the text widget
const Text(
'You have pushed the button this many times:',
),
Text(
//pass the counter variable
'$_counter',
//apply theme to counter Text
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
//make a Action button to press
floatingActionButton: FloatingActionButton(
//use onPressed function.
onPressed: _incrementCounter,
//by holding the floating button long it will show tooltip text
tooltip: 'Increment',
//Give an icon to floating button
child: const Icon(Icons.add),
),
);
}
}