Button state provider and UI update

This commit is contained in:
Ankit Mahato
2023-03-10 14:19:15 +05:30
parent ce142450b6
commit 416572bdee
2 changed files with 72 additions and 29 deletions

View File

@ -5,16 +5,15 @@ import '../../utils/utils.dart';
import '../styles.dart';
import '../../consts.dart';
class EditorPaneRequestURLCard extends ConsumerStatefulWidget {
class EditorPaneRequestURLCard extends StatefulWidget {
const EditorPaneRequestURLCard({super.key});
@override
ConsumerState<EditorPaneRequestURLCard> createState() =>
State<EditorPaneRequestURLCard> createState() =>
_EditorPaneRequestURLCardState();
}
class _EditorPaneRequestURLCardState
extends ConsumerState<EditorPaneRequestURLCard> {
class _EditorPaneRequestURLCardState extends State<EditorPaneRequestURLCard> {
@override
void initState() {
super.initState();
@ -22,7 +21,6 @@ class _EditorPaneRequestURLCardState
@override
Widget build(BuildContext context) {
final activeId = ref.watch(activeIdStateProvider);
return Card(
elevation: 0,
shape: RoundedRectangleBorder(
@ -37,38 +35,20 @@ class _EditorPaneRequestURLCardState
horizontal: 20,
),
child: Row(
children: [
const DropdownButtonHTTPMethod(),
const SizedBox(
children: const [
DropdownButtonHTTPMethod(),
SizedBox(
width: 20,
),
const Expanded(
Expanded(
child: URLTextField(),
),
const SizedBox(
SizedBox(
width: 20,
),
SizedBox(
height: 36,
child: FilledButton(
onPressed: () async {
ref
.read(collectionStateNotifierProvider.notifier)
.sendRequest(activeId!);
},
child: Row(
children: const [
Text(
"Send",
style: textStyleButton,
),
SizedBox(
width: 10,
),
Icon(size: 16, Icons.send),
],
),
),
child: SendRequestButton(),
),
],
),
@ -172,3 +152,65 @@ class _URLTextFieldState extends ConsumerState<URLTextField> {
);
}
}
class SendRequestButton extends ConsumerStatefulWidget {
const SendRequestButton({
super.key,
});
@override
ConsumerState<SendRequestButton> createState() => _SendRequestButtonState();
}
class _SendRequestButtonState extends ConsumerState<SendRequestButton> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final activeId = ref.watch(activeIdStateProvider);
final sentRequestId = ref.watch(sentRequestIdStateProvider);
bool disable = sentRequestId != null;
return FilledButton(
onPressed: disable
? null
: () async {
ref
.read(sentRequestIdStateProvider.notifier)
.update((state) => activeId);
await Future.delayed(
const Duration(seconds: 5),
() {
ref
.read(collectionStateNotifierProvider.notifier)
.sendRequest(activeId!);
},
);
ref
.read(sentRequestIdStateProvider.notifier)
.update((state) => null);
},
child: Row(
children: [
Text(
disable
? (activeId == sentRequestId ? "Sending.." : "Busy")
: "Send",
style: textStyleButton,
),
if (!disable)
const SizedBox(
width: 10,
),
if (!disable)
const Icon(
size: 16,
Icons.send,
),
],
),
);
}
}

View File

@ -7,6 +7,7 @@ import '../consts.dart';
const _uuid = Uuid();
final activeIdStateProvider = StateProvider<String?>((ref) => null);
final sentRequestIdStateProvider = StateProvider<String?>((ref) => null);
final StateNotifierProvider<CollectionStateNotifier, List<RequestModel>>
collectionStateNotifierProvider =