ThemeData هي واحدة من أهم المكونات في Flutter Material التي تساعد على تعريف مظهر التطبيق. تسمح ThemeData للمطورين بتعريف مجموعة من الألوان والخطوط والتنسيقات الأخرى التي تستخدم في التصميم العام للتطبيق.
ما هو ThemeData في فلاتر؟
يعد ThemeData كائنًا class يحتوي على مجموعة من الخصائص التي تحدد مظهر التطبيق. يتم استخدام هذه الخصائص لتعريف الألوان والخطوط والتنسيقات الأخرى التي تستخدم في التصميم العام للتطبيق.
“اقرأ أيضاً: شرح استخدام ويدجت ListTile في فلاتر“
خصائص ThemeData في فلاتر
تحتوي ThemeData على العديد من الخصائص التي يمكن استخدامها لتعريف مظهر التطبيق. فيما يلي قائمة بجميع خصائص ThemeData وشرح لكل منها بالتفصيل مع أمثلة للاستخدام:
primaryColor
هذه الخاصية تحدد اللون الأساسي للتطبيق. يتم استخدام هذا اللون كخلفية لشريط التطبيق ولأي عناصر أخرى تستخدم هذا اللون.
ThemeData( primaryColor: Colors.blue, )
accentColor
هذه الخاصية تحدد اللون الفرعي للتطبيق. يتم استخدام هذا اللون لتحديد لون بعض العناصر الفرعية مثل الأزرار والشعار.
ThemeData( accentColor: Colors.green, )
“اقرأ أيضاً: شرح استخدام ويدجت AlertDialog في فلاتر“
canvasColor
هذه الخاصية تحدد لون الخلفية للتطبيق. يتم استخدام هذا اللون كخلفية لأي عناصر تستخدم هذا اللون مثل البطاقات card والحاويات.
ThemeData( canvasColor: Colors.white, )
scaffoldBackgroundColor
هذه الخاصية تحدد لون خلفية Scaffold (الإطار). يتم استخدام هذا اللون كخلفية للصفحات ولأي عناصر تستخدم هذا اللون.
ThemeData( scaffoldBackgroundColor: Colors.grey, )
cardColor
هذه الخاصية تحدد لون البطاقة card. يتم استخدام هذا اللون كخلفية للبطاقات ولأي عناصر تستخدم هذا اللون.
ThemeData( cardColor: Colors.white, )
dividerColor
هذه الخاصية تحدد لون الخط الفاصل بين العناصر divider. يتم استخدام هذا اللون للخطوط الفاصلة بين العناصر.
ThemeData( dividerColor: Colors.black, )
textTheme
هذه الخاصية تحدد مظهر النص في التطبيق. يمكن استخدام TextTheme لتحديد الخط وحجمه وأسلوبه ولونه.
ThemeData( textTheme: TextTheme( headline1: TextStyle(fontSize: 32.0, fontWeight: FontWeight.bold), headline2: TextStyle(fontSize: 24.0, fontWeight: FontWeight.w600), bodyText1: TextStyle(fontSize: 16.0), bodyText2: TextStyle(fontSize: 14.0), ), )
“اقرأ أيضاً: شرح استخدام ويدجت StreamBuilder في فلاتر“
appBarTheme
هذه الخاصية تحدد مظهر شريط التطبيق appBar. يمكن استخدام AppBarTheme لتحديد لون الخلفية ولون النص وحجم الخط وأسلوب النص.
ThemeData( appBarTheme: AppBarTheme( backgroundColor: Colors.blue, foregroundColor: Colors.white, titleTextStyle: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold), ), )
buttonTheme
هذه الخاصية تحدد مظهر الأزرار في التطبيق. يمكن استخدام ButtonTheme لتحديد الألوان وحجم الخط وأسلوب النص وحجم الأزرار.
ThemeData( buttonTheme: ButtonThemeData( buttonColor: Colors.blue, textTheme: ButtonTextTheme.primary, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)), ), )
inputDecorationTheme
هذه الخاصية تحدد مظهر حقول الإدخال في التطبيق. يمكن استخدام InputDecorationTheme لتحديد لون الخلفية ولون الحدود وأسلوب الحدود.
ThemeData( inputDecorationTheme: InputDecorationTheme( filled: true, fillColor: Colors.grey[200], border: OutlineInputBorder( borderRadius: BorderRadius.circular(10.0), borderSide: BorderSide.none, ), ), )
tooltipTheme
هذه الخاصية تحدد مظهر أدوات التلميح في التطبيق. يمكن استخدام TooltipTheme لتحديد لون الخلفية ولون النص وحجم الخط وأسلوب النص.
ThemeData( tooltipTheme: TooltipThemeData( decoration: BoxDecoration( color: Colors.black87, borderRadius: BorderRadius.circular(5.0), ), textStyle: TextStyle(color: Colors.white), ), )
“اقرأ أيضاً: شرح استخدام ويدجت SafeArea في فلاتر“
مثال على استخدام ThemeData في فلاتر
يمكن استخدام ThemeData في أي تطبيق Flutter لتعريف مظهر التطبيق. فيما يلي مثال كامل على استخدام ThemeData لتعريف مظهر التطبيق:
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'My App', theme: ThemeData( primarySwatch: Colors.deepPurple, textTheme: const TextTheme( displayLarge: TextStyle(fontSize: 32.0, fontWeight: FontWeight.bold), displayMedium: TextStyle(fontSize: 24.0, fontWeight: FontWeight.w600), bodyLarge: TextStyle(fontSize: 16.0), bodyMedium: TextStyle(fontSize: 14.0), ), elevatedButtonTheme: ElevatedButtonThemeData( style: ElevatedButton.styleFrom( foregroundColor: Colors.white, backgroundColor: Colors.deepPurple, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10.0)), ), ), inputDecorationTheme: InputDecorationTheme( filled: true, fillColor: Colors.grey[200], border: OutlineInputBorder( borderRadius: BorderRadius.circular(10.0), borderSide: BorderSide.none, ), hintStyle: const TextStyle(color: Colors.grey), ), appBarTheme: const AppBarTheme( backgroundColor: Colors.deepPurple, foregroundColor: Colors.white, elevation: 0.0, iconTheme: IconThemeData(color: Colors.white), titleTextStyle: TextStyle( fontSize: 20.0, fontWeight: FontWeight.bold, color: Colors.white), ), textButtonTheme: TextButtonThemeData( style: TextButton.styleFrom( foregroundColor: Colors.deepPurple, textStyle: const TextStyle(fontSize: 18.0), ), ), cardTheme: CardTheme( color: Colors.grey[200], shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)), elevation: 2.0, ), dividerTheme: const DividerThemeData( thickness: 2.0, color: Colors.grey, ), scaffoldBackgroundColor: Colors.white, bottomNavigationBarTheme: const BottomNavigationBarThemeData( selectedItemColor: Colors.deepPurple, unselectedItemColor: Colors.grey, backgroundColor: Colors.white, elevation: 4.0, ), ), home: const MyHomePage(), ); } } class MyHomePage extends StatelessWidget { const MyHomePage({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('My App Home'), actions: <Widget>[ IconButton( icon: const Icon(Icons.search), onPressed: () {}, ), IconButton( icon: const Icon(Icons.more_vert), onPressed: () {}, ), ], ), body: Column( children: <Widget>[ const SizedBox(height: 20.0), Text( 'Welcome to my app!', style: Theme.of(context).textTheme.displayLarge, ), const SizedBox(height: 20.0), Container( padding: const EdgeInsets.all(20.0), margin: const EdgeInsets.all(20.0), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(10.0), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2.0, blurRadius: 5.0, offset: const Offset(0, 3), ), ], ), child: Column( children: <Widget>[ TextFormField( decoration: const InputDecoration( labelText: 'Username', prefixIcon: Icon(Icons.person), ), ), const SizedBox(height: 20.0), TextFormField( decoration: const InputDecoration( labelText: 'Password', prefixIcon: Icon(Icons.lock), ), ), const SizedBox(height: 20.0), ElevatedButton( onPressed: () {}, child: const Text('Login'), ), const SizedBox(height: 20.0), TextButton( onPressed: () {}, child: const Text('Forgot Password?'), ), ], ), ), ], ), bottomNavigationBar: BottomNavigationBar( items: const <BottomNavigationBarItem>[ BottomNavigationBarItem( icon: Icon(Icons.home), label: 'Home', ), BottomNavigationBarItem( icon: Icon(Icons.favorite), label: 'Favorites', ), BottomNavigationBarItem( icon: Icon(Icons.person), label: 'Profile', ), ], ), ); } }

في هذا المثال، تم استخدام العديد من الخصائص في ThemeData لتعريف مظهر التطبيق. تم تعريف مظهر الألوان الأساسي باستخدام primarySwatch. تم تعريف الأنماط الأخرى مثل elevatedButtonTheme وinputDecorationTheme وappBarTheme وtextButtonTheme وcardTheme وdividerTheme وbottomNavigationBarTheme. تم استخدام هذه الخصائص في العناصر المختلفة في التطبيق مثل شريط التطبيق وحقول الإدخال والأزرار والبطاقات والفواصل وشريط التنقل السفلي. ويظهر تأثير هذه الخصائص في تحسين مظهر التطبيق بشكل عام.
“اقرأ أيضاً: كيفية تثبيت Flutter على نظام ويندوز 2023“
في هذا المقال، تعرفنا على استخدام ThemeData في Flutter وشرحنا جميع خصائصها بالتفصيل. يمكن استخدام هذه الخصائص لتعريف مظهر التطبيق وجعله يبدو أكثر جاذبية واحترافية. ويمكنك استخدام هذه الخصائص في أي تطبيق Flutter تريد إنشاؤه.