Count Application with StatefulWidget

 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",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Counter: $count",
style: const TextStyle(fontSize: 34),
),
const SizedBox(height: 50.0),
ElevatedButton(
onPressed: () {
setState(
() {
//count = count + 1; //This is the first way for counter
//count += 1;
count++;
print(count);
},
);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(350.0, 50.0),
),
child: const Text(
"Click Here!",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white),
),
),

/* ElevatedButton(
onPressed: (){
count++; */ /*This is the Second way for counter*/ /*
setState((){ },);
print(count);
},
child: const Text("Click!"),
),*/
],
),
),
);
}
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter