Asset-Borrowing-App / lib / Project / HomePage / product_detail.dart
product_detail.dart
Raw
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/Project/HomePage/total.dart';
import '../utils/colors.dart';
import 'Model/data.dart';

class ProductDetailPage extends StatefulWidget {
  final Product product;

  const ProductDetailPage({Key? key, required this.product}) : super(key: key);

  @override
  State<ProductDetailPage> createState() => _ProductDetailPageState();
}

class _ProductDetailPageState extends State<ProductDetailPage> {
  DateTime selectfdate = DateTime.now();
  DateTime selectfdate2 = DateTime.now();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.black),
        backgroundColor: Colors.transparent,
        elevation: 0,
        title: Text(
          widget.product.productName,
          style: const TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.w700,
            color: Colors.black,
          ),
        ),
      ),
      body: Container(
        padding: const EdgeInsets.only(top: 5),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Center(
              child: Container(
                padding: const EdgeInsets.all(10),
                width: 200,
                height: 200,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(28),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.3),
                      spreadRadius: 1,
                      blurRadius: 5,
                      offset: const Offset(1, 1),
                    ),
                  ],
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(28),
                  child: Image.network(
                    widget.product.productImageUrl,
                    height: 190,
                    width: 190,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ),
            const SizedBox(
              height: 0,
            ),
            Padding(
              padding: const EdgeInsets.all(20.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    widget.product.productName,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: const TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                        color: Colors.black),
                  ),
                  const SizedBox(height: 10),
                  Container(
                    padding: const EdgeInsets.all(10),
                    height: 100,
                    decoration: BoxDecoration(
                      color: Colors.grey[200],
                      borderRadius: BorderRadius.circular(15),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.3),
                          spreadRadius: 1,
                          blurRadius: 5,
                          offset: const Offset(1, 1),
                        ),
                      ],
                    ),
                    child: SingleChildScrollView(
                      child: Text(
                        widget.product.productDescription,
                        style: const TextStyle(
                          fontSize: 15,
                        ),
                        textAlign: TextAlign.start,
                      ),
                    ),
                  ),
                  const SizedBox(height: 15),
                  const Center(
                    child: Text(
                      'Select Borrowing Period',
                      style: TextStyle(
                        fontSize: 15,
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                    ),
                  ),
                  const SizedBox(height: 10),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      ElevatedButton(
                        onPressed: () async {
                          final date = await pickdate();
                          if (date == null) return;
                          final newDatetime = DateTime(
                            date.year,
                            date.month,
                            date.day,
                          );
                          setState(
                            () => selectfdate = newDatetime,
                          );
                          // Handle button 1 press
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: kGreenColor,
                        ),
                        child: Text(
                            '${selectfdate.year}/${selectfdate.month}/${selectfdate.day}'),
                      ),
                      ElevatedButton(
                        onPressed: () async {
                          final date2 = await pickdate2();
                          if (date2 == null) return;
                          final newDatetime2 = DateTime(
                            date2.year,
                            date2.month,
                            date2.day,
                          );
                          setState(
                            () => selectfdate2 = newDatetime2,
                          );

                          // Handle button 2 press
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: kGreenColor,
                        ),
                        child: Text(
                            '${selectfdate2.year}/${selectfdate2.month}/${selectfdate2.day}'),
                      ),
                    ],
                  ),
                  const SizedBox(height: 10),
                  Center(
                    child: Column(
                      children: [
                        Text(
                          'Borrow for: ${selectfdate.year}/${selectfdate.month}/${selectfdate.day} - ${selectfdate2.year}/${selectfdate2.month}/${selectfdate2.day}',
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 10),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      ElevatedButton(
                        onPressed: () {
                          showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              return AlertDialog(
                                title: const Text(
                                  'CONFIRMATION',
                                  style: TextStyle(
                                      color: Colors.red,
                                      fontWeight: FontWeight.bold),
                                ),
                                content: const Text(
                                  'Are you sure you want to borrow this product?',
                                  style: TextStyle(
                                    height: 1.5,
                                  ),
                                ),
                                actions: [
                                  TextButton(
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                    child: const Text('Cancel'),
                                  ),
                                  TextButton(
                                    onPressed: () {
                                      Navigator.of(context)
                                          .pop(); // ปิด AlertDialog
                                      final req = FirebaseFirestore.instance
                                          .collection('products')
                                          .snapshots;
                                      Adddata();
                                      editData(req.toString());
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) => TotalPro(
                                            c: widget.product.productName
                                                .toString(),
                                          ),
                                        ),
                                      );
                                    },
                                    child: const Text(
                                      'Confirm',
                                      style: TextStyle(
                                        color: Colors.red,
                                      ),
                                    ),
                                  ),
                                ],
                              );
                            },
                          );
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: kGreenColor,
                        ),
                        child: const Text('Confirm'),
                      ),
                    ],
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Future<DateTime?> pickdate() => showDatePicker(
      context: context,
      initialDate: selectfdate,
      firstDate: DateTime(1900),
      lastDate: DateTime(2080));
  Future<DateTime?> pickdate2() => showDatePicker(
      context: context,
      initialDate: selectfdate,
      firstDate: DateTime(1900),
      lastDate: DateTime(2080));
  Future<void> Adddata() async {
    final FirebaseAuth _auth = FirebaseAuth.instance;
    String a = '${selectfdate.year}/${selectfdate.month}/${selectfdate.day}';
    String b = '${selectfdate2.year}/${selectfdate2.month}/${selectfdate2.day}';
    String c = widget.product.productName;
    DateTime d = DateTime.now();
    String e = '${d.year}/${d.month}/${d.day} Time : ${d.hour}.${d.minute}';
    String EmailUserreq = _auth.currentUser!.email.toString();
    var data = {
      'EmailUserreq': EmailUserreq,
      'BorrowingDay': a,
      'ReturnDay': b,
      'Name': c,
      'Status': 'Check',
      'Timerequest': e
    };
    FirebaseFirestore.instance.collection('request').add(data).then(
          (value) => print('Adding done!'),
          onError: (e) => print('Error $e'),
        );
  }

  Future<void> editData(String docId) async {
    var data = {'Req': '1'};
    FirebaseFirestore.instance
        .collection('products')
        .doc(docId)
        .update(data)
        .then(
          (value) => print('Updating done!'),
          onError: (e) => print('Error $e'),
        );
  }
}