On this page14 sections
لیکچر 002: متن اور Component تھیمز
| کیٹیگری | 1: تھیم کی بنیادی باتیں |
| لیکچر | 002 از 008 |
| ویڈیو کا تخمینی دورانیہ | 14 منٹ |
| ٹارگٹ | Flutter 3.44 · Dart 3.12 |
| سطح | ابتدائی |
ایک colour scheme پیلیٹ طے کرتا ہے۔ Component تھیمز طے کرتے ہیں کہ ہر طرح کا widget کیسا دکھے: ہر
AppBarبیچ میں، ہرCardround، ہر بٹن ایک ہی سائز کا۔ انہیں تھیم میں ایک بار 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 تھیمز
...ThemeDataclasses استعمال کرتی ہیں (CardThemeData)
1. ایک buildTheme فنکشن
تھیمز جلدی بڑھتی ہیں۔ انہیں MaterialApp سے باہر، اپنے ہی فنکشن میں رکھیں:
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 پر رہتا ہے:
textTheme: const TextTheme(
headlineSmall: TextStyle(fontWeight: FontWeight.bold),
bodyLarge: TextStyle(fontSize: 17, height: 1.4),
),آپ کی تبدیلیاں defaults کے ساتھ merge ہوتی ہیں: headlineSmall اپنا عام سائز اور رنگ
رکھتا ہے اور صرف bold بن جاتا ہے۔
3. Component تھیمز
ہر widget type کی اپنی تھیم property ہوتی ہے:
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 |
|---|---|---|
appBarTheme | AppBar | AppBarTheme |
cardTheme | Card | CardThemeData |
elevatedButtonTheme | ElevatedButton | ElevatedButtonThemeData |
inputDecorationTheme | TextField decoration | InputDecorationThemeData |
floatingActionButtonTheme | FloatingActionButton | FloatingActionButtonThemeData |
اب widgets کو کسی styling کی ضرورت نہیں: AppBar(title: Text('My Notes'))، Card(child: ...)۔
کوڈ کی مثالیں
مثال 1: Component تھیمز کے ساتھ My Notes
lib/main.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 استعمال کرنا
ThemeData(cardTheme: const CardTheme(elevation: 0))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 خود بخود اپنا لیتا ہے۔
مشق کے سوالات
- تھیم کو
buildTheme()فنکشن میں منتقل کرنے کی کیا وجہ ہے؟ - اگر آپ
headlineSmallکے لیے صرفfontWeightset کریں، تو اس کے سائز کو کیا ہوتا ہے؟ - کون سی property ہر
Cardکو style کرتی ہے، اور یہ کون سی class لیتی ہے؟ AppBarThemeمیںforegroundColorکیا control کرتا ہے؟- ایک اکیلے کارڈ کو مختلف رنگ چاہیے۔ آپ یہ کہاں set کریں گے؟
مشق کے جوابات
- تھیمز لمبی ہو جاتی ہیں؛ ایک فنکشن
MaterialAppکو پڑھنے کے قابل رکھتا ہے اور تھیمز کو دوبارہ استعمال کرنے دیتا ہے۔ - یہ default سائز ہی رکھتا ہے۔ آپ کا style default کے ساتھ merge ہوتا ہے۔
cardTheme، ایکCardThemeDataکے ساتھ۔- title متن اور آئیکنز کا رنگ۔
- اسی
Cardپر (Card(color: ...))؛ تھیم باقی سب کو ویسے ہی style کرتی رہتی ہے۔
اہم نکات
textThemeنامزد text styles کو ایڈجسٹ کرتا ہے؛ تبدیلیاں defaults کے ساتھ merge ہوتی ہیں۔- Component تھیمز (
appBarTheme،cardTheme،elevatedButtonTheme…) اس type کے ہر widget کو style کرتی ہیں۔ - موجودہ Flutter
...ThemeDataclasses استعمال کرتا ہے جیسےCardThemeData۔ - ایک اچھی تھیم کے ساتھ، اسکرینز میں layout ہوتا ہے، رنگ نہیں۔
مکمل کوڈ (کاپی پیسٹ حوالہ)
اس لیکچر تک ایپ کو جو کچھ درکار ہے، سب کچھ۔ ابھی تک صرف ایک فائل ہے۔
lib/main.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')),
],
),
);
}
}