Skip to content

Instantly share code, notes, and snippets.

@loic-sharma
Created March 12, 2025 21:12
Show Gist options
  • Save loic-sharma/f6427b62fbd41daa13fb2c5b64b7cb49 to your computer and use it in GitHub Desktop.
Save loic-sharma/f6427b62fbd41daa13fb2c5b64b7cb49 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Counter App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Counter Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
- MyHomePage({Key? key, required this.title}) : super(key: key);
+ const MyHomePage({super.key, required this.title});
final String title;
@override
- _MyHomePageState createState() => _MyHomePageState();
+ State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
void _decrementCounter() {
setState(() {
if(_counter > 0){
_counter--;
}
});
}
void _resetCounter(){
setState((){
_counter = 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
- style: Theme.of(context).textTheme.headline4,
+ style: Theme.of(context).textTheme.headlineLarge,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FloatingActionButton(
onPressed: _decrementCounter,
tooltip: 'Decrement',
child: Icon(Icons.remove),
),
SizedBox(width: 20), // Add spacing
FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
],
),
SizedBox(height: 20),
ElevatedButton(onPressed: _resetCounter, child: Text("Reset"))
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment