support formData

master
小赖 4 years ago
parent 38c7b0431c
commit 8915150d76

@ -38,6 +38,12 @@ class _MyHomePageState extends State<MyHomePage> {
Dio().get("https://www.baidu.com/ahefbawfbe.html").catchError((e) { Dio().get("https://www.baidu.com/ahefbawfbe.html").catchError((e) {
LoggerData.addData(e); LoggerData.addData(e);
}); });
Dio()
.post("https://www.baidu.com/ahefbawfbe.html",
data: FormData.fromMap({'test': 'test'}))
.catchError((e) {
LoggerData.addData(e);
});
Future.delayed( Future.delayed(
Duration(milliseconds: 300), Duration(milliseconds: 300),
() => PowerLogger.init(context), () => PowerLogger.init(context),

@ -1,5 +1,6 @@
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:power_logger/src/view/dio_error_view.dart';
class DioErrorBuilder extends StatefulWidget { class DioErrorBuilder extends StatefulWidget {
final DioError data; final DioError data;
@ -48,7 +49,14 @@ class _DioErrorBuilderState extends State<DioErrorBuilder> {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
ListTile( ListTile(
onTap: () {}, onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DioErrorView(data: widget.data),
),
);
},
title: Text( title: Text(
_request.path, _request.path,
style: TextStyle( style: TextStyle(

@ -1,3 +1,5 @@
import 'package:power_logger/power_logger.dart';
class LoggerData { class LoggerData {
static int _maxLength = 100; static int _maxLength = 100;
@ -9,11 +11,13 @@ class LoggerData {
} }
static addData(dynamic data) { static addData(dynamic data) {
if (_data.length < _maxLength) if (PowerLogger.debug) {
_data.insert(0, data); if (_data.length < _maxLength)
else { _data.insert(0, data);
_data.removeLast(); else {
_data.insert(0, data); _data.removeLast();
_data.insert(0, data);
}
} }
} }

@ -4,12 +4,19 @@ import 'package:flutter/material.dart';
import 'package:power_logger/power_logger.dart'; import 'package:power_logger/power_logger.dart';
class PowerLogger { class PowerLogger {
static init(BuildContext context) { static bool debug = true;
Overlay.of(context).insert(OverlayEntry(
builder: (context) { ///
return LoggerFAB(); ///
}, ///debug
)); static init(BuildContext context, {bool debug = true}) {
PowerLogger.debug = debug;
if (debug)
Overlay.of(context).insert(OverlayEntry(
builder: (context) {
return LoggerFAB();
},
));
} }
} }

@ -14,6 +14,15 @@ class _PowerLoggerViewState extends State<PowerLoggerView> {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text('Logger View'), title: Text('Logger View'),
actions: [
IconButton(
icon: Icon(Icons.clear_all_rounded),
onPressed: () {
LoggerData.clear();
setState(() {});
},
),
],
), ),
body: ListView.separated( body: ListView.separated(
padding: EdgeInsets.all(5), padding: EdgeInsets.all(5),

@ -0,0 +1,88 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:power_logger/src/view/box_view.dart';
import 'package:power_logger/src/view/table_view.dart';
import 'package:power_logger/src/view/title_view.dart';
class DioErrorView extends StatefulWidget {
final DioError data;
DioErrorView({Key key, @required this.data}) : super(key: key);
@override
_DioErrorViewState createState() => _DioErrorViewState();
}
class _DioErrorViewState extends State<DioErrorView> {
RequestOptions get _request => widget.data.request;
_buildBaseURL() {
return _request.baseUrl == null || _request.baseUrl.length == 0
? const SizedBox()
: BoxView(
title: Text('BaseURL'),
child: Text(_request.baseUrl),
);
}
_buildPath() {
return BoxView(
title: const Text('Path'),
child: Text(_request.path),
);
}
_buildStatus() {
return BoxView(
title: const Text('Type'),
child: Chip(
label: Text(widget.data.type.toString()),
),
);
}
_buildMap(dynamic params) {
if (params is FormData) {
Map<String, dynamic> formData = {};
formData.addEntries(params.fields);
return BoxView(
title: Text('FormData'),
child: TableView(map: formData),
);
}
return params?.isEmpty ?? true
? const SizedBox()
: BoxView(
title: Text('Params'),
child: TableView(map: params),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red[600],
title: Text(widget.data.request.path),
actions: [
Chip(
label: Text(widget.data.request.method),
backgroundColor: Colors.lightGreen,
),
SizedBox(width: 8),
],
),
body: ListView(
padding: EdgeInsets.symmetric(vertical: 20),
children: [
TitleView(title: Text('Request')),
_buildBaseURL(),
_buildPath(),
_buildMap(_request.headers),
_buildMap(_request.queryParameters),
_buildMap(_request.data),
TitleView(title: Text('Response')),
_buildStatus(),
],
),
);
}
}

@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_highlight/flutter_highlight.dart'; import 'package:flutter_highlight/flutter_highlight.dart';
@ -16,6 +18,7 @@ class DioResponseView extends StatefulWidget {
} }
class _DioResponseViewState extends State<DioResponseView> { class _DioResponseViewState extends State<DioResponseView> {
bool _showRawData = false;
RequestOptions get _request => widget.data.request; RequestOptions get _request => widget.data.request;
_buildBaseURL() { _buildBaseURL() {
return _request.baseUrl == null || _request.baseUrl.length == 0 return _request.baseUrl == null || _request.baseUrl.length == 0
@ -45,7 +48,15 @@ class _DioResponseViewState extends State<DioResponseView> {
); );
} }
_buildMap(Map<String, dynamic> params) { _buildMap(dynamic params) {
if (params is FormData) {
Map<String, dynamic> formData = {};
formData.addEntries(params.fields);
return BoxView(
title: Text('FormData'),
child: TableView(map: formData),
);
}
return params?.isEmpty ?? true return params?.isEmpty ?? true
? const SizedBox() ? const SizedBox()
: BoxView( : BoxView(
@ -55,30 +66,30 @@ class _DioResponseViewState extends State<DioResponseView> {
} }
_buildData() { _buildData() {
bool jsonFlag = true; if (widget.data.data is Map)
String json; return BoxView(
title: Text('Data'),
try { child: HighlightView(
json = prettyJson(widget.data.data); prettyJson(widget.data.data),
} catch (e) { language: 'json',
jsonFlag = false; theme: atomOneLightTheme,
} ),
return jsonFlag );
? BoxView( else
title: Text('Data'), return BoxView(
child: HighlightView( title: Text('Raw'),
json, child: HighlightView(
language: 'json', widget.data.data,
theme: atomOneLightTheme, language: 'html',
), theme: atomOneLightTheme,
) ),
: SizedBox(); );
} }
_buildRawData() { _buildRawData() {
return BoxView( return BoxView(
title: Text('Raw Data'), title: Text('Raw Data'),
child: SelectableText(prettyJson(widget.data.data)), child: SelectableText(widget.data.data),
); );
} }
@ -95,12 +106,14 @@ class _DioResponseViewState extends State<DioResponseView> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
backgroundColor: Colors.green[600],
title: Text(widget.data.request.path), title: Text(widget.data.request.path),
actions: [ actions: [
Chip( Chip(
label: Text(widget.data.request.method), label: Text(widget.data.request.method),
backgroundColor: Colors.lightGreen, backgroundColor: Colors.lightGreen,
), ),
SizedBox(width: 8),
], ],
), ),
body: ListView( body: ListView(
@ -115,8 +128,22 @@ class _DioResponseViewState extends State<DioResponseView> {
TitleView(title: Text('Response')), TitleView(title: Text('Response')),
_buildMap(widget.data.headers.map), _buildMap(widget.data.headers.map),
_buildStatus(), _buildStatus(),
_buildData(), SwitchListTile(
_buildRawData(), value: _showRawData,
title: Text('RawData'),
onChanged: (state) => setState(() => _showRawData = state),
),
AnimatedCrossFade(
firstChild: _buildRawData(),
secondChild: _buildData(),
crossFadeState: _showRawData
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
duration: Duration(milliseconds: 500),
firstCurve: Curves.easeInOutCubic,
secondCurve: Curves.easeInOutCubic,
sizeCurve: Curves.easeInOutCubic,
),
], ],
), ),
); );

Loading…
Cancel
Save