174 lines
6.8 KiB
Dart
174 lines
6.8 KiB
Dart
import 'package:carousel_slider/carousel_options.dart';
|
|
import 'package:carousel_slider/carousel_slider.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:dots_indicator/dots_indicator.dart';
|
|
import 'package:e_commerce/const/AppColors.dart';
|
|
import 'package:e_commerce/const/rupiah.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
import '../product_details_screen.dart';
|
|
import '../search_screen.dart';
|
|
|
|
class Home extends StatefulWidget {
|
|
@override
|
|
_HomeState createState() => _HomeState();
|
|
}
|
|
|
|
class _HomeState extends State<Home> {
|
|
List<String> _carouselImages = [];
|
|
var _dotPosition = 0;
|
|
List _products = [];
|
|
var _firestoreInstance = FirebaseFirestore.instance;
|
|
|
|
// Validasi URL untuk memastikan URL valid
|
|
bool isValidUrl(String url) {
|
|
try {
|
|
Uri uri = Uri.parse(url);
|
|
return uri.hasScheme && (uri.scheme == 'http' || uri.scheme == 'https');
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
fetchCarouselImages() async {
|
|
QuerySnapshot qn = await _firestoreInstance.collection("carousel-slider").get();
|
|
List<String> carouselImages = qn.docs.map((doc) => doc["img-path"].toString()).toList();
|
|
|
|
// Tambahkan gambar produk ke daftar carousel
|
|
_products.forEach((product) {
|
|
_carouselImages.addAll(product["product-img"]); // Menambahkan semua gambar produk ke carousel
|
|
});
|
|
|
|
setState(() {
|
|
_carouselImages = carouselImages; // Memperbarui daftar carousel dengan gambar baru
|
|
});
|
|
}
|
|
|
|
fetchProducts() async {
|
|
QuerySnapshot qn = await _firestoreInstance.collection("products").get();
|
|
setState(() {
|
|
_products = qn.docs.map((doc) {
|
|
return {
|
|
"product-name": doc["product-name"],
|
|
"product-description": doc["product-description"],
|
|
"product-price": doc["product-price"],
|
|
"stock": doc["stock"],
|
|
"product-img": isValidUrl(doc["product-img"]) ? [doc["product-img"]] : ["https://via.placeholder.com/150"],
|
|
};
|
|
}).toList();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
fetchCarouselImages();
|
|
fetchProducts();
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: Container(
|
|
child: Column(
|
|
children: [
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 20.w, right: 20.w),
|
|
child: TextFormField(
|
|
readOnly: true,
|
|
decoration: InputDecoration(
|
|
fillColor: Colors.white,
|
|
focusedBorder: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(0)), borderSide: BorderSide(color: Colors.blue)),
|
|
enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(0)), borderSide: BorderSide(color: Colors.grey)),
|
|
hintText: "Search products here",
|
|
hintStyle: TextStyle(fontSize: 15.sp),
|
|
),
|
|
onTap: () => Navigator.push(context, CupertinoPageRoute(builder: (_) => SearchScreen())),
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: 10.h,
|
|
),
|
|
Expanded(
|
|
child: GridView.builder(
|
|
scrollDirection: Axis.vertical,
|
|
itemCount: _products.length,
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 1),
|
|
itemBuilder: (_, index) {
|
|
final rupiah = int.parse(_products[index]["product-price"]) ?? 0;
|
|
return GestureDetector(
|
|
onTap: () async {
|
|
Navigator.push(context, MaterialPageRoute(builder: (_) => ProductDetails(_products[index])));
|
|
},
|
|
child: Container(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Card(
|
|
elevation: 3,
|
|
child: Column(
|
|
children: [
|
|
AspectRatio(
|
|
aspectRatio: 2,
|
|
child: Container(
|
|
child: isValidUrl(_products[index]["product-img"][0])
|
|
? Image.network(
|
|
_products[index]["product-img"][0],
|
|
fit: BoxFit.cover,
|
|
)
|
|
: Text("Invalid image URL"),
|
|
)),
|
|
const SizedBox(
|
|
height: 10.0,
|
|
),
|
|
Text(
|
|
"${_products[index]["product-name"]}",
|
|
style: GoogleFonts.beVietnamPro(
|
|
textStyle: const TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(
|
|
height: 5.0,
|
|
),
|
|
Text(
|
|
"${rupiah.toRupiah()}",
|
|
style: GoogleFonts.beVietnamPro(
|
|
textStyle: const TextStyle(
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(
|
|
height: 5.0,
|
|
),
|
|
Text(
|
|
"Stok : ${_products[index]['stock']}",
|
|
style: GoogleFonts.beVietnamPro(
|
|
textStyle: const TextStyle(
|
|
fontSize: 10,
|
|
fontWeight: FontWeight.normal,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}),
|
|
),
|
|
],
|
|
),
|
|
)),
|
|
);
|
|
}
|
|
}
|