DiscoverModule/lib/contacts_module/ui_screen/new_contacts.dart

538 lines
17 KiB
Dart
Raw Permalink Normal View History

2024-10-07 12:41:28 +00:00
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:discover_module/contacts_module/constants.dart';
2024-11-22 10:38:43 +00:00
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';
2024-10-07 12:41:28 +00:00
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';
2024-11-22 10:38:43 +00:00
import 'package:discover_module/contacts_module/provider_class/k2_provider/profile_storage.dart';
2024-10-07 12:41:28 +00:00
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<List<String>> ddlist = ValueNotifier<List<String>>([]);
String selectedValue = '';
2024-11-22 10:38:43 +00:00
2024-10-07 12:41:28 +00:00
String? selectedType;
final TextEditingController searchController = TextEditingController();
TextEditingController textEditingController = TextEditingController();
class Contacts1 extends StatefulWidget {
const Contacts1({Key? key}) : super(key: key);
@override
State<Contacts1> createState() => _Contacts1State();
}
class _Contacts1State extends State<Contacts1> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
bool _switchValue = false;
bool isOnline2 = true;
bool isSearchClickd = false;
List displayedHCPList = [];
List myContactHCPList = [];
final List<String> _selectedIndices = [];
final List<int> _selectedremoveIndices = [];
bool longpress = false;
bool longpressmy = false;
late List<dynamic> hcpdataList;
var hcpDataProvider1;
List<ConnectivityResult> _connectionStatus = [ConnectivityResult.none];
final Connectivity _connectivity = Connectivity();
late StreamSubscription<List<ConnectivityResult>> _connectivitySubscription;
2024-11-22 10:38:43 +00:00
bool offMode = false;
List<Map<String, String>> filterList = [];
2024-10-07 12:41:28 +00:00
get developer => null;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
initConnectivity();
_connectivitySubscription =
_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
2024-11-22 10:38:43 +00:00
2024-10-07 12:41:28 +00:00
hcpList();
});
}
Future<void> hcpList() async {
hcpDataProvider1 = Provider.of<hcpProvider>(context, listen: false);
await hcpDataProvider1.getHCPProvider();
final hcplist = hcpDataProvider1.list;
hcpdataList = hcplist;
}
2024-11-22 10:38:43 +00:00
void _onSwitchChanged(bool value) {
setState(() {
_switchValue = value; // Update the switch value
});
print("Switch value: $_switchValue"); // Optional: Log the switch value
}
2024-10-07 12:41:28 +00:00
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(
2024-11-22 10:38:43 +00:00
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;
});
}
2024-10-07 12:41:28 +00:00
return SafeArea(
child: Scaffold(
2024-11-22 10:38:43 +00:00
backgroundColor: Constants.bgwhitecolor,
2024-10-07 12:41:28 +00:00
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) {
2024-11-22 10:38:43 +00:00
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
selectedValue = value;
searchController.text = selectedValue;
});
});
2024-10-07 12:41:28 +00:00
},
2024-11-22 10:38:43 +00:00
decoration: const InputDecoration(
2024-10-07 12:41:28 +00:00
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))
],
),
2024-11-22 10:38:43 +00:00
endDrawer: FilterDrawer(_switchValue, searchController,
onValueChanged: updateSelectedValue,
onValueMultiChanged: updatemultivalue),
2024-10-07 12:41:28 +00:00
body: Consumer<hcpProvider>(
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: [
2024-11-22 10:38:43 +00:00
_offlineBanner(),
headerRow(),
const CustomSizedBox(
height: 5.0,
2024-10-07 12:41:28 +00:00
),
!_switchValue
? _connectionStatus[0]
.toString()
.contains("ConnectivityResult.none")
2024-11-22 10:38:43 +00:00
? hcpContactlistview(myContactHCPList, offMode)
2024-10-07 12:41:28 +00:00
: hcpContactlistview(displayedHCPList)
: hcpContactlistview(myContactHCPList),
contactsBottomdailog()
],
);
},
),
),
);
}
Map<String, dynamic> findRecordById(
List displayedHCPList, String selectedIndic) {
// print(
// "Gettt11111 ${displayedHCPList.firstWhere((element) => element['id'] == int.parse(selectedIndic))}");
return displayedHCPList
.firstWhere((element) => element['id'] == int.parse(selectedIndic));
}
2024-11-22 10:38:43 +00:00
hcpContactlistview(List HCPList, [offMode]) {
2024-10-07 12:41:28 +00:00
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(
2024-11-22 10:38:43 +00:00
mainAxisSize: MainAxisSize.min,
// mainAxisAlignment: MainAxisAlignment.end,
2024-10-07 12:41:28 +00:00
2024-11-22 10:38:43 +00:00
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,
2024-10-07 12:41:28 +00:00
),
),
2024-11-22 10:38:43 +00:00
],
2024-10-07 12:41:28 +00:00
),
2024-11-22 10:38:43 +00:00
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,
),
2024-10-07 12:41:28 +00:00
),
2024-11-22 10:38:43 +00:00
],
),
],
),
2024-10-07 12:41:28 +00:00
),
),
2024-11-22 10:38:43 +00:00
const Padding(
padding: EdgeInsets.only(left: 20.0, right: 20.0),
child: Divider(),
2024-10-07 12:41:28 +00:00
),
],
);
},
),
);
}
contactsBottomdailog() {
return Align(
alignment: Alignment.bottomCenter,
child: longpress
2024-11-22 10:38:43 +00:00
? CustomLongPressContainer(
switchValue: _switchValue,
selectedIndices: _selectedIndices,
selectedRemoveIndices: _selectedremoveIndices,
onAddToContacts: _onAddToContacts,
onRemoveFromContacts: _onRemoveFromContacts,
onDownload: _onDownload,
onCancel: _onCancel,
onSaveOffline: _onSaveOffline,
2024-10-07 12:41:28 +00:00
)
: Container(),
);
}
Future<void> initConnectivity() async {
late List<ConnectivityResult> 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<void> _updateConnectionStatus(List<ConnectivityResult> result) async {
setState(() {
_connectionStatus = result;
2024-11-22 10:38:43 +00:00
if (_connectionStatus[0].toString().contains("ConnectivityResult.none")) {
offMode = true; // Set to true when offline
} else {
offMode = false; // Set to false when online
}
2024-10-07 12:41:28 +00:00
});
// ignore: avoid_print
print('Connectivity changed: $_connectionStatus');
}
2024-11-22 10:38:43 +00:00
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<void> _onAddToContacts() async {
print("Selected: ${_selectedIndices}");
print("Selected: ${_selectedIndices.length}");
ProfileStorageService profileStorageService =
ProfileStorageService(context);
await profileStorageService.offlineProfileAddContact(
_selectedIndices, displayedHCPList);
displayAlert("Added");
}
Future<void> _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<void> _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
}
2024-10-07 12:41:28 +00:00
}