On this page14 sections
  1. ضروری معلومات
  2. پچھلے لیکچرز سے تعلق
  3. سیکھنے کے مقاصد
  4. 1. ایک buildTheme فنکشن
  5. 2. textTheme
  6. 3. Component تھیمز
  7. کوڈ کی مثالیں
  8. عام ابتدائی غلطیاں
  9. بہترین طریقے
  10. منی چیلنج
  11. مشق کے سوالات
  12. مشق کے جوابات
  13. اہم نکات
  14. مکمل کوڈ (کاپی پیسٹ حوالہ)

لیکچر 002: متن اور Component تھیمز

کیٹیگری1: تھیم کی بنیادی باتیں
لیکچر002 از 008
ویڈیو کا تخمینی دورانیہ14 منٹ
ٹارگٹFlutter 3.44 · Dart 3.12
سطحابتدائی

ایک colour scheme پیلیٹ طے کرتا ہے۔ Component تھیمز طے کرتے ہیں کہ ہر طرح کا widget کیسا دکھے: ہر AppBar بیچ میں، ہر Card round، ہر بٹن ایک ہی سائز کا۔ انہیں تھیم میں ایک بار style کریں، اور پھر کبھی ایک ایک کر کے style نہیں کرنا پڑے گا۔

مشمولات

ضروری معلومات · پچھلے لیکچرز سے تعلق · سیکھنے کے مقاصد · سبق · کوڈ کی مثالیں · عام ابتدائی غلطیاں · بہترین طریقے · منی چیلنج · مشق کے سوالات · مشق کے جوابات · اہم نکات · مکمل کوڈ


ضروری معلومات

لیکچر 001 (ThemeData، ColorScheme.fromSeed، Theme.of)۔


پچھلے لیکچرز سے تعلق

لیکچر 001 میں، HomeScreen نے خود app bar کا رنگ طے کیا تھا (backgroundColor: colors.primaryContainer)۔ یہاں یہ styling تھیم میں منتقل ہو جاتی ہے، تاکہ widgets سادہ رہیں۔


سیکھنے کے مقاصد

  • تھیم کے کوڈ کو ایک buildTheme() فنکشن میں منتقل کریں
  • textTheme سے text styles کو customise کریں
  • تمام app bars، cards اور بٹنز کو component تھیمز سے style کریں
  • جانیں کہ component تھیمز ...ThemeData classes استعمال کرتی ہیں (CardThemeData)

1. ایک buildTheme فنکشن

تھیمز جلدی بڑھتی ہیں۔ انہیں MaterialApp سے باہر، اپنے ہی فنکشن میں رکھیں:

dart
ThemeData buildTheme() {
  final colors = ColorScheme.fromSeed(seedColor: Colors.teal);

  return ThemeData(
    colorScheme: colors,
    // text and component themes go here
  );
}

پھر MaterialApp(theme: buildTheme(), ...)۔ پہلے colors بنانے سے نیچے موجود component تھیمز وہی پیلیٹ استعمال کر سکتی ہیں۔

2. textTheme

آپ صرف وہی styles درج کرتے ہیں جنہیں بدلنا ہے۔ باقی سب Flutter کے defaults پر رہتا ہے:

dart
textTheme: const TextTheme(
  headlineSmall: TextStyle(fontWeight: FontWeight.bold),
  bodyLarge: TextStyle(fontSize: 17, height: 1.4),
),

آپ کی تبدیلیاں defaults کے ساتھ merge ہوتی ہیں: headlineSmall اپنا عام سائز اور رنگ رکھتا ہے اور صرف bold بن جاتا ہے۔

3. Component تھیمز

ہر widget type کی اپنی تھیم property ہوتی ہے:

dart
appBarTheme: AppBarTheme(
  backgroundColor: colors.primary,
  foregroundColor: colors.onPrimary,   // title and icon colour
  centerTitle: true,
),
cardTheme: CardThemeData(
  elevation: 0,
  color: colors.secondaryContainer,
  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
),
elevatedButtonTheme: ElevatedButtonThemeData(
  style: ElevatedButton.styleFrom(
    backgroundColor: colors.primary,
    foregroundColor: colors.onPrimary,
    padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
  ),
),
Propertyہر… کو style کرتی ہےClass
appBarThemeAppBarAppBarTheme
cardThemeCardCardThemeData
elevatedButtonThemeElevatedButtonElevatedButtonThemeData
inputDecorationThemeTextField decorationInputDecorationThemeData
floatingActionButtonThemeFloatingActionButtonFloatingActionButtonThemeData

