import 'dart:async'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:discover_module/contacts_module/constants.dart'; import 'package:discover_module/contacts_module/custom_widget/custiom_profilepic.dart'; import 'package:discover_module/contacts_module/custom_widget/custom_bottomdailog.dart'; import 'package:discover_module/contacts_module/custom_widget/custom_sizedbox.dart'; import 'package:discover_module/contacts_module/custom_widget/custom_switch.dart'; import 'package:discover_module/contacts_module/custom_widget/show_alert.dart'; import 'package:discover_module/contacts_module/custom_widget/text.dart'; import 'package:discover_module/contacts_module/hive_fun.dart'; import 'package:discover_module/contacts_module/provider_class/hcp%20_provider.dart'; import 'package:discover_module/contacts_module/provider_class/k2_provider/profile_storage.dart'; import 'package:discover_module/contacts_module/ui_screen/contact_filters.dart'; import 'package:discover_module/contacts_module/ui_screen/interactionform/util.dart'; import 'package:discover_module/contacts_module/ui_screen/new_new_profile.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:flutter/services.dart'; final ValueNotifier> ddlist = ValueNotifier>([]); String selectedValue = ''; String? selectedType; final TextEditingController searchController = TextEditingController(); TextEditingController textEditingController = TextEditingController(); class Contacts1 extends StatefulWidget { const Contacts1({Key? key}) : super(key: key); @override State createState() => _Contacts1State(); } class _Contacts1State extends State { final GlobalKey _scaffoldKey = GlobalKey(); bool _switchValue = false; bool isOnline2 = true; bool isSearchClickd = false; List displayedHCPList = []; List myContactHCPList = []; final List _selectedIndices = []; final List _selectedremoveIndices = []; bool longpress = false; bool longpressmy = false; late List hcpdataList; var hcpDataProvider1; List _connectionStatus = [ConnectivityResult.none]; final Connectivity _connectivity = Connectivity(); late StreamSubscription> _connectivitySubscription; bool offMode = false; List> filterList = []; get developer => null; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((timeStamp) { initConnectivity(); _connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); hcpList(); }); } Future hcpList() async { hcpDataProvider1 = Provider.of(context, listen: false); await hcpDataProvider1.getHCPProvider(); final hcplist = hcpDataProvider1.list; hcpdataList = hcplist; } void _onSwitchChanged(bool value) { setState(() { _switchValue = value; // Update the switch value }); print("Switch value: $_switchValue"); // Optional: Log the switch value } @override Widget build(BuildContext context) { SystemChrome.setSystemUIOverlayStyle( const SystemUiOverlayStyle(statusBarColor: Constants.k2color)); void updateSelectedValue(String newValue) { setState(() { selectedValue = newValue; searchController.text = selectedValue; }); } void updatemultivalue(String newValue) { print("FilterdList_isss: ${newValue}"); setState(() { selectedValue = newValue; searchController.text = selectedValue; }); } return SafeArea( child: Scaffold( backgroundColor: Constants.bgwhitecolor, key: _scaffoldKey, // Assign the key to the Scaffold appBar: AppBar( automaticallyImplyLeading: false, title: isSearchClickd ? Container( height: 40, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(5.0)), child: TextField( controller: searchController, onChanged: (value) { WidgetsBinding.instance.addPostFrameCallback((_) { setState(() { selectedValue = value; searchController.text = selectedValue; }); }); }, decoration: const InputDecoration( fillColor: Constants.k2color, contentPadding: EdgeInsets.symmetric(vertical: 9.0), border: OutlineInputBorder(), hintText: "Search", // labelText: ' Search', prefixIcon: Icon( Icons.search, ), ), ), ) : Text1(title: "Contacts"), actions: [ IconButton( onPressed: () { setState(() { isSearchClickd = !isSearchClickd; if (!isSearchClickd) { searchController.clear(); } }); }, icon: Icon(isSearchClickd ? Icons.close : Icons.search)) ], ), endDrawer: FilterDrawer(_switchValue, searchController, onValueChanged: updateSelectedValue, onValueMultiChanged: updatemultivalue), body: Consumer( builder: (context, hcpProvider, child) { print("_selectedValueConsumerConsumer_isss: $selectedValue"); if (selectedValue.isNotEmpty) { searchController.text = selectedValue; } else { searchController.text = ""; } !_switchValue ? displayedHCPList = hcpProvider.searchHCP(searchController.text) : myContactHCPList = HiveFunctions.getindexUser(searchController.text); return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _offlineBanner(), headerRow(), const CustomSizedBox( height: 5.0, ), !_switchValue ? _connectionStatus[0] .toString() .contains("ConnectivityResult.none") ? hcpContactlistview(myContactHCPList, offMode) : hcpContactlistview(displayedHCPList) : hcpContactlistview(myContactHCPList), contactsBottomdailog() ], ); }, ), ), ); } Map findRecordById( List displayedHCPList, String selectedIndic) { // print( // "Gettt11111 ${displayedHCPList.firstWhere((element) => element['id'] == int.parse(selectedIndic))}"); return displayedHCPList .firstWhere((element) => element['id'] == int.parse(selectedIndic)); } hcpContactlistview(List HCPList, [offMode]) { return Expanded( child: ListView.builder( itemCount: HCPList.length, itemBuilder: (BuildContext context, int index) { var data = HCPList[index]; print("CheckinggggImage_path: ${data['img_path']}"); return Column( mainAxisSize: MainAxisSize.min, // mainAxisAlignment: MainAxisAlignment.end, children: [ Padding( padding: const EdgeInsets.only(left: 8.0, right: 8.0), child: ListTile( onLongPress: () { print("LongPresss"); setState(() { longpress = true; }); }, onTap: () async { print("Check_data11 = ${offMode}"); offMode != null ? Navigator.push( context, MaterialPageRoute( builder: (context) => NewProfile1(text: data, offlinemode: offMode), ), ) : Navigator.push( context, MaterialPageRoute( builder: (context) => NewProfile1(text: data), ), ); }, leading: CustomProfile( imgstring: data["img_path"], fontsize: 12.0, radius: 30, name: data["First Name"], ), trailing: Visibility( visible: longpress, child: !_switchValue ? Checkbox( value: _selectedIndices .contains(data["id"].toString()), onChanged: (value) { setState(() { if (value!) { print("Datata_issss_iddd: ${data["id"]}"); _selectedIndices.add(data["id"].toString()); } else { _selectedIndices .remove(data["id"].toString()); } print("id_total123: ${_selectedIndices}"); }); }) : Checkbox( value: _selectedremoveIndices.contains(data["id"]), onChanged: (value) { setState(() { if (value!) { _selectedremoveIndices.add(data["id"]); } else { _selectedremoveIndices.remove(data["id"]); } print("id_total: ${_selectedremoveIndices}"); }); }), ), title: Text1( title: "Dr. " + data['name'], fontweight: FontWeight.bold, txtfont: isTablet ? 22 : 15, ), subtitle: Column( children: [ Row( children: [ Expanded( child: Text1( title: data["speciality"] ?? data['spl'], fontweight: FontWeight.normal, txtcolor: Colors.black, txtfont: isTablet ? 20 : 13, ), ), ], ), const CustomSizedBox( height: 10.0, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( child: Text1( title: "Added by Pooja k on 01/07/2023", fontweight: FontWeight.normal, txtfont: 12, ), ), ], ), ], ), ), ), const Padding( padding: EdgeInsets.only(left: 20.0, right: 20.0), child: Divider(), ), ], ); }, ), ); } contactsBottomdailog() { return Align( alignment: Alignment.bottomCenter, child: longpress ? CustomLongPressContainer( switchValue: _switchValue, selectedIndices: _selectedIndices, selectedRemoveIndices: _selectedremoveIndices, onAddToContacts: _onAddToContacts, onRemoveFromContacts: _onRemoveFromContacts, onDownload: _onDownload, onCancel: _onCancel, onSaveOffline: _onSaveOffline, ) : Container(), ); } Future initConnectivity() async { late List result; // Platform messages may fail, so we use a try/catch PlatformException. try { result = await _connectivity.checkConnectivity(); } on PlatformException catch (e) { developer.log('Couldn\'t check connectivity status', error: e); return; } if (!mounted) { return Future.value(null); } return _updateConnectionStatus(result); } Future _updateConnectionStatus(List result) async { setState(() { _connectionStatus = result; if (_connectionStatus[0].toString().contains("ConnectivityResult.none")) { offMode = true; // Set to true when offline } else { offMode = false; // Set to false when online } }); // ignore: avoid_print print('Connectivity changed: $_connectionStatus'); } headerRow() { return Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Wrap( children: [ Padding( padding: const EdgeInsets.only(left: 8.0, right: 2.0), child: CustomSwitch( activeclor: Constants.k2color, switchvalue: _switchValue, onchanged: _onSwitchChanged, ), ), Padding( padding: const EdgeInsets.only(right: 8.0, top: 9.0), child: Text1( title: 'My Contacts', txtfont: 15, txtcolor: !_switchValue ? Colors.grey : Colors.black, )) ], ), Row( children: [ GestureDetector( onTap: () { print("SearchVon: ${selectedType}"); setState(() { selectedValue = ''; selectedType = null; }); }, child: Text1(title: 'My Filters'), ), IconButton( onPressed: () async { _scaffoldKey.currentState?.openEndDrawer(); }, icon: const Icon( Icons.sort, size: 30, )), ], ) ], ); } Future _onAddToContacts() async { print("Selected: ${_selectedIndices}"); print("Selected: ${_selectedIndices.length}"); ProfileStorageService profileStorageService = ProfileStorageService(context); await profileStorageService.offlineProfileAddContact( _selectedIndices, displayedHCPList); displayAlert("Added"); } Future _onRemoveFromContacts() async { ProfileStorageService profileStorageService = ProfileStorageService(context); await profileStorageService .offlineProfileRemoveContact(_selectedremoveIndices); displayAlert("selected contact has been removed"); } void _onDownload() { // Handle download print("Download action triggered"); } void _onCancel() { setState(() { longpress = false; _selectedIndices.clear(); _selectedremoveIndices.clear(); }); } Future _onSaveOffline() async { // Handle save offline print("Save offline triggered"); print("Selected: ${_selectedIndices}"); ProfileStorageService profileStorageService = ProfileStorageService(context); await profileStorageService.offlineProfileK2Store( _selectedIndices, displayedHCPList); displayAlert("selected contact has been added to offline"); setState(() {}); } displayAlert(String data) { return showDialog( context: context, builder: (_) { return Alert( data: data, onPressed: () { Navigator.of(context).pop(); setState(() { _selectedIndices.clear(); _selectedremoveIndices.clear(); longpress = false; longpressmy = false; }); }, ); }); } Widget _offlineBanner() { return offMode ? Container( color: Colors.red, // Set the color of the banner padding: EdgeInsets.symmetric(vertical: 8.0), child: const Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.signal_wifi_off, color: Colors.white, size: 20, ), SizedBox(width: 8.0), Text( 'You are offline', style: TextStyle(color: Colors.white, fontSize: 16), ), ], ), ) : SizedBox.shrink(); // Empty widget if online } }