Mad Lab
Mad Lab
flutter clean Clears build cache and fixes weird build issues.
flutter build apk Builds a release version of your app for Android.
Format to remember:
main() → runApp() → MaterialApp → Scaffold → AppBar + Body → Widgets
🔹 1. main() Function
This is where your app starts running.
void main() => runApp(MyApp());
🔹 2. MyApp Class
This sets up your whole app — usually a StatelessWidget unless you're changing state.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold( // screen structure
appBar: AppBar(title: Text('Title')),
body: WidgetTree(),
),
);
}
}
🔹 3. Scaffold
🔹 4. AppBar
Gives your app a basic layout — like an app bar, body, floating button, etc.
🔹 5. Body
The top bar of the app — use this for the title.
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
setState(() {
isAlarmOn = !isAlarmOn;
});
},
child: Text(isAlarmOn ? 'Alarm Off' : 'Alarm On'), //button text
),
),
);
}
}
3. Calculator
import 'package:flutter/material.dart';
final buttons = [
"C", "⌫", "%", "/",
"7","8","9","*",
"4", "5", "6", "-",
"1", "2", "3", "+",
"+/-", "0", ".", "=",
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Text(output),
Expanded(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4),
itemCount: buttons.length,
itemBuilder: (context, index) {
return ElevatedButton(
onPressed: () => buttonPressed(buttons[index]),
child: Text(buttons[index]),
);
}, ), ), ], ), ), ); }}
4. 2-D game(ball gesture movement)
import 'package:flutter/material.dart';
void move(DragUpdateDetails d) {
setState(() {
// Directly using d.delta.dx and d.delta.dy for movement
left += d.delta.dx;
top += d.delta.dy;
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onPanUpdate: move, // Detecting drag movement
child: SizedBox.expand(
child: Stack(
children: [
AnimatedPositioned(
duration: Duration(milliseconds: 100),
left: left,
top: top,
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: ballColor, // Change ball color when moved
shape: BoxShape.circle,
),
),
),
Positioned(
top: 40,
left: 20,
child: Text("Score: $score",
style: TextStyle(fontSize: 24, color: Colors.black)),
),
],
),
),
),
);
}
}
6. Develop an application to connect to a web service and to retrieve data with HTTP
Add the http package In your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
http: ^0.13.6
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
@override
void initState() {
super.initState();
getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("API Data")),
body: Center(child: Text(title)),
);
}}
7. Develop a simple shopping application
import 'package:flutter/material.dart';
void purchase() {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text("Purchase Complete"),
content: Text("Total: ₹$total"),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
setState(() => total = 0);
},
child: Text("OK"),
)
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Shop")),
body: Column(
children: [
Text("Total: ₹$total", style: TextStyle(fontSize: 20)),
Column(
children: [
Image.asset("assets/image.jpg", width: 100, height: 100),
Text("Shoes - ₹1500"),
ElevatedButton(
onPressed: () => addItem(1500), child: Text("Add to Cart")),
],
),
Column(
children: [
Image.asset("assets/image.jpg", width: 100, height: 100),
Text("Watch - ₹2500"),
ElevatedButton(
onPressed: () => addItem(2500), child: Text("Add to Cart")),
],
),
Column(
children: [
Image.asset("assets/image.jpg", width: 100, height: 100),
Text("Headphones - ₹1000"),
ElevatedButton(
onPressed: () => addItem(1000), child: Text("Add to Cart")),
],
),
ElevatedButton(
onPressed: purchase,
child: Text("Purchase"),
),
],
),
);
}
}
10. Hangman Game
import 'package:flutter/material.dart';
@override
Widget build(BuildContext context) {
String display = word.split('').map((c) => guessed.contains(c) ? c : "_").join(' ');
bool won = !display.contains("_");
bool lost = tries >= 6;
return Scaffold(
appBar: AppBar(title: Text("Hangman")),
body: Column(
children: [
Text(lost ? "You Lost!" : won ? "You Won!" : display),
Wrap(
children: "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('').map((l) {
return ElevatedButton(
onPressed: guessed.contains(l) || won || lost ? null : () {
//If any of those are true, the button is disabled by setting onPressed: null.
setState(() {
guessed.add(l);
if (!word.contains(l)) tries++;
});
},
child: Text(l),
);
}).toList(), //comes for map(l)
),
Text("Tries left: ${6 - tries}")
],
),
);
}
}
9. Google Maps Integration
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
dependencies:
flutter:
sdk: flutter
flutter_map: ^6.1.0
latlong2: ^0.9.0
8. Push notification
1. Go to firebase console website and create a project and choose parent as licet.ac.in →
continue and disable google analytics → continue and create project
2. Navigate to android studio project → android → app → src → main → AndroidManifest.xml
amd in the first line search for package=com.example.pushnotification
If not found note as → com.example.<appname>
3. In the firebase select android and follow the steps to add commands in files
✅ Procedure (Simplified)
dependencies:
flutter:
sdk: flutter
firebase_core: ^2.27.0
firebase_messaging: ^14.7.9
bash
CopyEdit
flutter pub get
bash
CopyEdit
flutter run -d chrome
✅ Minimal main.dart
dart
CopyEdit
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
FirebaseMessaging.onMessage.listen((message) {
print('Foreground Notification: ${message.notification?.body}');
});
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Push Notifications")),
body: Center(child: Text("Waiting for messages...")),
),
);
}
}