Our Code Hub

Explained code snippets and concepts to help developers build better apps — engineered by Mohsin Sattar Portfolio.

Advertisement
📌 What is a Widget?

In Flutter, EVERYTHING is a Widget. A widget is a description of part of your UI. Widgets form a tree structure — parent and child widgets work together to build the complete UI.

main.dart — StatelessWidget
// 🎯 A simple Flutter app — Hello World!
import 'package:flutter/material.dart';

// Entry point of every Flutter app
void main() {
  runApp(const MyApp());
}

// StatelessWidget: UI that never changes
class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My First App',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.cyan),
      ),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Hello Flutter!')),
      body: const Center(child: Text('Welcome to Flutter! 🚀', style: TextStyle(fontSize: 24))),
    );
  }
}
counter.dart — StatefulWidget
// 🔄 StatefulWidget: UI that CAN change
class CounterScreen extends StatefulWidget {
  const CounterScreen({super.key});
  @override
  State<CounterScreen> createState() => _CounterScreenState();
}

class _CounterScreenState extends State<CounterScreen> {
  int _count = 0;
  void _increment() {
    setState(() { _count++; });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('Count: $_count', style: const TextStyle(fontSize: 32)),
          ElevatedButton(onPressed: _increment, child: const Text('Tap Me! 👆')),
        ],
      )),
    );
  }
}
🔥 Why Firebase?

Firebase is Google's backend platform. It gives Flutter apps real-time database (Firestore), authentication, file storage, cloud functions, and analytics — all without managing a server.

firebase_auth.dart — Sign In
// 🔐 Firebase Authentication in Flutter
import 'package:firebase_auth/firebase_auth.dart';

class AuthService {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<UserCredential?> signIn(String email, String password) async {
    try {
      return await _auth.signInWithEmailAndPassword(email: email, password: password);
    } on FirebaseAuthException catch (e) {
      print('Auth Error: ${e.message}');
      return null;
    }
  }

  Future<void> signOut() async {
    await _auth.signOut();
  }

  Stream<User?> get authStateChanges => _auth.authStateChanges();
}
⚙️ State Management — GetX

GetX is one of the most popular state management solutions for Flutter. It's fast, reactive, and super easy to use.

counter_controller.dart — GetX
// 📦 GetX Controller
import 'package:get/get.dart';

class CounterController extends GetxController {
  var count = 0.obs;
  void increment() => count++;
}

class MyScreen extends StatelessWidget {
  final ctrl = Get.put(CounterController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Obx(() => Center(child: Text('Count: ${ctrl.count}'))),
      floatingActionButton: FloatingActionButton(
        onPressed: ctrl.increment,
        child: const Icon(Icons.add),
      ),
    );
  }
}
🎨 Beautiful Flutter UI Tips

Great UI separates good apps from GREAT apps.

neon_card.dart — Glassmorphism
// 💎 Glassmorphism Card in Flutter
import 'dart:ui';

Widget glassCard({required Widget child}) {
  return ClipRRect(
    borderRadius: BorderRadius.circular(16),
    child: BackdropFilter(
      filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white.withOpacity(0.08),
          borderRadius: BorderRadius.circular(16),
          border: Border.all(color: Colors.white.withOpacity(0.15)),
          boxShadow: [BoxShadow(color: Colors.cyan.withOpacity(0.2), blurRadius: 20, spreadRadius: 2)],
        ),
        padding: const EdgeInsets.all(20),
        child: child,
      ),
    ),
  );
}
Advertisement
Advertisement