From 8915150d76574feecc3a28379b9b8c488d095d48 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Tue, 26 Jan 2021 17:29:33 +0800 Subject: [PATCH] support formData --- example/lib/main.dart | 6 ++ lib/src/builders/dio_error_builder.dart | 10 ++- lib/src/data/logger_data.dart | 14 ++-- lib/src/power_logger.dart | 19 ++++-- lib/src/power_logger_view.dart | 9 +++ lib/src/view/dio_error_view.dart | 88 +++++++++++++++++++++++++ lib/src/view/dio_response_view.dart | 71 +++++++++++++------- 7 files changed, 183 insertions(+), 34 deletions(-) create mode 100644 lib/src/view/dio_error_view.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 7aacf10..22acde5 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -38,6 +38,12 @@ class _MyHomePageState extends State { 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), diff --git a/lib/src/builders/dio_error_builder.dart b/lib/src/builders/dio_error_builder.dart index d4982fd..ec6c5f7 100644 --- a/lib/src/builders/dio_error_builder.dart +++ b/lib/src/builders/dio_error_builder.dart @@ -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 { mainAxisSize: MainAxisSize.min, children: [ ListTile( - onTap: () {}, + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => DioErrorView(data: widget.data), + ), + ); + }, title: Text( _request.path, style: TextStyle( diff --git a/lib/src/data/logger_data.dart b/lib/src/data/logger_data.dart index 5fe0dcb..4f23a2d 100644 --- a/lib/src/data/logger_data.dart +++ b/lib/src/data/logger_data.dart @@ -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); + } } } diff --git a/lib/src/power_logger.dart b/lib/src/power_logger.dart index 32cf6ca..b29522a 100644 --- a/lib/src/power_logger.dart +++ b/lib/src/power_logger.dart @@ -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(); + }, + )); } } diff --git a/lib/src/power_logger_view.dart b/lib/src/power_logger_view.dart index c29f8b6..736f1bf 100644 --- a/lib/src/power_logger_view.dart +++ b/lib/src/power_logger_view.dart @@ -14,6 +14,15 @@ class _PowerLoggerViewState extends State { 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), diff --git a/lib/src/view/dio_error_view.dart b/lib/src/view/dio_error_view.dart new file mode 100644 index 0000000..d27b7a7 --- /dev/null +++ b/lib/src/view/dio_error_view.dart @@ -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 { + 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 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(), + ], + ), + ); + } +} diff --git a/lib/src/view/dio_response_view.dart b/lib/src/view/dio_response_view.dart index c65448f..09d6a42 100644 --- a/lib/src/view/dio_response_view.dart +++ b/lib/src/view/dio_response_view.dart @@ -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 { + 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 { ); } - _buildMap(Map params) { + _buildMap(dynamic params) { + if (params is FormData) { + Map 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 { } _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 { 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 { 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, + ), ], ), );