From 38c7b0431c56f2357b6ef1298009b9b4ff0586c0 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Fri, 22 Jan 2021 11:11:18 +0800 Subject: [PATCH] pretty json code --- example/lib/main.dart | 86 +------------------ example/pubspec.lock | 7 -- lib/src/builders/dio_error_builder.dart | 91 +++++++++++++++------ lib/src/builders/dio_responses_builder.dart | 53 ++++++------ lib/src/parser/unfocus_parser.dart | 16 ++-- lib/src/power_logger_view.dart | 4 +- lib/src/view/dio_response_view.dart | 29 ++++++- pubspec.lock | 7 -- pubspec.yaml | 1 - 9 files changed, 139 insertions(+), 155 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index f9a8a72..7aacf10 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -7,27 +7,10 @@ void main() { } class MyApp extends StatelessWidget { - // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', - theme: ThemeData( - // This is the theme of your application. - // - // Try running your application with "flutter run". You'll see the - // application has a blue toolbar. Then, without quitting the app, try - // changing the primarySwatch below to Colors.green and then invoke - // "hot reload" (press "r" in the console where you ran "flutter run", - // or simply save your changes to "hot reload" in a Flutter IDE). - // Notice that the counter didn't reset back to zero; the application - // is not restarted. - primarySwatch: Colors.blue, - // This makes the visual density adapt to the platform that you run - // the app on. For desktop platforms, the controls will be smaller and - // closer together (more dense) than on mobile platforms. - visualDensity: VisualDensity.adaptivePlatformDensity, - ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } @@ -35,16 +18,6 @@ class MyApp extends StatelessWidget { class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); - - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - final String title; @override @@ -52,19 +25,6 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - @override void initState() { super.initState(); @@ -75,6 +35,9 @@ class _MyHomePageState extends State { (e) { LoggerData.addData(e); }); + Dio().get("https://www.baidu.com/ahefbawfbe.html").catchError((e) { + LoggerData.addData(e); + }); Future.delayed( Duration(milliseconds: 300), () => PowerLogger.init(context), @@ -84,53 +47,10 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. return Scaffold( appBar: AppBar( - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. title: Text(widget.title), ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. - child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Invoke "debug painting" (press "p" in the console, choose the - // "Toggle Debug Paint" action from the Flutter Inspector in Android - // Studio, or the "Toggle Debug Paint" command in Visual Studio Code) - // to see the wireframe for each widget. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headline4, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. ); } } diff --git a/example/pubspec.lock b/example/pubspec.lock index 7a86dad..07c75dc 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,13 +1,6 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - animations: - dependency: transitive - description: - name: animations - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.1.2" async: dependency: transitive description: diff --git a/lib/src/builders/dio_error_builder.dart b/lib/src/builders/dio_error_builder.dart index f1342f0..d4982fd 100644 --- a/lib/src/builders/dio_error_builder.dart +++ b/lib/src/builders/dio_error_builder.dart @@ -11,35 +11,80 @@ class DioErrorBuilder extends StatefulWidget { class _DioErrorBuilderState extends State { RequestOptions get _request => widget?.data?.request; + + String renderErrText(DioErrorType type) { + switch (type) { + case DioErrorType.CONNECT_TIMEOUT: + return '连接超时'; + break; + case DioErrorType.SEND_TIMEOUT: + return '发送超时'; + break; + case DioErrorType.RECEIVE_TIMEOUT: + return '接收超时'; + break; + case DioErrorType.RESPONSE: + return '404 or Serve Side Error'; + break; + case DioErrorType.CANCEL: + return '取消连接'; + break; + case DioErrorType.DEFAULT: + return '未知错误'; + break; + } + return ''; + } + @override Widget build(BuildContext context) { if (widget.data == null) return ListTile(title: Text('NULL Dio Error')); return Material( - color: Colors.red.withOpacity(0.1), - child: ListTile( - onTap: () {}, - title: Text( - _request.path, - style: TextStyle( - color: Colors.black87, + clipBehavior: Clip.antiAlias, + borderRadius: BorderRadius.circular(5), + color: Colors.red[100], + elevation: 4, + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + ListTile( + onTap: () {}, + title: Text( + _request.path, + style: TextStyle( + color: Colors.black87, + ), + ), + subtitle: Text( + widget.data.message, + style: TextStyle( + color: Colors.black45, + fontWeight: FontWeight.w300, + ), + ), + trailing: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Chip( + backgroundColor: Colors.green.withOpacity(0.8), + label: Text(_request.method), + ), + ], + ), ), - ), - subtitle: Text( - widget.data.message, - style: TextStyle( - color: Colors.black45, - fontWeight: FontWeight.w300, + Row( + children: [ + SizedBox(width: 24), + Text(widget.data.type.toString()), + Spacer(), + Chip( + backgroundColor: Colors.red.withOpacity(0.3), + label: Text(renderErrText(widget.data.type)), + ), + SizedBox(width: 24), + ], ), - ), - trailing: Row( - mainAxisSize: MainAxisSize.min, - children: [ - Chip( - backgroundColor: Colors.green.withOpacity(0.8), - label: Text(_request.method), - ), - ], - ), + ], ), ); } diff --git a/lib/src/builders/dio_responses_builder.dart b/lib/src/builders/dio_responses_builder.dart index 3f10c9f..7c8bf47 100644 --- a/lib/src/builders/dio_responses_builder.dart +++ b/lib/src/builders/dio_responses_builder.dart @@ -1,4 +1,3 @@ -import 'package:animations/animations.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:power_logger/src/view/dio_response_view.dart'; @@ -16,32 +15,34 @@ class _DioResponseBuilderState extends State { @override Widget build(BuildContext context) { - return OpenContainer( - tappable: false, - closedBuilder: (context, action) { - return Material( - color: Colors.green.withOpacity(0.1), - child: ListTile( - onTap: action, - title: Text( - _request.path, - style: TextStyle( - color: Colors.black87, - ), - ), - trailing: Row( - mainAxisSize: MainAxisSize.min, - children: [ - Chip( - backgroundColor: Colors.green.withOpacity(0.8), - label: Text(_request.method), - ), - ], - ), + return Material( + clipBehavior: Clip.antiAlias, + elevation: 4, + borderRadius: BorderRadius.circular(5), + color: Colors.green[100], + child: ListTile( + onTap: () => Navigator.push( + context, + MaterialPageRoute( + builder: (context) => DioResponseView(data: widget.data), ), - ); - }, - openBuilder: (context, action) => DioResponseView(data: widget.data), + ), + title: Text( + _request.path, + style: TextStyle( + color: Colors.black87, + ), + ), + trailing: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Chip( + backgroundColor: Colors.green.withOpacity(0.8), + label: Text(_request.method), + ), + ], + ), + ), ); } } diff --git a/lib/src/parser/unfocus_parser.dart b/lib/src/parser/unfocus_parser.dart index 9f6b595..645fe92 100644 --- a/lib/src/parser/unfocus_parser.dart +++ b/lib/src/parser/unfocus_parser.dart @@ -6,11 +6,17 @@ class UnfocusParser extends StatelessWidget { @override Widget build(BuildContext context) { - return ListTile( - onTap: () {}, - title: Text(data.toString()), - trailing: Chip( - label: Text(data.runtimeType.toString()), + return Material( + elevation: 4, + color: Colors.white, + clipBehavior: Clip.antiAlias, + borderRadius: BorderRadius.circular(5), + child: ListTile( + onTap: () {}, + title: Text(data.toString()), + trailing: Chip( + label: Text(data.runtimeType.toString()), + ), ), ); } diff --git a/lib/src/power_logger_view.dart b/lib/src/power_logger_view.dart index 506c851..c29f8b6 100644 --- a/lib/src/power_logger_view.dart +++ b/lib/src/power_logger_view.dart @@ -15,7 +15,9 @@ class _PowerLoggerViewState extends State { appBar: AppBar( title: Text('Logger View'), ), - body: ListView.builder( + body: ListView.separated( + padding: EdgeInsets.all(5), + separatorBuilder: (_, __) => SizedBox(height: 5), itemBuilder: (context, index) { return LoggerDataParser.builder(LoggerData.data[index]); }, diff --git a/lib/src/view/dio_response_view.dart b/lib/src/view/dio_response_view.dart index 8a38e55..c65448f 100644 --- a/lib/src/view/dio_response_view.dart +++ b/lib/src/view/dio_response_view.dart @@ -1,8 +1,11 @@ import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_highlight/flutter_highlight.dart'; +import 'package:flutter_highlight/themes/atom-one-light.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'; +import 'package:pretty_json/pretty_json.dart'; class DioResponseView extends StatefulWidget { final Response data; @@ -52,9 +55,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(); + } + + _buildRawData() { return BoxView( - title: Text('Params'), - child: SelectableText(widget.data.data.toString()), + title: Text('Raw Data'), + child: SelectableText(prettyJson(widget.data.data)), ); } @@ -92,6 +116,7 @@ class _DioResponseViewState extends State { _buildMap(widget.data.headers.map), _buildStatus(), _buildData(), + _buildRawData(), ], ), ); diff --git a/pubspec.lock b/pubspec.lock index de70718..8d35956 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,13 +1,6 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - animations: - dependency: "direct main" - description: - name: animations - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.1.2" async: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 9dfe3e6..4989a94 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,7 +11,6 @@ dependencies: flutter: sdk: flutter dio: ^3.0.10 - animations: ^1.1.2 pretty_json: ^1.1.0 flutter_highlight: ^0.6.0