اب widgets کو کسی styling کی ضرورت نہیں: AppBar(title: Text('My Notes'))، Card(child: ...)۔


کوڈ کی مثالیں

مثال 1: Component تھیمز کے ساتھ My Notes

lib/main.dart

dart
import 'package:flutter/material.dart';

void main() {
  runApp(const NotesApp());
}

ThemeData buildTheme() {
  final colors = ColorScheme.fromSeed(seedColor: Colors.teal);

  return ThemeData(
    colorScheme: colors,
    textTheme: const TextTheme(
      headlineSmall: TextStyle(fontWeight: FontWeight.bold),
      bodyLarge: TextStyle(fontSize: 17, height: 1.4),
    ),
    appBarTheme: AppBarTheme(
      backgroundColor: colors.primary,
      foregroundColor: colors.onPrimary,
      centerTitle: true,
    ),
    cardTheme: CardThemeData(
      elevation: 0,
      color: colors.secondaryContainer,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        backgroundColor: colors.primary,
        foregroundColor: colors.onPrimary,
        padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
      ),
    ),
  );
}

class NotesApp extends StatelessWidget {
  const NotesApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Notes',
      debugShowCheckedModeBanner: false,
      theme: buildTheme(),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final text = Theme.of(context).textTheme;
    final notes = ['Buy milk and bread', 'Call the plumber', 'Revise Lecture 005'];

    return Scaffold(
      appBar: AppBar(title: const Text('My Notes')),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          Text('Today', style: text.headlineSmall),
          const SizedBox(height: 12),
          for (final note in notes)
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Text(note, style: text.bodyLarge),
              ),
            ),
          const SizedBox(height: 16),
          ElevatedButton(onPressed: () {}, child: const Text('Add note')),
        ],
      ),
    );
  }
}

آپ کیا دیکھیں گے: ایک solid teal app bar جس میں بیچ میں سفید title ہے، ایک bold "Today" heading، تین round flat نوٹ کارڈز اور ایک teal "Add note" بٹن۔ HomeScreen میں کوئی رنگ نہیں ہے؛ سب کچھ buildTheme() سے آتا ہے۔


عام ابتدائی غلطیاں

1. CardThemeData کے بجائے CardTheme استعمال کرنا

dart
ThemeData(cardTheme: const CardTheme(elevation: 0))
code
error • The argument type 'CardTheme' can't be assigned to the parameter type 'CardThemeData?'.

پرانے tutorials CardTheme استعمال کرتے ہیں۔ موجودہ Flutter CardThemeData چاہتا ہے (اور DialogThemeData، TabBarThemeData وغیرہ)۔

2. پھر بھی widget کو style کرنا: Card(color: Colors.white, ...) اس ایک کارڈ کے لیے card تھیم کو override کر دیتا ہے۔ اسے تھیم میں set کریں، جب تک کہ کوئی ایک کارڈ واقعی مختلف ہونا ضروری نہ ہو۔

3. foregroundColor بھول جانا: appBarTheme.backgroundColor کو ایک dark رنگ میں بدلنا بغیر foregroundColor: colors.onPrimary کے، ایک dark bar پر dark title چھوڑ سکتا ہے۔


بہترین طریقے

  • ایک ہی buildTheme() فنکشن، بہتر ہے کہ جیسے یہ بڑھے اپنی فائل (lib/theme.dart) میں ہو۔
  • component تھیمز کو colors.… roles سے بنائیں، fixed رنگوں سے نہیں۔
  • صرف انہی text styles کو override کریں جن کی ضرورت ہو؛ defaults اچھی طرح balanced ہیں۔

منی چیلنج

