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) {
LoggerData.addData(e);
});
Dio()
.post("https://www.baidu.com/ahefbawfbe.html",
data: FormData.fromMap({'test': 'test'}))
.catchError((e) {
LoggerData.addData(e);
});
Future.delayed(
Duration(milliseconds: 300),
() => PowerLogger.init(context),

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

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

@ -4,12 +4,19 @@ import 'package:flutter/material.dart';
import 'package:power_logger/power_logger.dart';
class PowerLogger {
static init(BuildContext context) {
Overlay.of(context).insert(OverlayEntry(
builder: (context) {
return LoggerFAB();
},
));
static bool debug = true;
///
///
///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(
appBar: AppBar(
title: Text('Logger View'),
actions: [
IconButton(
icon: Icon(Icons.clear_all_rounded),
onPressed: () {
LoggerData.clear();
setState(() {});
},
),
],
),
body: ListView.separated(
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:flutter/material.dart';
import 'package:flutter_highlight/flutter_highlight.dart';
@ -16,6 +18,7 @@ class DioResponseView extends StatefulWidget {
}
class _DioResponseViewState extends State<DioResponseView> {
bool _showRawData = false;
RequestOptions get _request => widget.data.request;
_buildBaseURL() {
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
? const SizedBox()
: BoxView(
@ -55,30 +66,30 @@ class _DioResponseViewState extends State<DioResponseView> {
}
_buildData() {
bool jsonFlag = true;
String json;
try {
json = prettyJson(widget.data.data);
} catch (e) {
jsonFlag = false;
}
return jsonFlag
? BoxView(
title: Text('Data'),
child: HighlightView(
json,
language: 'json',
theme: atomOneLightTheme,
),
)
: SizedBox();
if (widget.data.data is Map)
return BoxView(
title: Text('Data'),
child: HighlightView(
prettyJson(widget.data.data),
language: 'json',
theme: atomOneLightTheme,
),
);
else
return BoxView(
title: Text('Raw'),
child: HighlightView(
widget.data.data,
language: 'html',
theme: atomOneLightTheme,
),
);
}
_buildRawData() {
return BoxView(
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) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green[600],
title: Text(widget.data.request.path),
actions: [
Chip(
label: Text(widget.data.request.method),
backgroundColor: Colors.lightGreen,
),
SizedBox(width: 8),
],
),
body: ListView(
@ -115,8 +128,22 @@ class _DioResponseViewState extends State<DioResponseView> {
TitleView(title: Text('Response')),
_buildMap(widget.data.headers.map),
_buildStatus(),
_buildData(),
_buildRawData(),
SwitchListTile(
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