mobileapplicationPassvault/lib/view_pages/add_credentials.dart

330 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_passvault/api_provider/add_credentail_provider.dart';
import 'package:flutter_passvault/api_provider/connectivty_provider.dart';
import 'package:flutter_passvault/api_provider/my_credential_provider.dart';
import 'package:flutter_passvault/constant/constantfile.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/alertbox.dart';
import 'package:flutter_passvault/custom_widgets/new_reuse_textfield.dart';
import 'package:flutter_passvault/custom_widgets/reuse_textview.dart';
import 'package:flutter_passvault/custom_widgets/reuse_button.dart';
import 'package:flutter_passvault/view_pages/shared_preferance.dart';
import 'package:hive/hive.dart';
import 'package:provider/provider.dart';
import '../main.dart';
class AddCredentials extends StatefulWidget {
const AddCredentials({super.key});
@override
State<AddCredentials> createState() => _AddCredentialsState();
}
class _AddCredentialsState extends State<AddCredentials> {
Color color1 = ApiConstants.backgroundcolor;
String fontfamily = ApiConstants.fontFamily;
TextEditingController applicationnaecontroller = TextEditingController();
TextEditingController usrnamecontroller = TextEditingController();
TextEditingController pwdcontroller = TextEditingController();
TextEditingController commentcontroller = TextEditingController();
String? usename;
String? customValue = '';
String? storeduserid;
var isLoading = false;
int? autoIncrementKey;
@override
void initState() {
super.initState();
getKey();
}
Future<void> getKey() async {
String? value = await CustomSharedPreferences.getCustomValue();
String? storeuserid = await CustomSharedPreferences.getuserid();
setState(() {
customValue = value ?? '';
storeduserid = storeuserid;
print("customKey_value_is: $customValue");
});
}
@override
Widget build(BuildContext context) {
print("User_idddd_is: $storeduserid");
return Consumer<AddCredentialProvider>(
builder: (context, provider, _) => Scaffold(
appBar: AppBar(
backgroundColor: color1,
title: const Text(
'Add Credentials',
style: TextStyle(color: Colors.white),
),
iconTheme: const IconThemeData(color: Colors.white),
),
body: SafeArea(
child: Stack(
children: [
SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Padding(
padding: const EdgeInsets.only(top: 30.0, bottom: 30.0),
child: ReusableTextView(
text: 'Please fill the details',
// ignore: prefer_const_constructors
textStyle: TextStyle(
fontFamily: fontfamily,
color: Colors.black,
fontSize: 20,
),
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 0.0, bottom: 40.0),
child: Container(
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black,
width: 1.5, // Set the desired border width
),
),
),
child: NewReusableTextField(
// hintText: 'Application name',
labelText: "Application Name *",
hasBorder: false,
controller: applicationnaecontroller,
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 0.8, bottom: 40.0),
child: Container(
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black,
width: 1.5, // Set the desired border width
),
),
// Set the desired border radius
),
child: NewReusableTextField(
emailkeyboard: true,
hasBorder: false,
labelText: 'Username *',
controller: usrnamecontroller,
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 0.8, bottom: 40.0),
child: Container(
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black,
width: 1.5, // Set the desired border width
),
),
// Set the desired border radius
),
child: NewReusableTextField(
secure: true,
labelText: 'Password *',
// labelText: "Password",
hasBorder: false,
controller: pwdcontroller,
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 0.8, bottom: 40.0),
child: Container(
height: 100,
decoration: BoxDecoration(
border: Border.all(
color: const Color(0xff000000),
width: 1,
),
borderRadius: BorderRadius.circular(10.0)),
child: NewReusableTextField(
hasBorder: false,
labelText: 'Description (Optional)',
controller: commentcontroller,
),
),
//),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Padding(
padding:
const EdgeInsets.only(top: 20.0, bottom: 0.0),
child: ReusableButton(
text: 'CANCEL',
color: Colors.grey,
isRounded: true,
onPressed: () {
print("submit_pressed::");
Navigator.pop(context, true);
},
),
),
Padding(
padding:
const EdgeInsets.only(top: 20.0, bottom: 0.0),
child: ReusableButton(
text: 'SAVE',
color: const Color.fromARGB(255, 39, 121, 42),
isRounded: true,
onPressed: _submitCredentialsdata,
),
),
],
),
],
),
),
Visibility(
visible: isLoading,
child: const Center(
child: CircularProgressIndicator(),
))
],
),
// })
),
),
);
}
showAlertBox(String msg) {
timerrfun();
showDialog(
context: context,
builder: (BuildContext context) => CustomAlertBox(message: msg));
}
void fecthDetails1() async {
final internetprovider =
Provider.of<ConnectivityProvider>(context, listen: false);
if (internetprovider.isConnected) {
Provider.of<MyCredentialProvider>(context, listen: false)
.fetchMyCredential();
} else {
showAlertBox("No Internet Connectivity");
}
}
Future<void> _submitCredentialsdata() async {
if (timer != null) timer?.cancel();
print("submit_pressed::");
final applicationName = applicationnaecontroller.text;
final userName = usrnamecontroller.text;
final password = pwdcontroller.text;
if (applicationName.isEmpty || userName.isEmpty || password.isEmpty) {
showAlertBox("Please fill the fields.");
} else {
setState(() {
isLoading = true;
});
autoIncrementKey = await getNextAutoIncrementValue();
print("autoIncrementKey_iss: $autoIncrementKey");
saveDataOffline();
}
}
void saveDataOffline() async {
final data = Storedcredential(
id: autoIncrementKey!,
name: applicationnaecontroller.text,
username: usrnamecontroller.text,
password: pwdcontroller.text,
detail: commentcontroller.text,
createdBy: int.parse(storeduserid!),
modifiedBy: int.parse(storeduserid!),
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
isOfflinecreated: true,
isOfflineupdate: false,
isOfflinedelete: false,
isOfflineshare: false,
issync: false,
uid: 0,
sharedUserIds: [],
DeletedUserIds: []);
print("added_dataaa: $data");
Provider.of<HiveDataRepository>(context, listen: false)
.addOfflineData(data);
setState(() {
isLoading = true;
});
usrnamecontroller.clear();
pwdcontroller.clear();
applicationnaecontroller.clear();
commentcontroller.clear();
showAlertBox("Credential Added Successfully");
setState(() {
isLoading = false;
});
}
@override
void dispose() {
super.dispose();
usrnamecontroller.dispose();
pwdcontroller.dispose();
applicationnaecontroller.dispose();
commentcontroller.dispose();
}
Future<int> getNextAutoIncrementValue() async {
var counterBox = await Hive.openBox<int>('counterBox');
if (!counterBox.containsKey('counter')) {
counterBox.put('counter', 0);
}
int? counter = counterBox.get('counter');
counterBox.put('counter', counter! + 1);
await counterBox.close();
return counter;
}
}