Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.3 kB
2
Indexable
Never
import 'package:flutter/material.dart';
import 'pembayaran.dart';

class Language {
  final String name;
  final double rating;
  final int price;

  Language({required this.name, required this.rating, required this.price});
}

class CartPage extends StatefulWidget {
  @override
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  TextEditingController searchController = TextEditingController();
  List<Language> selectedLanguages = [
    Language(name: 'HTML Dasar', rating: 4.5, price: 50),
    Language(name: 'Phyton', rating: 4.2, price: 60),
    Language(name: 'Golang', rating: 4.8, price: 70),
    // Tambahkan bahasa pemrograman lainnya dengan rating dan harga yang sesuai
  ];

  List<Language> filteredLanguages = [];
  int selectedCourseIndex = -1;

  void removeLanguage(Language language) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Hapus ${language.name}?'),
          content: Text(
              'Apakah Anda yakin ingin menghapus ${language.name} dari keranjang belanja?'),
          actions: [
            TextButton(
              child: Text('Tidak'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text('Ya'),
              onPressed: () {
                setState(() {
                  selectedLanguages.remove(language);
                });
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  void checkout() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Checkout'),
          content: Text('Apakah Anda yakin ingin checkout?'),
          actions: [
            TextButton(
              child: Text('Tidak'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text('Ya'),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => PaymentPage()),
                );
              },
            ),
          ],
        );
      },
    );
  }

  void filterLanguages(String query) {
    filteredLanguages.clear();
    if (query.isNotEmpty) {
      selectedLanguages.forEach((language) {
        if (language.name.toLowerCase().contains(query.toLowerCase())) {
          filteredLanguages.add(language);
        }
      });
    } else {
      filteredLanguages.addAll(selectedLanguages);
    }
    setState(() {});
  }

  void _showCourseDetail(String courseName) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(courseName),
          content: Text('Ini adalah detail course $courseName'),
          actions: [
            TextButton(
              child: Text('Tutup'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Keranjang Belanja'),
        backgroundColor: Colors.green,
      ),
      body: Column(
        children: [
          TextField(
            controller: searchController,
            decoration: InputDecoration(
              labelText: 'Cari',
              prefixIcon: Icon(Icons.search),
            ),
            onChanged: filterLanguages,
          ),
          Expanded(
            child: ListView.builder(
              itemCount: filteredLanguages.length,
              itemBuilder: (BuildContext context, int index) {
                return InkWell(
                  onTap: () {
                    setState(() {
                      selectedCourseIndex =
                          selectedLanguages.indexOf(filteredLanguages[index]);
                    });
                    _showCourseDetail(filteredLanguages[index].name);
                  },
                  child: Card(
                    elevation: 4,
                    margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                    color: selectedCourseIndex ==
                            selectedLanguages.indexOf(filteredLanguages[index])
                        ? Colors.green[100]
                        : Colors.white,
                    child: ListTile(
                      leading: Image.asset(
                        'assets/${filteredLanguages[index].name.toLowerCase()}.png',
                        width: 60,
                        height: 60,
                      ),
                      title: Text(filteredLanguages[index].name),
                      subtitle: Text(
                          '\$${filteredLanguages[index].price.toString()} | Rating: ${filteredLanguages[index].rating.toString()}/5'),
                      trailing: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          IconButton(
                            icon: Icon(Icons.delete),
                            onPressed: () {
                              removeLanguage(filteredLanguages[index]);
                            },
                          ),
                          ElevatedButton(
                            child: Text('Checkout'),
                            onPressed: () {
                              checkout();
                            },
                            style: ButtonStyle(
                              backgroundColor: MaterialStateProperty.all<Color>(
                                  Colors.green),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}