pretty json code

master
小赖 4 years ago
parent c972545147
commit 38c7b0431c

@ -7,27 +7,10 @@ void main() {
} }
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', 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'), home: MyHomePage(title: 'Flutter Demo Home Page'),
); );
} }
@ -35,16 +18,6 @@ class MyApp extends StatelessWidget {
class MyHomePage extends StatefulWidget { class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key); 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; final String title;
@override @override
@ -52,19 +25,6 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
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 @override
void initState() { void initState() {
super.initState(); super.initState();
@ -75,6 +35,9 @@ class _MyHomePageState extends State<MyHomePage> {
(e) { (e) {
LoggerData.addData(e); LoggerData.addData(e);
}); });
Dio().get("https://www.baidu.com/ahefbawfbe.html").catchError((e) {
LoggerData.addData(e);
});
Future.delayed( Future.delayed(
Duration(milliseconds: 300), Duration(milliseconds: 300),
() => PowerLogger.init(context), () => PowerLogger.init(context),
@ -84,53 +47,10 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { 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( return Scaffold(
appBar: AppBar( 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), 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: <Widget>[
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.
); );
} }
} }

@ -1,13 +1,6 @@
# Generated by pub # Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile # See https://dart.dev/tools/pub/glossary#lockfile
packages: packages:
animations:
dependency: transitive
description:
name: animations
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.2"
async: async:
dependency: transitive dependency: transitive
description: description:

@ -11,12 +11,43 @@ class DioErrorBuilder extends StatefulWidget {
class _DioErrorBuilderState extends State<DioErrorBuilder> { class _DioErrorBuilderState extends State<DioErrorBuilder> {
RequestOptions get _request => widget?.data?.request; 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (widget.data == null) return ListTile(title: Text('NULL Dio Error')); if (widget.data == null) return ListTile(title: Text('NULL Dio Error'));
return Material( return Material(
color: Colors.red.withOpacity(0.1), clipBehavior: Clip.antiAlias,
child: ListTile( borderRadius: BorderRadius.circular(5),
color: Colors.red[100],
elevation: 4,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
onTap: () {}, onTap: () {},
title: Text( title: Text(
_request.path, _request.path,
@ -41,6 +72,20 @@ class _DioErrorBuilderState extends State<DioErrorBuilder> {
], ],
), ),
), ),
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),
],
),
],
),
); );
} }
} }

@ -1,4 +1,3 @@
import 'package:animations/animations.dart';
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_response_view.dart'; import 'package:power_logger/src/view/dio_response_view.dart';
@ -16,13 +15,18 @@ class _DioResponseBuilderState extends State<DioResponseBuilder> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return OpenContainer(
tappable: false,
closedBuilder: (context, action) {
return Material( return Material(
color: Colors.green.withOpacity(0.1), clipBehavior: Clip.antiAlias,
elevation: 4,
borderRadius: BorderRadius.circular(5),
color: Colors.green[100],
child: ListTile( child: ListTile(
onTap: action, onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DioResponseView(data: widget.data),
),
),
title: Text( title: Text(
_request.path, _request.path,
style: TextStyle( style: TextStyle(
@ -40,8 +44,5 @@ class _DioResponseBuilderState extends State<DioResponseBuilder> {
), ),
), ),
); );
},
openBuilder: (context, action) => DioResponseView(data: widget.data),
);
} }
} }

@ -6,12 +6,18 @@ class UnfocusParser extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ListTile( return Material(
elevation: 4,
color: Colors.white,
clipBehavior: Clip.antiAlias,
borderRadius: BorderRadius.circular(5),
child: ListTile(
onTap: () {}, onTap: () {},
title: Text(data.toString()), title: Text(data.toString()),
trailing: Chip( trailing: Chip(
label: Text(data.runtimeType.toString()), label: Text(data.runtimeType.toString()),
), ),
),
); );
} }
} }

@ -15,7 +15,9 @@ class _PowerLoggerViewState extends State<PowerLoggerView> {
appBar: AppBar( appBar: AppBar(
title: Text('Logger View'), title: Text('Logger View'),
), ),
body: ListView.builder( body: ListView.separated(
padding: EdgeInsets.all(5),
separatorBuilder: (_, __) => SizedBox(height: 5),
itemBuilder: (context, index) { itemBuilder: (context, index) {
return LoggerDataParser.builder(LoggerData.data[index]); return LoggerDataParser.builder(LoggerData.data[index]);
}, },

@ -1,8 +1,11 @@
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/themes/atom-one-light.dart';
import 'package:power_logger/src/view/box_view.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/table_view.dart';
import 'package:power_logger/src/view/title_view.dart'; import 'package:power_logger/src/view/title_view.dart';
import 'package:pretty_json/pretty_json.dart';
class DioResponseView extends StatefulWidget { class DioResponseView extends StatefulWidget {
final Response data; final Response data;
@ -52,9 +55,30 @@ class _DioResponseViewState extends State<DioResponseView> {
} }
_buildData() { _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( return BoxView(
title: Text('Params'), title: Text('Raw Data'),
child: SelectableText(widget.data.data.toString()), child: SelectableText(prettyJson(widget.data.data)),
); );
} }
@ -92,6 +116,7 @@ class _DioResponseViewState extends State<DioResponseView> {
_buildMap(widget.data.headers.map), _buildMap(widget.data.headers.map),
_buildStatus(), _buildStatus(),
_buildData(), _buildData(),
_buildRawData(),
], ],
), ),
); );

@ -1,13 +1,6 @@
# Generated by pub # Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile # See https://dart.dev/tools/pub/glossary#lockfile
packages: packages:
animations:
dependency: "direct main"
description:
name: animations
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.2"
async: async:
dependency: transitive dependency: transitive
description: description:

@ -11,7 +11,6 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
dio: ^3.0.10 dio: ^3.0.10
animations: ^1.1.2
pretty_json: ^1.1.0 pretty_json: ^1.1.0
flutter_highlight: ^0.6.0 flutter_highlight: ^0.6.0

Loading…
Cancel
Save