main.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import 'dart:async';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_logan/flutter_logan.dart';
  4. import 'package:flutter/material.dart';
  5. void main() => runApp(MyApp());
  6. class MyApp extends StatefulWidget {
  7. @override
  8. _MyAppState createState() => _MyAppState();
  9. }
  10. class _MyAppState extends State<MyApp> {
  11. String _showText = 'you should init log first';
  12. @override
  13. initState() {
  14. initLog();
  15. super.initState();
  16. }
  17. Future<void> initLog() async {
  18. String result = 'Failed to init log';
  19. try {
  20. final bool back = await FlutterLogan.init(
  21. '0123456789012345', '0123456789012345', 1024 * 1024 * 10);
  22. if (back) {
  23. result = 'Init log succeed';
  24. }
  25. } on PlatformException {
  26. result = 'Failed to init log';
  27. }
  28. if (!mounted) return;
  29. setState(() {
  30. _showText = result;
  31. });
  32. }
  33. Future<void> log() async {
  34. String result = 'Write log succeed';
  35. try {
  36. await FlutterLogan.log(10, 'this is log string');
  37. } on PlatformException {
  38. result = 'Failed to write log';
  39. }
  40. if (!mounted) return;
  41. setState(() {
  42. _showText = result;
  43. });
  44. }
  45. Future<void> getUploadPath() async {
  46. String result;
  47. try {
  48. final today = DateTime.now();
  49. final date = "${today.year.toString()}-${today.month.toString().padLeft(2, '0')}-${today.day.toString().padLeft(2, '0')}";
  50. final String path = await FlutterLogan.getUploadPath(date);
  51. if (path != null) {
  52. result = 'upload path = ' + path;
  53. } else {
  54. result = 'Failed to get upload path';
  55. }
  56. } on PlatformException {
  57. result = 'Failed to get upload path';
  58. }
  59. if (!mounted) return;
  60. setState(() {
  61. _showText = result;
  62. });
  63. }
  64. Future<void> flush() async {
  65. String result = 'Flush log succeed';
  66. try {
  67. await FlutterLogan.flush();
  68. } on PlatformException {
  69. result = 'Failed to flush log';
  70. }
  71. if (!mounted) return;
  72. setState(() {
  73. _showText = result;
  74. });
  75. }
  76. Future<void> cleanAllLog() async {
  77. String result = 'Clean log succeed';
  78. try {
  79. await FlutterLogan.cleanAllLogs();
  80. } on PlatformException {
  81. result = 'Failed to clean log';
  82. }
  83. if (!mounted) return;
  84. setState(() {
  85. _showText = result;
  86. });
  87. }
  88. Future<void> uploadToServer() async {
  89. String result = 'Failed upload to server';
  90. try {
  91. final today = DateTime.now();
  92. final date = "${today.year.toString()}-${today.month.toString().padLeft(2, '0')}-${today.day.toString().padLeft(2, '0')}";
  93. final bool back = await FlutterLogan.upload(
  94. 'http://127.0.0.1:3000/logupload',
  95. date,
  96. 'FlutterTestAppId',
  97. 'FlutterTestUnionId',
  98. 'FlutterTestDeviceId'
  99. );
  100. if (back) {
  101. result = 'Upload to server succeed';
  102. }
  103. } on PlatformException {
  104. result = 'Failed upload to server';
  105. }
  106. if (!mounted) return;
  107. setState(() {
  108. _showText = result;
  109. });
  110. }
  111. Widget buttonWidge(String title, Function event) {
  112. Color color = Theme.of(context).primaryColor;
  113. return new Column(
  114. mainAxisSize: MainAxisSize.min,
  115. mainAxisAlignment: MainAxisAlignment.center,
  116. children: [
  117. new FlatButton(
  118. onPressed: event,
  119. child: Text(title),
  120. color: color,
  121. ),
  122. ],
  123. );
  124. }
  125. @override
  126. Widget build(BuildContext context) {
  127. Widget buttonSection = new Column(
  128. children: [
  129. Row(
  130. mainAxisAlignment: MainAxisAlignment.spaceAround,
  131. children: [
  132. buttonWidge('flush', flush),
  133. buttonWidge('cleanAllLog', cleanAllLog),
  134. ],
  135. ),
  136. Row(
  137. mainAxisAlignment: MainAxisAlignment.spaceAround,
  138. children: [
  139. buttonWidge('log', log),
  140. buttonWidge('getUploadUrl', getUploadPath),
  141. buttonWidge('uploadToServer', uploadToServer),
  142. ],
  143. ),
  144. ],
  145. );
  146. return MaterialApp(
  147. home: Scaffold(
  148. appBar: AppBar(
  149. title: const Text('Plugin example app'),
  150. ),
  151. body: Column(
  152. children: [
  153. new Container(
  154. margin: const EdgeInsets.only(top: 30.0),
  155. child: buttonSection,
  156. ),
  157. new Container(
  158. margin: const EdgeInsets.only(top: 30.0,left: 10,right: 10),
  159. child: new Text(_showText),
  160. )
  161. ],
  162. )),
  163. );
  164. }
  165. }