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

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

class EnglishLearningApp extends StatelessWidget {
  const EnglishLearningApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'تعلم الإنجليزية',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: const HomeScreen(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('الصفحة الرئيسية'),
        centerTitle: true,
        elevation: 4.0,
      ),
      body: GridView.count(
        padding: const EdgeInsets.all(16.0),
        crossAxisCount: 2,
        crossAxisSpacing: 16.0,
        mainAxisSpacing: 16.0,
        children: <Widget>[
          _buildGridItem(context, 'تعلم الحروف', Icons.abc, const LevelsScreen()),
          _buildGridItem(context, 'الاختبارات', Icons.quiz, const TestSelectionScreen()),
          _buildGridItem(context, 'الألعاب', Icons.gamepad, const GamesScreen()),
        ],
      ),
    );
  }

  Widget _buildGridItem(BuildContext context, String title, IconData icon, Widget page) {
    return Card(
      elevation: 5,
      child: InkWell(
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => page),
          );
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Icon(icon, size: 100, color: Colors.blueAccent),
            const SizedBox(height: 10),
            Text(title, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
          ],
        ),
      ),
    );
  }
}

class LevelsScreen extends StatelessWidget {
  const LevelsScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('اختيار المستوى'),
        centerTitle: true,
        elevation: 4.0,
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: GridView.count(
        padding: const EdgeInsets.all(16.0),
        crossAxisCount: 2,
        crossAxisSpacing: 16.0,
        mainAxisSpacing: 16.0,
        children: <Widget>[
          _buildLevelItem(context, 'المستوى 1', 'A', 'F'),
          _buildLevelItem(context, 'المستوى 2', 'G', 'L'),
          _buildLevelItem(context, 'المستوى 3', 'M', 'R'),
          _buildLevelItem(context, 'المستوى 4', 'S', 'Z'),
        ],
      ),
    );
  }

  Widget _buildLevelItem(BuildContext context, String title, String startLetter, String endLetter) {
    return Card(
      elevation: 5,
      child: InkWell(
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => LetterDetailScreen(
                letters: _generateLettersRange(startLetter, endLetter),
                levelTitle: title,
              ),
            ),
          );
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Icon(Icons.school, size: 100, color: Colors.blueAccent),
            const SizedBox(height: 10),
            Text(title, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
          ],
        ),
      ),
    );
  }

  List<String> _generateLettersRange(String start, String end) {
    return List.generate(end.codeUnitAt(0) - start.codeUnitAt(0) + 1, (index) => String.fromCharCode(start.codeUnitAt(0) + index));
  }
}

class LetterDetailScreen extends StatefulWidget {
  final List<String> letters;
  final String levelTitle;

  const LetterDetailScreen({Key? key, required this.letters, required this.levelTitle}) : super(key: key);

  @override
  _LetterDetailScreenState createState() => _LetterDetailScreenState();
}

