StatefulWidget Counter App
import "package:flutter/material.dart";
void main()
{
runApp(const MyApp());
}
class MyApp extends StatelessWidget
{
const MyApp({super.key});
@override
Widget build(BuildContext context)
{
return MaterialApp(
title: "Hello",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.indigo,
),
home: const FlutterApp(),
/*"home" decide that which screen will open first.starting Launcher screen will decide from here*/
);
}
}
class FlutterApp extends StatefulWidget {
const FlutterApp({super.key});
/*Here "State" is the class of "StatefulWidget" type."createState()" is the required function in the StatefulWidget.
createState() function required State of class type "State" object*/
@override
State<StatefulWidget> createState() {
return FlutterState();
}
}
/*State<StatefulWidget> createState() => FlutterState();*/
/*This is the another way for denoting the single line return statement using "=>"*/
class FlutterState extends State<FlutterApp>
{
var count = 0;
@override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title: const Text("Hello Flutter"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Counter: $count", style: const TextStyle(fontSize: 34),),
ElevatedButton(
onPressed: (){
setState((){
//count = count + 1; //This is the first way for counter
//count += 1;
count++;
print(count);
},);
},
child: const Text("Click Here!"),
),
/* ElevatedButton(
onPressed: (){
count++; *//*This is the Second way for counter*//*
setState((){ },);
print(count);
},
child: const Text("Click!"),
),*/
],
),
),
);
}
}
Comments
Post a Comment