mirrored_repositories/bat_theme
import 'package:flutter/material.dart';
import 'package:kaunta/extensions/custom_textstyles.dart';
import 'package:kaunta/themes.dart';
import 'extensions/custom_colors.dart';
import 'theme_manager.dart';

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

ThemeManager _themeManager = ThemeManager();

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // This widget is the root of your application.

  @override
  void dispose() {
    _themeManager.removeListener(themeListener);
    super.dispose();
  }

  @override
  void initState() {
    _themeManager.addListener(themeListener);
    super.initState();
  }

  themeListener() {
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: lightTheme.copyWith(extensions: <ThemeExtension<dynamic>>[
        const AppStyles(
            body: TextStyle(
          fontFamily: 'Marianne',
          fontSize: 17.0,
          fontWeight: FontWeight.normal,
          decoration: TextDecoration.none,
        )),
        const AppColors(),
      ]),
      darkTheme: darkTheme,
      themeMode: _themeManager.themeMode,
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void dispose() {
    _themeManager.removeListener(themeListener);
    super.dispose();
  }

  @override
  void initState() {
    _themeManager.addListener(themeListener);
    super.initState();
  }

  themeListener() {
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Theme Extension"),
        actions: [
          Switch(
              value: _themeManager.themeMode == ThemeMode.dark,
              onChanged: (newValue) {
                _themeManager.toggleTheme(newValue);
              })
        ],
      ),
      body: ListView.separated(
        padding: const EdgeInsets.all(16),
        itemBuilder: (context, index) => DetailTile(index: index),
        separatorBuilder: (_, __) => Container(
          height: 8,
          color: Colors.grey.withOpacity(.1),
        ),
        itemCount: 20,
      ),
    );
  }
}

class DetailTile extends StatelessWidget {
  const DetailTile({
    super.key,
    required this.index,
  });

  final int index;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color(0xFF28A745),
      child: Row(
        children: [
          const FlutterLogo(size: 40),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'item ${index + 1}',
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
                Text(
                  'Description ${index + 1}',
                  style: Theme.of(context).textTheme.bodyLarge,
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
