mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 18:57:05 +08:00
SSE InitalResponseFix & SSEDisplay SmartAutoScroll
This commit is contained in:
@@ -397,10 +397,14 @@ class CollectionStateNotifier
|
|||||||
final statusCode = response.statusCode;
|
final statusCode = response.statusCode;
|
||||||
|
|
||||||
respModel = respModel ??
|
respModel = respModel ??
|
||||||
baseHttpResponseModel.fromResponse(
|
baseHttpResponseModel
|
||||||
response: response,
|
.fromResponse(
|
||||||
time: duration,
|
response: response,
|
||||||
);
|
time: duration,
|
||||||
|
)
|
||||||
|
.copyWith(
|
||||||
|
sseOutput: isTextStream ? [response.body] : [],
|
||||||
|
);
|
||||||
|
|
||||||
newRequestModel = newRequestModel.copyWith(
|
newRequestModel = newRequestModel.copyWith(
|
||||||
responseStatus: statusCode,
|
responseStatus: statusCode,
|
||||||
|
|||||||
@@ -151,10 +151,9 @@ class _ResponseBodySuccessState extends State<ResponseBodySuccess> {
|
|||||||
width: double.maxFinite,
|
width: double.maxFinite,
|
||||||
padding: kP8,
|
padding: kP8,
|
||||||
decoration: textContainerdecoration,
|
decoration: textContainerdecoration,
|
||||||
child: SingleChildScrollView(
|
child: SSEDisplay(
|
||||||
child: SSEDisplay(
|
|
||||||
sseOutput: widget.body,
|
sseOutput: widget.body,
|
||||||
)),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
@@ -167,16 +166,67 @@ class _ResponseBodySuccessState extends State<ResponseBodySuccess> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//MOVE THIS SOMEWHERE ELSE
|
//MOVE THIS SOMEWHERE ELSE
|
||||||
class SSEDisplay extends StatelessWidget {
|
class SSEDisplay extends StatefulWidget {
|
||||||
final String sseOutput;
|
final String sseOutput;
|
||||||
const SSEDisplay({super.key, required this.sseOutput});
|
const SSEDisplay({super.key, required this.sseOutput});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<SSEDisplay> createState() => _SSEDisplayState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SSEDisplayState extends State<SSEDisplay> {
|
||||||
|
final _scrollController = ScrollController();
|
||||||
|
bool autoScrollEnabled = true;
|
||||||
|
bool _isScrolling = false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
|
||||||
|
_scrollController.addListener(() {
|
||||||
|
final position = _scrollController.position;
|
||||||
|
final atBottom = position.pixels >= position.maxScrollExtent - 50;
|
||||||
|
|
||||||
|
if (autoScrollEnabled && !atBottom) {
|
||||||
|
// User scrolled up manually
|
||||||
|
setState(() => autoScrollEnabled = false);
|
||||||
|
} else if (!autoScrollEnabled && atBottom) {
|
||||||
|
// User scrolled back to bottom
|
||||||
|
setState(() => autoScrollEnabled = true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didUpdateWidget(covariant SSEDisplay oldWidget) {
|
||||||
|
super.didUpdateWidget(oldWidget);
|
||||||
|
if (oldWidget.sseOutput != widget.sseOutput &&
|
||||||
|
autoScrollEnabled &&
|
||||||
|
!_isScrolling) {
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||||
|
if (_scrollController.hasClients) {
|
||||||
|
_isScrolling = true;
|
||||||
|
_scrollController.jumpTo(
|
||||||
|
_scrollController.position.maxScrollExtent,
|
||||||
|
);
|
||||||
|
_isScrolling = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_scrollController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
List<dynamic> sse;
|
List<dynamic> sse;
|
||||||
try {
|
try {
|
||||||
sse = jsonDecode(sseOutput);
|
sse = jsonDecode(widget.sseOutput);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return Text(
|
return Text(
|
||||||
'Invalid SSE output',
|
'Invalid SSE output',
|
||||||
@@ -184,59 +234,64 @@ class SSEDisplay extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Column(
|
return SingleChildScrollView(
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
controller: _scrollController,
|
||||||
children: sse.map<Widget>((chunk) {
|
child: Column(
|
||||||
Map<String, dynamic>? parsedJson;
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
try {
|
children: sse.map<Widget>((chunk) {
|
||||||
parsedJson = jsonDecode(chunk);
|
Map<String, dynamic>? parsedJson;
|
||||||
} catch (_) {
|
try {
|
||||||
// Not a JSON object
|
parsedJson = jsonDecode(chunk);
|
||||||
}
|
} catch (_) {}
|
||||||
|
|
||||||
return Card(
|
return Card(
|
||||||
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 8),
|
margin: const EdgeInsets.symmetric(vertical: 6, horizontal: 8),
|
||||||
elevation: 2,
|
elevation: 2,
|
||||||
shape:
|
shape: RoundedRectangleBorder(
|
||||||
RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
borderRadius: BorderRadius.circular(12),
|
||||||
child: Padding(
|
),
|
||||||
padding: const EdgeInsets.all(12.0),
|
child: Padding(
|
||||||
child: parsedJson != null
|
padding: const EdgeInsets.all(12.0),
|
||||||
? Column(
|
child: parsedJson != null
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
? Column(
|
||||||
children: parsedJson.entries.map((entry) {
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
return Padding(
|
children: parsedJson.entries.map((entry) {
|
||||||
padding: const EdgeInsets.symmetric(vertical: 2.0),
|
return Padding(
|
||||||
child: Row(
|
padding: const EdgeInsets.symmetric(vertical: 2.0),
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
child: Row(
|
||||||
children: [
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
Text(
|
children: [
|
||||||
'${entry.key}: ',
|
Text(
|
||||||
style: theme.textTheme.bodyMedium?.copyWith(
|
'${entry.key}: ',
|
||||||
|
style: theme.textTheme.bodyMedium?.copyWith(
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
color: kColorGQL),
|
color: kColorGQL,
|
||||||
),
|
),
|
||||||
SizedBox(width: 4),
|
|
||||||
Expanded(
|
|
||||||
child: Text(
|
|
||||||
entry.value.toString(),
|
|
||||||
style: theme.textTheme.bodyMedium
|
|
||||||
?.copyWith(fontFamily: 'monospace'),
|
|
||||||
),
|
),
|
||||||
),
|
const SizedBox(width: 4),
|
||||||
],
|
Expanded(
|
||||||
),
|
child: Text(
|
||||||
);
|
entry.value.toString(),
|
||||||
}).toList(),
|
style: theme.textTheme.bodyMedium?.copyWith(
|
||||||
)
|
fontFamily: 'monospace',
|
||||||
: Text(
|
),
|
||||||
chunk.toString(),
|
),
|
||||||
style: theme.textTheme.bodyMedium
|
),
|
||||||
?.copyWith(fontFamily: 'monospace'),
|
],
|
||||||
),
|
),
|
||||||
),
|
);
|
||||||
);
|
}).toList(),
|
||||||
}).toList(),
|
)
|
||||||
|
: Text(
|
||||||
|
chunk.toString(),
|
||||||
|
style: theme.textTheme.bodyMedium?.copyWith(
|
||||||
|
fontFamily: 'monospace',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user