import 'dart:convert'; import 'dart:io'; import 'package:carousel_slider/carousel_slider.dart'; import 'package:cathaypay_mobile/GetPaid/GetPaid.dart'; import 'package:cathaypay_mobile/Home/home_bottom_menu_widget.dart'; import 'package:cathaypay_mobile/Login/LoginValidate.dart'; import 'package:cathaypay_mobile/Pay/PayPage.dart'; import 'package:cathaypay_mobile/TopUp/TopUpMenuPage.dart'; import 'package:cathaypay_mobile/TransferMoney/TransferMoneyMenu.dart'; import 'package:cathaypay_mobile/model/banner_model.dart'; import 'package:cathaypay_mobile/model/profile_model.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:url_launcher/url_launcher.dart'; import '../api/api.dart'; import '../model/promotion_model.dart'; import '../model/token_model.dart'; import '../utils/color_custom.dart'; import '../utils/utils.dart'; Profile? profile; String tokenAdmin = ""; bool _canPop = false; class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); @override State createState() => _HomePageState(); } class _HomePageState extends State { @override void initState() { // getProfile(context); // getBalance(context); // getPoint(context); getTokenAdmin(); getBanner(); getPromotion(); checkAndRequestPermissions(skipIfExists: true); super.initState(); } Future checkAndRequestPermissions({required bool skipIfExists}) async { if (!Platform.isAndroid && !Platform.isIOS) { return false; // Only Android and iOS platforms are supported } if (Platform.isAndroid) { final deviceInfo = await DeviceInfoPlugin().androidInfo; final sdkInt = deviceInfo.version.sdkInt; if (skipIfExists) { // Read permission is required to check if the file already exists return sdkInt >= 33 ? await Permission.photos.request().isGranted : await Permission.storage.request().isGranted; } else { // No read permission required for Android SDK 29 and above return sdkInt >= 29 ? true : await Permission.storage.request().isGranted; } } else if (Platform.isIOS) { // iOS permission for saving images to the gallery return skipIfExists ? await Permission.photos.request().isGranted : await Permission.photosAddOnly.request().isGranted; } return false; // Unsupported platforms } Future getProfile() async { Api.get(context, Api.profile).then((value) => { if (value != null) {initProfile(value)} else {clearProfile()} }); } initProfile(value) { setState(() { profile = Profile.fromJson(value["profile"]); }); _refreshController.refreshCompleted(); } clearProfile() async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString("access_token", ""); _canPop = true; Navigator.popUntil(context, ModalRoute.withName('/')); _canPop = false; } lockTest() { var param = jsonEncode({"userId": profile?.id}); Api.post(context, Api.lockTest, param).then((value) => { if (value != null) { value.forEach((v) { if (v["file_url"] != null) { listBanner.add(BannerModel.fromJson(v)); } }), refresh() } else {} }); } List listBanner = []; /* getBanner() { Api.get(context, Api.banner).then((value) => { if (value != null) { value.forEach((v) { if (v["file_url"] != null) { listBanner.add(BannerModel.fromJson(v)); } }), refresh() } else {} }); }*/ getBanner() { Api.get(context, Api.banner).then((value) { if (value != null) { DateTime now = DateTime.now(); // วันปัจจุบัน value.forEach((v) { if (v["file_url"] != null && v["start_date"] != null && v["end_date"] != null) { DateTime startDate = DateTime.parse(v["start_date"]); DateTime endDate = DateTime.parse(v["end_date"]); if (startDate.isBefore(now) && endDate.isAfter(now)) { listBanner.add(BannerModel.fromJson(v)); } } }); refresh(); } }); } List listPromotion = []; // getPromotion() { // Api.get(context, Api.promotion).then((value) => { // if (value != null) // { // value.forEach((v) { // if (v["file_url"] != null) { // listPromotion.add(PromotionModel.fromJson(v)); // } // }), // refresh() // } // else // {} // }); // } getPromotion() { Api.get(context, Api.promotion).then((value) { if (value != null) { DateTime now = DateTime.now(); // วันปัจจุบัน value.forEach((v) { if (v["file_url"] != null && v["start_date"] != null && v["end_date"] != null) { DateTime startDate = DateTime.parse(v["start_date"]); DateTime endDate = DateTime.parse(v["end_date"]); if (startDate.isBefore(now) && endDate.isAfter(now)) { listPromotion.add(PromotionModel.fromJson(v)); } } }); refresh(); } }); } getTokenAdmin() { // Api.get(context, Api.getTokenAdmin).then((value) => {tokenAdmin = value["token"]}); var param = jsonEncode({ "UserName": Api.userAdmin, "Password": Api.passAdmin, "MobileDeviceId": Api.mobileAdmin, }); Api.postLogin(context, Api.login, param).then((value) => { if (value != null) { setState(() { var tokenModel = TokenModel.fromJson(value); tokenAdmin = tokenModel.token ?? ""; }) } else {} }); } refresh() { setState(() {}); } Future _launchUrl(String url) async { if (!await launchUrl(Uri.parse(url))) { throw Exception('Could not launch $url'); } } RefreshController _refreshController = RefreshController(initialRefresh: false); @override Widget build(BuildContext context) { return PopScope( canPop: _canPop, // <== set a bool var or set it to false onPopInvoked: (bool didPop) async { if (didPop) { return; } if (Navigator.of(context).canPop()) { Navigator.of(context).pop(context); } }, child: Scaffold( body: SafeArea( child: SmartRefresher( onRefresh: () { getProfile(); }, controller: _refreshController, child: Column( children: [ Expanded( child: Stack( children: [ Container( width: MediaQuery.of(context).size.width, child: Image( image: AssetImage('images/home/Head.png'), fit: BoxFit.fitWidth, ), ), SingleChildScrollView( child: Padding( padding: EdgeInsets.all(10), child: Column( children: [ Container( alignment: Alignment.centerRight, child: Row( crossAxisAlignment: CrossAxisAlignment.end, mainAxisAlignment: MainAxisAlignment.end, children: [ InkWell( onTap: () { clearProfile(); }, child: Icon( Icons.logout, size: 20, color: Colors.white, ), ), SizedBox( width: 10, ), InkWell( onTap: () { if (context.locale.toString() == "th") { context.setLocale(Locale('en', '')); } else { context.setLocale(Locale('th', '')); } refresh(); }, child: Row( children: [ context.locale.toString() == "th" ? Image.asset( "images/home/lang_20.png", width: 20, ) : Image.asset( "images/home/lang_21.png", width: 20, ), SizedBox( width: 5, ), Text( context.locale.toString().toUpperCase(), style: GoogleFonts.kanit( color: Colors.white, fontSize: 14, ), ), ], ), ), ], ), ), Container( padding: EdgeInsets.only(left: 10), child: Center( child: Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Image( image: AssetImage('images/neopay_logo.png'), fit: BoxFit.fitHeight, height: 80, ), Column( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "Your Name".tr(), style: GoogleFonts.kanit( color: Colors.white, fontSize: 12, fontWeight: FontWeight.w300, ), ), Text( tokenModel?.fullName ?? "", style: GoogleFonts.kanit( color: Colors.white, fontSize: 18, ), ), SizedBox( height: 10, ), Text( "Member ID".tr(), style: GoogleFonts.kanit( color: Colors.white, fontSize: 12, fontWeight: FontWeight.w300, ), ), Text( tokenModel?.maxCardMembers?.first.maxCardID ?? "", style: GoogleFonts.kanit( color: Colors.white, fontSize: 18, ), ), ], ) ], ), ), ), SizedBox( height: 20, ), CardHomePage(), SizedBox( height: 20, ), MenuHomePage(), SizedBox( height: 20, ), CarouselSlider( options: CarouselOptions( height: 200.0, aspectRatio: 16 / 9, viewportFraction: 1.0, initialPage: 0, enableInfiniteScroll: true, reverse: false, autoPlay: true, enlargeCenterPage: true, ), items: listBanner.map((i) { return Builder( builder: (BuildContext context) { return InkWell( onTap: () { _launchUrl(i.link ?? ""); }, child: Container(width: MediaQuery.of(context).size.width, child: i.fileUrl != null ? Image.network(i.fileUrl ?? "") : Container()), ); }, ); }).toList(), ), /* Image( image: AssetImage('images/home/Banner.png'), fit: BoxFit.fill, ),*/ SizedBox( height: 20, ), Row( children: [ Text( "Promotions for you".tr(), style: GoogleFonts.kanit( color: Color(0xff050505), fontSize: 16, ), ), Spacer() ], ), SizedBox( height: 20, ), GridView.count( physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, crossAxisCount: 2, childAspectRatio: 1 / 1, mainAxisSpacing: 5.0, crossAxisSpacing: 5.0, children: listPromotion.map((PromotionModel promo) { return InkWell( onTap: () { _launchUrl(promo.link ?? ""); }, child: promo.fileUrl != null ? ClipRRect( borderRadius: BorderRadius.circular(15), child: Image.network(promo.fileUrl ?? ""), ) : Container(), ); }).toList()), /*CarouselSlider( options: CarouselOptions( height: 200.0, aspectRatio: 16 / 9, viewportFraction: 1.0, initialPage: 0, enableInfiniteScroll: true, reverse: false, autoPlay: true, enlargeCenterPage: true, ), items: listPromotion.map((i) { return Builder( builder: (BuildContext context) { return InkWell( onTap: () { _launchUrl(i.link ?? ""); }, child: Container(width: MediaQuery.of(context).size.width, child: i.fileUrl != null ? Image.network(i.fileUrl ?? "") : Container()), ); }, ); }).toList(), ),*/ ], ), )) ], ), ), /* Container( child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.end, children: [ Container( width: MediaQuery.of(context).size.width, padding: EdgeInsets.all(20), decoration: BoxDecoration( boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.07999999821186066), offset: Offset(0, -4), blurRadius: 4)], color: Color.fromRGBO(255, 255, 255, 1), ), child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Container( height: 35, width: 35, child: Image( image: AssetImage('images/home/1.home.png'), fit: BoxFit.cover, ), ), Text( "หน้าหลัก".tr(), textAlign: TextAlign.center, style: TextStyle( color: Color(0xff65676b), fontSize: 12, ), ) ], ), Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Container( height: 35, width: 35, child: Image( image: AssetImage('images/home/2.scan.png'), fit: BoxFit.cover, ), ), Text( "สแกน".tr(), textAlign: TextAlign.center, style: TextStyle( color: Color(0xff65676b), fontSize: 12, ), ) ], ), InkWell( onTap: () { // clearProfile(); }, child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Container( height: 35, width: 35, child: Image( image: AssetImage('images/home/3.all.png'), fit: BoxFit.cover, ), ), Text( "ทั้งหมด".tr(), textAlign: TextAlign.center, style: TextStyle( color: Color(0xff65676b), fontSize: 12, ), ) ], ), ), ], ), ), ], ), )*/ HomeBottomMenuWidget() ], ), ), ))); } } class CardHomePage extends StatefulWidget { const CardHomePage({Key? key}) : super(key: key); @override State createState() => _CardHomePageState(); } class _CardHomePageState extends State { @override void initState() { getProfile(context); super.initState(); } clearProfile() async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString("access_token", ""); Navigator.popUntil(context, ModalRoute.withName('/')); } getProfile(BuildContext context) { Api.get(context, Api.profile).then((value) => { if (value != null) {profile = Profile.fromJson(value["profile"]), refresh()} else {clearProfile()} }); } refresh() { setState(() {}); } @override Widget build(BuildContext context) { return Container( width: 376, decoration: BoxDecoration( borderRadius: BorderRadius.circular(6), boxShadow: [ BoxShadow( color: Color(0x19000000), blurRadius: 4, offset: Offset(1, 1), ), ], color: Colors.white, ), padding: const EdgeInsets.all(12), child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( width: double.infinity, child: Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( height: 24, child: Text( "Balance".tr(), style: GoogleFonts.kanit( color: Color(0xff050505), fontSize: 14, fontWeight: FontWeight.w300, ), ), ), Row( children: [ Text( Utils.moneyFormat(profile?.maxCardMembers.first.balance), style: GoogleFonts.kanit( color: Color(0xff050505), fontSize: 20, ), ), Text( " ${"Baht".tr()}", style: GoogleFonts.kanit( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), ), InkWell( child: Icon( Icons.refresh, size: 20, color: Color(0xffd0315a), ), onTap: () { getProfile(context); }, ), ], ), ], ), Expanded(child: Container()), InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, builder: (BuildContext context) { return SafeArea(child: TopUpMenuPage()); }); }, child: SizedBox( width: 160, child: Card( color: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(40), ), child: Container( padding: EdgeInsets.all(10), child: Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ SizedBox( height: 30, child: Text( "Wallet Topup".tr(), style: GoogleFonts.kanit( color: ColorCustom.red, fontSize: 18, ), ), ), Spacer(), Image( image: AssetImage('images/home/icon_plus.png'), fit: BoxFit.cover, color: ColorCustom.red, ) ], ), ), ), ), ), ], ), ), SizedBox(height: 10), Container( child: Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( width: 92, height: 24, child: Text( "Point".tr(), style: GoogleFonts.kanit( color: Color(0xff050505), fontSize: 14, fontWeight: FontWeight.w300, ), ), ), Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( width: 24, height: 24, child: Stack( children: [ Container( width: 24, height: 24, decoration: BoxDecoration( shape: BoxShape.circle, color: Color(0xffd0315a), ), ), Positioned( left: 8, top: 2, child: Text( "P", style: GoogleFonts.kanit( color: Colors.white, fontSize: 14, fontWeight: FontWeight.w500, ), ), ), ], ), ), SizedBox(width: 8), Row( children: [ Text( Utils.moneyFormat(profile?.maxCardMembers.first.pointBalance), style: GoogleFonts.kanit( color: Color(0xff050505), fontSize: 20, ), ), /* Text( " ${"Point".tr()}", style: GoogleFonts.kanit( color: Color(0xff65676b), fontSize: 16, fontWeight: FontWeight.w300, ), maxLines: 1, overflow: TextOverflow.ellipsis, ),*/ ], ), ], ), ], ), ), SizedBox(width: 20), InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, builder: (BuildContext context) { return SafeArea(child: PayPage()); }); }, child: SizedBox( width: 160, child: Card( color: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(40), ), child: Container( padding: EdgeInsets.all(10), child: Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.center, children: [ SizedBox( height: 30, child: Text( "Pay".tr(), style: GoogleFonts.kanit( color: ColorCustom.red, fontSize: 18, ), ), ), Spacer(), Container( width: 21, height: 21, child: Image( image: AssetImage('images/home/icon_qr.png'), fit: BoxFit.cover, color: ColorCustom.red, ), ) ], ), ), ), ), ), ], ), ), ], ), ); } } class MenuHomePage extends StatefulWidget { const MenuHomePage({Key? key}) : super(key: key); @override State createState() => _MenuHomePageState(); } class _MenuHomePageState extends State { @override Widget build(BuildContext context) { return Container( child: Column( children: [ Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ Expanded( child: InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, builder: (BuildContext context) { return GetPaidPage(); }); }, child: Image( width: 80, height: 80, image: context.locale.toString() == "th" ? AssetImage('images/menu/27.png') : AssetImage('images/menu/22.png'), fit: BoxFit.fitHeight, ), ), ), /* InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, builder: (BuildContext context) { return GetPaidShopByAmount(); }); }, child: Container( width: 85, height: 85, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.white, ), child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Image( width: 50, height: 50, image: AssetImage('images/home/1.png'), fit: BoxFit.cover, ), Text( "SHOP", textAlign: TextAlign.center, style: GoogleFonts.kanit( color: Color(0xff65676b), fontSize: 13, ), ), ], ), ), ),*/ SizedBox( width: 10, ), Expanded( child: InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, builder: (BuildContext context) { return TransferMoneyMenuPage(); }); }, child: Image( width: 80, height: 80, image: context.locale.toString() == "th" ? AssetImage('images/menu/26.png') : AssetImage('images/menu/23.png'), fit: BoxFit.fitHeight, ), ), ), SizedBox( width: 10, ), Expanded( child: InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, builder: (BuildContext context) { return PayPage(); }); }, child: Image( width: 80, height: 80, image: context.locale.toString() == "th" ? AssetImage('images/menu/25.png') : AssetImage('images/menu/24.png'), fit: BoxFit.fitHeight, ), ), ), /* InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, builder: (BuildContext context) { return SafeArea(child: WithDrawMenuPage()); }); }, child: Container( width: 85, height: 85, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.white, ), child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Image( width: 50, height: 50, image: AssetImage('images/home/3.png'), fit: BoxFit.cover, ), Text( "ถอนเงิน", textAlign: TextAlign.center, style: GoogleFonts.kanit( color: Color(0xff65676b), fontSize: 13, ), ), ], ), ), ), SizedBox( width: 10, ), InkWell( onTap: () { showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, builder: (BuildContext context) { return EFXPage(); }); }, child: Container( width: 85, height: 85, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.white, ), child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Image( width: 50, height: 50, image: AssetImage('images/home/1.png'), fit: BoxFit.cover, ), Text( "eFx", textAlign: TextAlign.center, style: GoogleFonts.kanit( color: Color(0xff65676b), fontSize: 13, ), ), ], ), ), ),*/ ], ), /* SizedBox( height: 15, ), SingleChildScrollView( scrollDirection: Axis.horizontal, child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ SizedBox( width: 10, ), InkWell( // onTap: () { showModalBottomSheet( // context: context, // isScrollControlled: true,useSafeArea: true, // builder: (BuildContext context) { // return PointMenuPage(); // });}, child: Container( width: 85, height: 85, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.white, ), child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Image( width: 50, height: 50, image: AssetImage('images/home/6.png'), fit: BoxFit.cover, ), Text( "Point Transfer", textAlign: TextAlign.center, style: GoogleFonts.kanit( color: Color(0xff65676b), fontSize: 13, ), ), ], ), ), ), InkWell( onTap: () {}, child: Container( width: 85, height: 85, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.transparent, ), ), ), InkWell( onTap: () {}, child: Container( width: 85, height: 85, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.transparent, ), ), ), ], ), )*/ ], ), ); } } class PromotionPage extends StatefulWidget { const PromotionPage({Key? key}) : super(key: key); @override State createState() => _PromotionPageState(); } class _PromotionPageState extends State { @override Widget build(BuildContext context) { return Container(); } }