mirror of
https://github.com/foss42/apidash.git
synced 2025-08-06 13:51:20 +08:00
Refactor
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'settings_providers.dart';
|
||||
import 'ui_providers.dart';
|
||||
@ -9,7 +8,6 @@ import '../consts.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
final selectedIdStateProvider = StateProvider<String?>((ref) => null);
|
||||
final searchQueryProvider = StateProvider<String>((ref) => '');
|
||||
|
||||
final selectedRequestModelProvider = StateProvider<RequestModel?>((ref) {
|
||||
final selectedId = ref.watch(selectedIdStateProvider);
|
||||
@ -33,7 +31,7 @@ final StateNotifierProvider<CollectionStateNotifier, Map<String, RequestModel>?>
|
||||
class CollectionStateNotifier
|
||||
extends StateNotifier<Map<String, RequestModel>?> {
|
||||
CollectionStateNotifier(this.ref, this.hiveHandler) : super(null) {
|
||||
var status = filterRequests(ref.read(searchQueryProvider.notifier).state);
|
||||
var status = loadData();
|
||||
Future.microtask(() {
|
||||
if (status) {
|
||||
ref.read(requestSequenceProvider.notifier).state = [
|
||||
@ -250,37 +248,6 @@ class CollectionStateNotifier
|
||||
}
|
||||
}
|
||||
|
||||
bool filterRequests(String query) {
|
||||
if (query.isEmpty) {
|
||||
|
||||
loadData();
|
||||
return true;
|
||||
} else {
|
||||
// Filter requests based on the query
|
||||
final filteredRequests = state?.values.where((request) =>
|
||||
request.name.toLowerCase().contains(query.toLowerCase())).toList();
|
||||
|
||||
|
||||
if (filteredRequests != null && filteredRequests.isNotEmpty) {
|
||||
|
||||
Map<String, RequestModel> requestMap = {};
|
||||
|
||||
for (var request in filteredRequests) {
|
||||
requestMap[request.id] = request;
|
||||
}
|
||||
|
||||
state = requestMap;
|
||||
|
||||
} else {
|
||||
print("No matching requests found");
|
||||
}
|
||||
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveData() async {
|
||||
ref.read(saveDataStateProvider.notifier).state = true;
|
||||
final saveResponse = ref.read(settingsProvider).saveResponses;
|
||||
|
@ -23,3 +23,5 @@ final nameTextFieldFocusNodeProvider =
|
||||
});
|
||||
return focusNode;
|
||||
});
|
||||
|
||||
final searchQueryProvider = StateProvider<String>((ref) => '');
|
||||
|
@ -1,4 +1,3 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:apidash/providers/providers.dart';
|
||||
@ -67,33 +66,44 @@ class CollectionPane extends ConsumerWidget {
|
||||
style: kTextStyleButton,
|
||||
),
|
||||
),
|
||||
|
||||
],
|
||||
),
|
||||
),
|
||||
kVSpacer10,
|
||||
SizedBox(
|
||||
height:30,
|
||||
child:SearchBar(
|
||||
|
||||
onChanged: (value){
|
||||
if(value.isNotEmpty){
|
||||
Container(
|
||||
height: 30,
|
||||
margin: const EdgeInsets.only(right: 8),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: kBorderRadius8,
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.surfaceVariant,
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
kHSpacer5,
|
||||
Icon(
|
||||
Icons.filter_alt,
|
||||
size: 18,
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
kHSpacer5,
|
||||
Expanded(
|
||||
child: RawTextField(
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
hintText: "Filter by name or URL",
|
||||
onChanged: (value) {
|
||||
if (value.trim().isNotEmpty) {
|
||||
ref.read(searchQueryProvider.notifier).state = value;
|
||||
ref.read(collectionStateNotifierProvider.notifier)
|
||||
.filterRequests(value);
|
||||
print(value);
|
||||
}
|
||||
else{
|
||||
|
||||
}
|
||||
|
||||
},
|
||||
hintText: "search",
|
||||
leading: Icon(Icons.search),
|
||||
textStyle: MaterialStateTextStyle.resolveWith((states) => TextStyle(fontSize: 15.0)),
|
||||
elevation: MaterialStateProperty.all(2.0),
|
||||
)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
kVSpacer10,
|
||||
|
||||
const Expanded(
|
||||
child: RequestList(),
|
||||
),
|
||||
@ -134,7 +144,7 @@ class _RequestListState extends ConsumerState<RequestList> {
|
||||
final alwaysShowCollectionPaneScrollbar = ref.watch(settingsProvider
|
||||
.select((value) => value.alwaysShowCollectionPaneScrollbar));
|
||||
|
||||
return requestItems.isNotEmpty?Scrollbar(
|
||||
return Scrollbar(
|
||||
controller: controller,
|
||||
thumbVisibility: alwaysShowCollectionPaneScrollbar ? true : null,
|
||||
radius: const Radius.circular(12),
|
||||
@ -155,7 +165,6 @@ class _RequestListState extends ConsumerState<RequestList> {
|
||||
},
|
||||
itemBuilder: (context, index) {
|
||||
var id = requestSequence[index];
|
||||
print(requestItems[id]!);
|
||||
|
||||
return ReorderableDragStartListener(
|
||||
key: ValueKey(id),
|
||||
@ -170,7 +179,7 @@ class _RequestListState extends ConsumerState<RequestList> {
|
||||
);
|
||||
},
|
||||
),
|
||||
):Text("data");
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,14 +94,39 @@ class JsonSearchField extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextField(
|
||||
return RawTextField(
|
||||
controller: controller,
|
||||
onChanged: onChanged,
|
||||
style: kCodeStyle,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Search..',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RawTextField extends StatelessWidget {
|
||||
const RawTextField({
|
||||
super.key,
|
||||
this.onChanged,
|
||||
this.controller,
|
||||
this.hintText,
|
||||
this.style,
|
||||
});
|
||||
|
||||
final void Function(String)? onChanged;
|
||||
final TextEditingController? controller;
|
||||
final String? hintText;
|
||||
final TextStyle? style;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return TextField(
|
||||
controller: controller,
|
||||
onChanged: onChanged,
|
||||
style: style,
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
border: InputBorder.none,
|
||||
hintText: 'Search..',
|
||||
hintText: hintText,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user