import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_passvault/api_provider/connectivty_provider.dart'; import 'package:flutter_passvault/constant/constantfile.dart'; import 'package:flutter_passvault/custom_widgets/reuse_textview.dart'; import 'package:flutter_passvault/hive_storage/hive_repositary.dart'; import 'package:flutter_passvault/hive_storage/store_credential_model.dart'; import 'package:flutter_passvault/view_pages/shared_preferance.dart'; import 'package:flutter_passvault/view_pages/view_details_screen.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; class SharedCredentials extends StatefulWidget { const SharedCredentials({super.key}); @override State createState() => _SharedCredentialsState(); } class _SharedCredentialsState extends State { // Color color1 = HexColor("2a47a5"); Color color1 = ApiConstants.backgroundcolor; Color orangecolor = ApiConstants.orangecolor; String fontFamily = ApiConstants.fontFamily; var isLoading = false; String? username, userid; bool _switchValue = false; final TextEditingController _searchController = TextEditingController(); Timer? timer; //Timer? timer; @override void initState() { super.initState(); timer = Timer.periodic(Duration(seconds: 30), (timer) { refreshData(); }); refreshData(); getname(); } @override void didChangeDependencies() { super.didChangeDependencies(); print("didChangeDependencies"); refreshData(); // Fetch data when the widget is inserted into the widget tree } @override Widget build(BuildContext context) { final connetivity = Provider.of(context); return GestureDetector( onTap: () { print('Clicked outside'); FocusScope.of(context).unfocus(); }, child: Scaffold( backgroundColor: Colors.white, body: Column( children: [ Expanded( child: Consumer( builder: (context, storedProvider, _) { print("connetivity.isConnected: ${connetivity.isConnected}"); List storedcredential = storedProvider.getAllDataFromHive(); print("stored_hivedata_is: ${storedProvider.searchResults}"); // storedcredential // .sort((a, b) => b.updatedAt.compareTo(a.updatedAt)); storedcredential.sort((a, b) => a.name.toLowerCase().compareTo(b.name.toLowerCase())); List filteredCredentials = storedcredential .where((credential) => credential.name .toLowerCase() .contains(_searchController.text.toLowerCase())) .toList(); return Column( children: [ Row( mainAxisAlignment: MainAxisAlignment .center, // Align children to start and end of the row children: [ Expanded( child: Padding( padding: const EdgeInsets.only(left: 45.0), child: Text( _switchValue ? "My Credentials" : "All Credentials", textAlign: TextAlign.center, style: TextStyle( fontSize: 20.0, fontWeight: FontWeight.w500, fontStyle: FontStyle.normal, fontFamily: fontFamily, color: _switchValue ? Colors.black : Colors.black), ), ), ), // Expanded( Row( // Adjust alignment as needed mainAxisSize: MainAxisSize.min, children: [ // Text('All'), // const TextWithHorizontalLine(text: 'All'), _switchValue ? ReusableTextView( text: 'All', textStyle: const TextStyle( fontSize: 12, decoration: TextDecoration.lineThrough, decorationThickness: 0.85, ), // TODO: implement dispose ) : ReusableTextView( text: 'All', textStyle: const TextStyle( fontSize: 12, )), CupertinoSwitch( activeColor: orangecolor, value: _switchValue, onChanged: (value) { setState(() { print("Switch_value_is : $value"); _switchValue = value; }); }, ), Padding( padding: const EdgeInsets.only(right: 8.0), child: !_switchValue ? ReusableTextView( text: 'My', textStyle: const TextStyle( fontSize: 12, decoration: TextDecoration.lineThrough, decorationThickness: 0.85, ), ) : ReusableTextView( text: 'My', textStyle: const TextStyle( fontSize: 12, )), ) ], ), // ), ]), Padding( padding: const EdgeInsets.only( top: 8.0, bottom: 8.0, left: 20.0, right: 20.0), child: Center( child: TextField( controller: _searchController, onChanged: (value) { setState(() { Provider.of(context, listen: false) .searchInHive( value); // Update the search results }); }, // ignore: prefer_const_constructors decoration: InputDecoration( contentPadding: const EdgeInsets.symmetric( vertical: 9.0), // Adjust the vertical padding border: const OutlineInputBorder(), labelText: ' Search', prefixIcon: const Icon(Icons.search), )), ), ), Expanded( child: Padding( padding: const EdgeInsets.only(top: 0.0), child: RefreshIndicator( onRefresh: refreshData, child: ListView.builder( shrinkWrap: true, itemCount: filteredCredentials.length, itemBuilder: (context, index) { final mycrdentialappname = filteredCredentials[index]; String mydatetimevalue = mycrdentialappname.updatedAt.toString(); print( "Listhive_data_is : ${mycrdentialappname.name}, ${mycrdentialappname.updatedAt}"); DateTime dateTime = DateTime.parse(mydatetimevalue); String formattedDate = DateFormat('dd-MM-yyyy HH:mm ') .format(dateTime); print("date&Time_is : $formattedDate"); print("stored userid : $userid"); if (!_switchValue) { return mycrdentialappname.isOfflinedelete ? Container() : listview(mycrdentialappname, index); } else { if (userid == mycrdentialappname.createdBy .toString()) { // textdata = "My Credential"; return mycrdentialappname.isOfflinedelete ? Container() : listview(mycrdentialappname, index); } else { return Container(); } } })), ), ), ], ); //} }), ), ], ), floatingActionButton: FloatingActionButton( shape: const CircleBorder(), backgroundColor: const Color(0xFFF96731), onPressed: () async { Navigator.of(context, rootNavigator: true) .pushNamed("/Addcredential"); }, tooltip: 'Add Credential', child: const Icon( Icons.add, color: Colors.white, ), ), // ), ), ); } Future getname() async { String? username1 = await CustomSharedPreferences.getname(); String? userid1 = await CustomSharedPreferences.getuserid(); setState(() { username = username1; userid = userid1; }); print("value_isss: $username"); } Future refreshData() async { // Simulate data loading delay (you can fetch new data here). await Future.delayed(const Duration(seconds: 0)); setState(() { fecthDetails1(); }); } void fecthDetails1() async { Provider.of(context, listen: false) .getAllDataFromHive(); } @override void dispose() { // TODO: implement dispose super.dispose(); timer!.cancel(); } listview(Storedcredential mycrdentialappname, int index) { String mydatetimevalue = mycrdentialappname.updatedAt.toString(); DateTime dateTime = DateTime.parse(mydatetimevalue); DateTime localTime = dateTime.toLocal(); String formattedDate = DateFormat('dd-MM-yyyy HH:mm ').format(localTime); print("date&Time_is : $formattedDate"); print("nameeee_is : ${mycrdentialappname.name}"); return Card( elevation: 2, surfaceTintColor: Colors.white, shape: const RoundedRectangleBorder( borderRadius: BorderRadius.all( Radius.circular(0.0), ), ), child: GestureDetector( onTap: () { print("nameeee_cred_id_is : ${mycrdentialappname.uid}"); print("nameeee_cred_id_is1 : ${mycrdentialappname.id}"); Navigator.push( context, MaterialPageRoute( builder: (context) => ViewDetilsScreen( jsondata: mycrdentialappname, index: index), )); }, child: ListTile( title: ReusableTextView( text: mycrdentialappname.name, // ignore: prefer_const_constructors textStyle: TextStyle( fontFamily: fontFamily, color: userid != mycrdentialappname.createdBy.toString() ? color1 : orangecolor, fontSize: 18, ), ), subtitle: ReusableTextView( text: "Last Updated on $formattedDate ", // ignore: prefer_const_constructors textStyle: TextStyle( fontFamily: fontFamily, color: Colors.grey.shade600, fontSize: 12, fontStyle: FontStyle.italic), ), trailing: Transform.rotate( angle: 3.141592, // 180 degrees in radians child: const Icon( Icons.arrow_back_ios_new, color: Colors.black45, ), ), ), ), ); } }