ایک inputDecorationTheme شامل کریں جو ہر TextField کو ایک filled، round border دے (filled: true، border: OutlineInputBorder(...))، پھر HomeScreen میں ایک TextField شامل کریں جس کی اپنی کوئی decoration نہ ہو اور دیکھیں کہ یہ style خود بخود اپنا لیتا ہے۔


مشق کے سوالات

  1. تھیم کو buildTheme() فنکشن میں منتقل کرنے کی کیا وجہ ہے؟
  2. اگر آپ headlineSmall کے لیے صرف fontWeight set کریں، تو اس کے سائز کو کیا ہوتا ہے؟
  3. کون سی property ہر Card کو style کرتی ہے، اور یہ کون سی class لیتی ہے؟
  4. AppBarTheme میں foregroundColor کیا control کرتا ہے؟
  5. ایک اکیلے کارڈ کو مختلف رنگ چاہیے۔ آپ یہ کہاں set کریں گے؟

مشق کے جوابات

  1. تھیمز لمبی ہو جاتی ہیں؛ ایک فنکشن MaterialApp کو پڑھنے کے قابل رکھتا ہے اور تھیمز کو دوبارہ استعمال کرنے دیتا ہے۔
  2. یہ default سائز ہی رکھتا ہے۔ آپ کا style default کے ساتھ merge ہوتا ہے۔
  3. cardTheme، ایک CardThemeData کے ساتھ۔
  4. title متن اور آئیکنز کا رنگ۔
  5. اسی Card پر (Card(color: ...))؛ تھیم باقی سب کو ویسے ہی style کرتی رہتی ہے۔

اہم نکات

  • textTheme نامزد text styles کو ایڈجسٹ کرتا ہے؛ تبدیلیاں defaults کے ساتھ merge ہوتی ہیں۔
  • Component تھیمز (appBarTheme، cardTheme، elevatedButtonTheme…) اس type کے ہر widget کو style کرتی ہیں۔
  • موجودہ Flutter ...ThemeData classes استعمال کرتا ہے جیسے CardThemeData۔
  • ایک اچھی تھیم کے ساتھ، اسکرینز میں layout ہوتا ہے، رنگ نہیں۔

مکمل کوڈ (کاپی پیسٹ حوالہ)

اس لیکچر تک ایپ کو جو کچھ درکار ہے، سب کچھ۔ ابھی تک صرف ایک فائل ہے۔

lib/main.dart

dart
import 'package:flutter/material.dart';

void main() {
  runApp(const NotesApp());
}

/// Keeps MaterialApp readable as the theme grows; reused wherever a theme is needed.
ThemeData buildTheme() {
  final colors = ColorScheme.fromSeed(seedColor: Colors.teal);

  return ThemeData(
    colorScheme: colors,
    // Only the styles listed here change; everything else keeps Flutter's defaults.
    textTheme: const TextTheme(
      headlineSmall: TextStyle(fontWeight: FontWeight.bold),
      bodyLarge: TextStyle(fontSize: 17, height: 1.4),
    ),
    appBarTheme: AppBarTheme(
      backgroundColor: colors.primary,
      foregroundColor: colors.onPrimary, // title + icon colour
      centerTitle: true,
    ),
    cardTheme: CardThemeData(
      elevation: 0,
      color: colors.secondaryContainer,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
    ),
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        backgroundColor: colors.primary,
        foregroundColor: colors.onPrimary,
        padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
      ),
    ),
  );
}

class NotesApp extends StatelessWidget {
  const NotesApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My Notes',
      debugShowCheckedModeBanner: false,
      theme: buildTheme(),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final text = Theme.of(context).textTheme;
    final notes = ['Buy milk and bread', 'Call the plumber', 'Revise Lecture 005'];

    return Scaffold(
      // No colour set here - appBarTheme already styles every AppBar.
      appBar: AppBar(title: const Text('My Notes')),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          Text('Today', style: text.headlineSmall),
          const SizedBox(height: 12),
          for (final note in notes)
            // No colour or shape set here either - cardTheme supplies both.
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16),
                child: Text(note, style: text.bodyLarge),
              ),
            ),
          const SizedBox(height: 16),
          ElevatedButton(onPressed: () {}, child: const Text('Add note')),
        ],
      ),
    );
  }
}