feat: add read-only support to authentication fields

This commit is contained in:
Udhay-Adithya
2025-06-25 22:49:20 +05:30
parent bf170e1446
commit 8d8940d57d
6 changed files with 33 additions and 2 deletions

View File

@@ -7,12 +7,14 @@ import 'package:flutter/material.dart';
class ApiKeyAuthFields extends StatefulWidget {
final AuthModel? authData;
final bool readOnly;
final Function(AuthModel?) updateAuth;
const ApiKeyAuthFields({
super.key,
required this.authData,
required this.updateAuth,
this.readOnly = false
});
@override
@@ -67,12 +69,14 @@ class _ApiKeyAuthFieldsState extends State<ApiKeyAuthFields> {
),
const SizedBox(height: 16),
AuthTextField(
readOnly: widget.readOnly,
controller: _nameController,
hintText: "Header/Query Param Name",
onChanged: (value) => _updateApiKeyAuth(),
),
const SizedBox(height: 16),
AuthTextField(
readOnly: widget.readOnly,
controller: _keyController,
hintText: "API Key",
isObscureText: true,

View File

@@ -5,11 +5,13 @@ import 'package:apidash_core/apidash_core.dart';
class BasicAuthFields extends StatelessWidget {
final AuthModel? authData;
final Function(AuthModel?) updateAuth;
final bool readOnly;
const BasicAuthFields({
super.key,
required this.authData,
required this.updateAuth,
this.readOnly = false,
});
@override
@@ -25,6 +27,7 @@ class BasicAuthFields extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AuthTextField(
readOnly: readOnly,
hintText: "Username",
controller: usernameController,
onChanged: (_) => _updateBasicAuth(
@@ -34,6 +37,7 @@ class BasicAuthFields extends StatelessWidget {
),
const SizedBox(height: 16),
AuthTextField(
readOnly: readOnly,
hintText: "Password",
isObscureText: true,
controller: passwordController,

View File

@@ -5,11 +5,13 @@ import 'package:flutter/material.dart';
class BearerAuthFields extends StatefulWidget {
final AuthModel? authData;
final Function(AuthModel?) updateAuth;
final bool readOnly;
const BearerAuthFields({
super.key,
required this.authData,
required this.updateAuth,
this.readOnly = false,
});
@override
@@ -29,6 +31,7 @@ class _BearerAuthFieldsState extends State<BearerAuthFields> {
@override
Widget build(BuildContext context) {
return AuthTextField(
readOnly: widget.readOnly,
controller: _tokenController,
hintText: "Token",
isObscureText: true,

View File

@@ -6,11 +6,13 @@ import 'package:apidash_core/apidash_core.dart';
class JwtAuthFields extends StatefulWidget {
final AuthModel? authData;
final Function(AuthModel?) updateAuth;
final bool readOnly;
const JwtAuthFields({
super.key,
required this.authData,
required this.updateAuth,
this.readOnly = false,
});
@override
@@ -95,6 +97,7 @@ class _JwtAuthFieldsState extends State<JwtAuthFields> {
),
const SizedBox(height: 16),
AuthTextField(
readOnly: widget.readOnly,
controller: _secretController,
isObscureText: true,
hintText: "Secret key",
@@ -130,6 +133,7 @@ class _JwtAuthFieldsState extends State<JwtAuthFields> {
),
SizedBox(height: 4),
TextField(
readOnly: widget.readOnly,
controller: _payloadController,
maxLines: 4,
decoration: InputDecoration(

View File

@@ -7,12 +7,14 @@ class AuthTextField extends StatefulWidget {
final TextEditingController controller;
final bool isObscureText;
final Function(String)? onChanged;
final bool readOnly;
const AuthTextField({
super.key,
required this.hintText,
required this.controller,
required this.onChanged,
this.readOnly = false,
this.isObscureText = false,
});
@@ -51,6 +53,7 @@ class _AuthFieldState extends State<AuthTextField> {
),
const SizedBox(height: 6),
TextFormField(
readOnly: widget.readOnly,
controller: widget.controller,
style: kCodeStyle.copyWith(
color: Theme.of(context).colorScheme.onSurface,

View File

@@ -1,3 +1,5 @@
import 'dart:developer';
import 'package:apidash/screens/common_widgets/auth/api_key_auth_fields.dart';
import 'package:apidash/screens/common_widgets/auth/basic_auth_fields.dart';
import 'package:apidash/screens/common_widgets/auth/bearer_auth_fields.dart';
@@ -24,17 +26,19 @@ class EditAuthType extends ConsumerWidget {
final APIAuthType currentAuthType;
if (authModel != null) {
log("Got Auth Model");
currentAuthData = authModel;
currentAuthType = authModel!.type;
} else {
log("Using Provider");
final selectedRequest = ref.read(selectedRequestModelProvider);
if (selectedRequest == null) {
return const SizedBox.shrink();
}
currentAuthType = ref.watch(
selectedRequestModelProvider.select((request) =>
request?.authModel?.type ?? APIAuthType.none),
selectedRequestModelProvider
.select((request) => request?.authModel?.type ?? APIAuthType.none),
);
currentAuthData = selectedRequest.authModel;
}
@@ -115,6 +119,11 @@ class EditAuthType extends ConsumerWidget {
AuthModel? authData,
) {
void updateAuth(AuthModel? model) {
if (model == null) {
ref.read(collectionStateNotifierProvider.notifier).update(
authData: AuthModel(type: APIAuthType.none),
);
}
ref.read(collectionStateNotifierProvider.notifier).update(
authData: model,
);
@@ -123,21 +132,25 @@ class EditAuthType extends ConsumerWidget {
switch (authData?.type) {
case APIAuthType.basic:
return BasicAuthFields(
readOnly: readOnly,
authData: authData,
updateAuth: updateAuth,
);
case APIAuthType.bearer:
return BearerAuthFields(
readOnly: readOnly,
authData: authData,
updateAuth: updateAuth,
);
case APIAuthType.apiKey:
return ApiKeyAuthFields(
readOnly: readOnly,
authData: authData,
updateAuth: updateAuth,
);
case APIAuthType.jwt:
return JwtAuthFields(
readOnly: readOnly,
authData: authData,
updateAuth: updateAuth,
);