content: // This code is distributed under the MIT License.
  // Copyright (c) 2022 Remi Rousselet.
  // You can find the original at https://github.com/rrousselGit/riverpod.
  
  import 'package:collection/collection.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter_riverpod/flutter_riverpod.dart';
  
  void main() {
    runApp(const ProviderScope(child: MyApp()));
  }
  
  class MyApp extends StatelessWidget {
    const MyApp({super.key});
  
    @override
    Widget build(BuildContext context) {
      return const MaterialApp(
        debugShowCheckedModeBanner: false,
        home: MyHomePage(),
      );
    }
  }
  
  class Product {
    Product({required this.name, required this.price});
  
    final String name;
    final double price;
  }
  
  final _products = [
    Product(name: 'iPhone', price: 999),
    Product(name: 'cookie', price: 2),
    Product(name: 'ps5', price: 500),
  ];
  
  enum ProductSortType {
    name,
    price,
  }
  
  final productSortTypeProvider = StateProvider<ProductSortType>(
    // Sıralama türü adını döndürün. Bu varsayılan durumdur.
    (ref) => ProductSortType.name,
  );
  
  final productsProvider = Provider<List<Product>>((ref) {
    final sortType = ref.watch(productSortTypeProvider);
    switch (sortType) {
      case ProductSortType.name:
        return _products.sorted((a, b) => a.name.compareTo(b.name));
      case ProductSortType.price:
        return _products.sorted((a, b) => a.price.compareTo(b.price));
    }
  });
  
  class MyHomePage extends ConsumerWidget {
    const MyHomePage({super.key});
  
    @override
    Widget build(BuildContext context, WidgetRef ref) {
      final products = ref.watch(productsProvider);
      return Scaffold(
        appBar: AppBar(
          title: const Text('Products'),
          actions: [
            DropdownButton<ProductSortType>(
              // Sıralama türü değiştiğinde açılır menü güncellenir
              // Görüntülenen simge (menü öğesi) değişecektir.
              value: ref.watch(productSortTypeProvider),
              // Kullanıcı açılır menüyle etkileşime girdiğinde provider'ın durumunu güncelleyin.
              onChanged: (value) =>
                  ref.read(productSortTypeProvider.notifier).state = value!,
              items: const [
                DropdownMenuItem(
                  value: ProductSortType.name,
                  child: Icon(Icons.sort_by_alpha),
                ),
                DropdownMenuItem(
                  value: ProductSortType.price,
                  child: Icon(Icons.sort),
                ),
              ],
            ),
          ],
        ),
        body: ListView.builder(
          itemCount: products.length,
          itemBuilder: (context, index) {
            final product = products[index];
            return ListTile(
              title: Text(product.name),
              subtitle: Text('${product.price} \$'),
            );
          },
        ),
      );
    }
  }
  