class _LetterDetailScreenState extends State<LetterDetailScreen> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    final letter = widget.letters[_currentIndex];
    final descriptions = {
      'A': ['Apple', 'Ant'],
      'B': ['Ball', 'Banana'],
      'C': ['Cat', 'Car'],
      'D': ['Dog', 'Duck'],
      'E': ['Elephant', 'Egg'],
      'F': ['Fish', 'Flower'],
      'G': ['Guitar', 'Grapes'],
      'H': ['Hat', 'House'],
      'I': ['Ice cream', 'Insect'],
      'J': ['Juice', 'Jacket'],
      'K': ['Kite', 'King'],
      'L': ['Lion', 'Lamp'],
      'M': ['Monkey', 'Mango'],
      'N': ['Nest', 'Nurse'],
      'O': ['Orange', 'Owl'],
      'P': ['Pen', 'Pencil'],
      'Q': ['Queen', 'Quilt'],
      'R': ['Rabbit', 'Rainbow'],
      'S': ['Sun', 'Star'],
      'T': ['Tree', 'Tiger'],
      'U': ['Umbrella', 'UFO'],
      'V': ['Violin', 'Vase'],
      'W': ['Watermelon', 'Whale'],
      'X': ['Xylophone'],
      'Y': ['Yellow', 'Yacht'],
      'Z': ['Zebra', 'Zoo'],
    };

    final images = {
      'A': ['assets/apple.png', 'assets/ant.png'],
      'B': ['assets/ball.png', 'assets/banana.png'],
      'C': ['assets/cat.png', 'assets/car.png'],
      'D': ['assets/dog.png', 'assets/duck.png'],
      'E': ['assets/elephant.png', 'assets/egg.png'],
      'F': ['assets/fish.png', 'assets/flower.png'],
      'G': ['assets/guitar.png', 'assets/grapes.png'],
      'H': ['assets/hat.png', 'assets/house.png'],
      'I': ['assets/ice_cream.png', 'assets/insect.png'],
      'J': ['assets/juice.png', 'assets/jacket.png'],
      'K': ['assets/kite.png', 'assets/king.png'],
      'L': ['assets/lion.png', 'assets/lamp.png'],
      'M': ['assets/monkey.png', 'assets/mango.png'],
      'N': ['assets/nest.png', 'assets/nurse.png'],
      'O': ['assets/orange.png', 'assets/owl.png'],
      'P': ['assets/pen.png', 'assets/pencil.png'],
      'Q': ['assets/queen.png', 'assets/quilt.png'],
      'R': ['assets/rabbit.png', 'assets/rainbow.png'],
      'S': ['assets/sun.png', 'assets/star.png'],
      'T': ['assets/tree.png', 'assets/tiger.png'],
      'U': ['assets/umbrella.png', 'assets/ufo.png'],
      'V': ['assets/violin.png', 'assets/vase.png'],
      'W': ['assets/watermelon.png', 'assets/whale.png'],
      'X': ['assets/xylophone.png'],
      'Y': ['assets/yellow.png', 'assets/yacht.png'],
      'Z': ['assets/zebra.png', 'assets/zoo.png'],
    };

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.levelTitle),
        centerTitle: true,
        elevation: 4.0,
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            letter,
            style: const TextStyle(fontSize: 100.0, fontWeight: FontWeight.bold),
          ),
          const SizedBox(height: 20),
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List.generate(
                descriptions[letter]!.length,
                (index) => Expanded(
                  child: Card(
                    child: Column(
                      children: <Widget>[
                        Image.asset(images[letter]![index], height: 100.0),
                        Text(
                          descriptions[letter]![index],
                          style: const TextStyle(fontSize: 18.0),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: () {
              setState(() {
                if (_currentIndex < widget.letters.length - 1) {
                  _currentIndex++;
                } else {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => const TestSelectionScreen(),
                    ),
                  );
                }
              });
            },
            child: Text(
              _currentIndex < widget.letters.length - 1 ? 'التالي' : 'اختبار',
              style: const TextStyle(fontSize: 18.0),
            ),
          ),
        ],
      ),
    );
  }
}

class TestSelectionScreen extends StatelessWidget {
  const TestSelectionScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('الاختبارات'),
        centerTitle: true,
        elevation: 4.0,
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: GridView.count(
        padding: const EdgeInsets.all(16.0),
        crossAxisCount: 2,
        crossAxisSpacing: 16.0,
        mainAxisSpacing: 16.0,
        children: <Widget>[
          _buildTestItem(context, 'اختبار الحروف', Icons.text_fields),
          _buildTestItem(context, 'اختبار الكلمات', Icons.text_format),
        ],
      ),
    );
  }

  Widget _buildTestItem(BuildContext context, String title, IconData icon) {
    return Card(
      elevation: 5,
      child: InkWell(
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => TestDetailScreen(testType: title),
            ),
          );
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Icon(icon, size: 100, color: Colors.blueAccent),
            const SizedBox(height: 10),
            Text(title, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
          ],
        ),
      ),
    );
  }
}

class TestDetailScreen extends StatelessWidget {
  final String testType;

  const TestDetailScreen({Key? key, required this.testType}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(testType),
        centerTitle: true,
        elevation: 4.0,
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: Center(
        child: Text(
          'هنا سيكون اختبار $testType',
          style: const TextStyle(fontSize: 24.0),
        ),
      ),
    );
  }
}

class GamesScreen extends StatelessWidget {
  const GamesScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('الألعاب'),
        centerTitle: true,
        elevation: 4.0,
        actions: [
          IconButton(
            icon: const Icon(Icons.home),
            onPressed: () {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(
                  builder: (context) => const HomeScreen(),
                ),
                (Route<dynamic> route) => false,
              );
            },
          ),
        ],
      ),
      body: Center(
        child: Text(
          'هنا سيكون الألعاب',
          style: const TextStyle(fontSize: 24.0),
        ),
      ),
    );
  }
}
  طور الكود واضف ما يلزم