💻 آخرین فرصت یادگیری برنامه‌نویسی با آفر ویژه قبل از افزایش قیمت در ۵ آذر ماه (🎁 به همراه یک هدیه ارزشمند )
۰ ثانیه
۰ دقیقه
۰ ساعت
۲ Mahyar
مشکل در تغیر تم دارک و لایت
یلدا محصلی حل شده توسط یلدا محصلی

سلام دوستان، تم در اجرای برنامه دارکه و با فشردن کلید تم تغیر نمیکنه. اگه کسی میتونه لطفا راهنمایی کنه منو

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
void main() {
  runApp(const MyApp());
}
class MyApp extends StatefulWidget {
  const MyApp({super.key});
  @override
  State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  ThemeMode _themeMode = ThemeMode.light;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: _themeMode == ThemeMode.dark
          ? MyAppThemeConfig.dark().getTheme()
          : MyAppThemeConfig.light().getTheme(),
      home: MyHomePage(
        toggleThemeMode: () {
          setState(() {
            _themeMode == ThemeMode.dark
                ? _themeMode = ThemeMode.light
                : _themeMode = ThemeMode.dark;
          });
        },
      ),
    );
  }
}
class MyAppThemeConfig {
  final Color primaryColor = Colors.pink.shade400;
  final Color primaryTextColor;
  final Color secondaryTextColor;
  final Color surfaceColor;
  final Color backgroundColor;
  final Color appBarColor;
  final Brightness brightness;
  MyAppThemeConfig.dark()
      : primaryTextColor = Colors.white,
        secondaryTextColor = Colors.white70,
        surfaceColor = const Color(0x0dffffff),
        backgroundColor = const Color.fromARGB(225, 30, 30, 30),
        appBarColor = Colors.black,
        brightness = Brightness.dark;
  MyAppThemeConfig.light()
      : primaryTextColor = Colors.grey.shade900,
        secondaryTextColor = Colors.grey.shade900.withOpacity(0.8),
        surfaceColor = const Color(0x0d000000),
        backgroundColor = Colors.white,
        appBarColor = const Color.fromARGB(255, 235, 235, 235),
        brightness = Brightness.light;
  ThemeData getTheme() {
    return ThemeData(
      primarySwatch: Colors.pink,
      primaryColor: primaryColor,
      brightness: brightness,
      dividerColor: surfaceColor,
      elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
              backgroundColor: Colors.pinkAccent,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8)))),
      scaffoldBackgroundColor: backgroundColor,
      appBarTheme: AppBarTheme(
          backgroundColor: appBarColor, foregroundColor: primaryTextColor),
      inputDecorationTheme: InputDecorationTheme(
          filled: true,
          fillColor: surfaceColor,
          border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
              borderSide: BorderSide.none)),
      textTheme: GoogleFonts.latoTextTheme(TextTheme(
          bodyMedium: TextStyle(fontSize: 15, color: primaryTextColor),
          bodyLarge: TextStyle(fontSize: 15, color: secondaryTextColor),
          titleLarge:
              TextStyle(fontWeight: FontWeight.w900, color: primaryTextColor),
          titleMedium: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: primaryTextColor))),
    );
  }
}
class MyHomePage extends StatefulWidget {
  final Function toggleThemeMode;
  const MyHomePage({super.key, required this.toggleThemeMode});
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
enum Favorite {
  asuraScans,
  voidScans,
  elarcToon,
  manhuaUs,
  resetScans,
}
class _MyHomePageState extends State<MyHomePage> {
  Favorite _selectedFavorite = Favorite.asuraScans;
  bool like = false;
  bool more = false;
  List<int> shadowCol = [
    0xff6e32a6,
    0xffBB0045,
    0xffF9C943,
    0xff620711,
    0xff0DFFFD
  ];
  List<double> imageScale = [6.0, 6.0, 4.0, 3.0, 4.0];
  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Curriculum Vitae'),
        actions: [
          IconButton(
            onPressed: () {},
            icon: const Icon(CupertinoIcons.chat_bubble),
          ),
          IconButton(
            onPressed: () {
              widget.toggleThemeMode;
            },
            icon: const Icon(CupertinoIcons.ellipsis_vertical),
          ),
        ],
      ),
      body: SingleChildScrollView(
        physics: const BouncingScrollPhysics(),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 30),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 15.0),
              child: Row(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(8),
                    child: Image.asset(
                      'assets/profile/1.jpg',
                      width: 100,
                      height: 100,
                    ),
                  ),
                  const SizedBox(width: 15),
                  SizedBox(
                    width: width / 1.76,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('Mahyar Bayat',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: Theme.of(context).textTheme.titleLarge),
                        Text("Manhua Fan",
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: Theme.of(context).textTheme.titleMedium),
                        Row(
                          children: [
                            const Icon(
                              CupertinoIcons.location_solid,
                            ),
                            Text('Iran / Tehran',
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: Theme.of(context).textTheme.bodyLarge),
                          ],
                        ),
                      ],
                    ),
                  ),
                  const Spacer(),
                  IconButton(
                    onPressed: () {
                      setState(() {
                        like = !like;
                      });
                    },
                    icon: Icon(
                      like ? CupertinoIcons.heart_fill : CupertinoIcons.heart,
                      color: Colors.pinkAccent,
                      size: 30,
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 30),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 15.0),
              child: Text(
                "As a manhua fan, I, immersing myself in the rich tapestry of Chinese comics with unyielding passion. My extensive collection reflects my unwavering dedication to the captivating worlds and intricate storytelling found within the pages.",
                maxLines: 20,
                overflow: TextOverflow.ellipsis,
                style: Theme.of(context).textTheme.bodyLarge,
              ),
            ),
            const SizedBox(height: 15),
            const Divider(color: Colors.black26),
            const SizedBox(height: 8),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 15.0),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    'Favorite sources',
                    style: Theme.of(context).textTheme.titleMedium,
                  ),
                  IconButton(
                    onPressed: () => setState(() => more = !more),
                    icon: Icon(
                      more
                          ? Icons.keyboard_arrow_down
                          : Icons.keyboard_arrow_up,
                      size: 18,
                    ),
                  ),
                ],
              ),
            ),
            more
                ? Center(
                    child: Wrap(
                      direction: Axis.horizontal,
                      spacing: width / 50,
                      runSpacing: width / 50,
                      children: Favorite.values.map((favorite) {
                        final index = favorite.index;
                        final imageSca = imageScale[index];
                        final shadowColor = Color(shadowCol[index]);
                        return FavoriteSources(
                          width: width,
                          imagePath: 'assets/favorites/${index + 1}.png',
                          name: favorite
                              .toString()
                              .split('.')
                              .last
                              .replaceAllMapped(RegExp(r'([A-Z])'),
                                  (match) => ' ${match.group(0)}')
                              .replaceFirstMapped(RegExp(r'^[a-z]'),
                                  (match) => match.group(0)!.toUpperCase()),
                          imageScale: imageSca,
                          shadowColor: shadowColor,
                          isActive: _selectedFavorite == favorite,
                          onTap: () {
                            setState(() {
                              _selectedFavorite = favorite;
                            });
                          },
                        );
                      }).toList(),
                    ),
                  )
                : const SizedBox(),
            const Divider(color: Colors.black26),
            Padding(
              padding: const EdgeInsets.all(15),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    'Personal information',
                    style: Theme.of(context).textTheme.titleMedium,
                  ),
                  const SizedBox(height: 15),
                  TextField(
                    decoration: InputDecoration(
                        prefixIcon: const Icon(CupertinoIcons.at),
                        label: Text('Email',
                            style: Theme.of(context).textTheme.titleSmall)),
                  ),
                  const SizedBox(height: 15),
                  TextField(
                    decoration: InputDecoration(
                        prefixIcon: const Icon(CupertinoIcons.lock),
                        label: Text('Password',
                            style: Theme.of(context).textTheme.titleSmall)),
                  ),
                  const SizedBox(height: 15),
                  SizedBox(
                    width: double.infinity,
                    height: 65,
                    child: ElevatedButton(
                        onPressed: () {},
                        child: Center(
                            child: Text(
                          'Save',
                          style: Theme.of(context).textTheme.titleMedium,
                        ))),
                  )
                ],
              ),
            ),
            const SizedBox(height: 50)
          ],
        ),
      ),
    );
  }
}
class FavoriteSources extends StatelessWidget {
  const FavoriteSources({
    super.key,
    required this.width,
    required this.imagePath,
    required this.name,
    required this.imageScale,
    required this.shadowColor,
    required this.isActive,
    required this.onTap,
  });
  final double width;
  final String imagePath;
  final String name;
  final double imageScale;
  final Color shadowColor;
  final bool isActive;
  final VoidCallback onTap;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      borderRadius: BorderRadius.circular(8),
      onTap: onTap,
      child: Container(
        height: width / 3.4,
        width: width / 3.4,
        decoration: isActive
            ? BoxDecoration(
                color: Colors.black26,
                borderRadius: BorderRadius.circular(8),
              )
            : null,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              decoration: isActive
                  ? BoxDecoration(
                      boxShadow: [
                        BoxShadow(
                          color: shadowColor.withOpacity(0.7),
                          blurRadius: 20,
                          offset: Offset.zero,
                        )
                      ],
                    )
                  : null,
              child: Image.asset(
                imagePath,
                width: width / imageScale,
                height: width / 6,
              ),
            ),
            Text(name, style: Theme.of(context).textTheme.titleMedium)
          ],
        ),
      ),
    );
  }
}

سلام. پرانتز رو فراموش کردید:

widget.toggleThemeMode();
بهترین پاسخ
یلدا محصلی ۱۵ فروردین ۱۴۰۳، ۱۸:۲۷

ممنون

Mahyar ۱۵ فروردین ۱۴۰۳، ۱۸:۳۶