添加debug

This commit is contained in:
LollipopKit
2021-09-13 14:58:08 +08:00
parent 4dd509a1d9
commit e804ade452
10 changed files with 287 additions and 28 deletions

View File

@@ -10,17 +10,9 @@ class MyApp extends StatelessWidget {
return MaterialApp(
title: 'ToolBox',
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,
),
darkTheme: ThemeData.dark(),
home: const MyHomePage(title: 'ToolBox'),
);
}

View File

@@ -1,6 +1,60 @@
import 'package:flutter/material.dart';
import 'package:toolbox/app.dart';
import 'dart:async';
void main() {
runApp(const MyApp());
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:provider/provider.dart';
import 'package:toolbox/app.dart';
import 'package:toolbox/core/analysis.dart';
import 'package:toolbox/data/provider/app.dart';
import 'package:toolbox/data/provider/debug.dart';
import 'package:toolbox/locator.dart';
Future<void> initApp() async {
await Hive.initFlutter();
await setupLocator();
}
void runInZone(dynamic Function() body) {
final zoneSpec = ZoneSpecification(
print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
parent.print(zone, line);
// This is a hack to avoid
// `setState() or markNeedsBuild() called during build`
// error.
Future.delayed(const Duration(milliseconds: 1), () {
final debugProvider = locator<DebugProvider>();
debugProvider.addText(line);
});
},
);
runZonedGuarded(
body,
onError,
zoneSpecification: zoneSpec,
);
}
void onError(Object obj, StackTrace stack) {
print('error: $obj');
Analysis.recordException(obj);
final debugProvider = locator<DebugProvider>();
debugProvider.addError(obj);
debugProvider.addError(stack);
}
Future<void> main() async {
runInZone(() async {
await initApp();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => locator<AppProvider>()),
ChangeNotifierProvider(create: (_) => locator<DebugProvider>()),
],
child: const MyApp(),
),
);
});
}

45
lib/page/debug.dart Normal file
View File

@@ -0,0 +1,45 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:toolbox/data/provider/debug.dart';
class DebugPage extends StatefulWidget {
const DebugPage({Key? key}) : super(key: key);
@override
_DebugPageState createState() => _DebugPageState();
}
class _DebugPageState extends State<DebugPage> {
DebugProvider get debug => Provider.of<DebugProvider>(context);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar:
AppBar(title: const Text('Terminal'), backgroundColor: Colors.black),
body: _buildTerminal(context),
backgroundColor: Colors.black,
);
}
Widget _buildTerminal(BuildContext context) {
return Container(
padding: const EdgeInsets.all(10),
color: Colors.black,
child: DefaultTextStyle(
style: const TextStyle(
fontFamily: 'monospace',
color: Colors.white,
fontWeight: FontWeight.bold,
),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: debug.widgets,
),
),
),
);
}
}

View File

@@ -1,6 +1,8 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:toolbox/core/route.dart';
import 'package:toolbox/page/debug.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
@@ -42,16 +44,21 @@ class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
title: GestureDetector(
onLongPress: () => AppRoute(const DebugPage(), 'Debug Page').go(context),
child: Text(widget.title),
),
),
body: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 7),
child: Column(
children: [
_buildInputTop(),
_buildTypeOption(),
_buildResult(),
],
)),
children: [
const SizedBox(height: 13),
_buildInputTop(),
_buildTypeOption(),
_buildResult(),
],
)),
floatingActionButton: FloatingActionButton(
onPressed: () {
_textEditingControllerResult.text = doConvert();