From 2f477dc908f5bd2ab4e2f45acd8ae54702923d06 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Wed, 13 Jan 2021 16:20:51 +0800 Subject: [PATCH] add logger --- lib/main.dart | 1 + lib/pages/sign/sign_in_page.dart | 208 +++++++++++++++++-------------- lib/pages/tab_navigator.dart | 4 + lib/utils/logger_card.dart | 62 +++++++++ lib/utils/logger_data.dart | 14 +++ lib/utils/logger_view.dart | 98 +++++++++++++++ lib/utils/net_util.dart | 28 ++++- pubspec.lock | 9 +- pubspec.yaml | 4 +- 9 files changed, 330 insertions(+), 98 deletions(-) create mode 100644 lib/utils/logger_card.dart create mode 100644 lib/utils/logger_data.dart create mode 100644 lib/utils/logger_view.dart diff --git a/lib/main.dart b/lib/main.dart index aea24f93..4593e519 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ import 'package:akuCommunity/pages/tab_navigator.dart'; import 'package:akuCommunity/provider/user_provider.dart'; import 'package:akuCommunity/utils/developer_util.dart'; +import 'package:akuCommunity/utils/logger_view.dart'; import 'package:amap_map_fluttify/amap_map_fluttify.dart'; import 'package:ani_route/ani_route.dart'; import 'package:bot_toast/bot_toast.dart'; diff --git a/lib/pages/sign/sign_in_page.dart b/lib/pages/sign/sign_in_page.dart index 48348af7..41a0c1ed 100644 --- a/lib/pages/sign/sign_in_page.dart +++ b/lib/pages/sign/sign_in_page.dart @@ -1,11 +1,15 @@ +import 'dart:async'; import 'dart:math'; import 'dart:ui'; import 'package:akuCommunity/pages/setting_page/agreement_page/agreement_page.dart'; import 'package:akuCommunity/pages/setting_page/agreement_page/privacy_page.dart'; import 'package:akuCommunity/pages/sign/user_authentication_page.dart'; +import 'package:akuCommunity/extensions/num_ext.dart'; +import 'package:akuCommunity/const/resource.dart'; import 'package:ani_route/ani_route.dart'; import 'package:bot_toast/bot_toast.dart'; +import 'package:common_utils/common_utils.dart'; import 'package:flustars/flustars.dart' show TextUtil; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; @@ -13,6 +17,7 @@ import 'package:akuCommunity/utils/screenutil.dart'; import 'package:akuCommunity/base/base_style.dart'; import 'package:akuCommunity/base/assets_image.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; +import 'package:velocity_x/velocity_x.dart'; class SignInPage extends StatefulWidget { SignInPage({Key key}) : super(key: key); @@ -24,96 +29,14 @@ class SignInPage extends StatefulWidget { class _SignInPageState extends State { TextEditingController _phone = new TextEditingController(); TextEditingController _code = new TextEditingController(); - String _verifyStr = '获取验证码'; - AppBar _appBar() { - return AppBar( - elevation: 0, - backgroundColor: Colors.white, - // leading: IconButton( - // icon: Icon(AntDesign.left, size: 40.sp), - // onPressed: () { - // Navigator.pop(context); - // }, - // ), - ); - } + AppBar get _appBar => AppBar(elevation: 0, backgroundColor: Colors.white); - Container _containerTextField(String imagePath, - TextEditingController controller, String hintText, bool isCode) { - return Container( - padding: EdgeInsets.symmetric(horizontal: 29.w), - margin: EdgeInsets.symmetric(horizontal: 82.w), - decoration: BoxDecoration( - color: Color(0xfffff4d7), - borderRadius: BorderRadius.all(Radius.circular(36)), - ), - child: Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Image.asset( - imagePath, - height: 50.w, - width: 50.w, - ), - SizedBox(width: 24.w), - Expanded( - child: TextFormField( - obscureText: false, - obscuringCharacter: '*', - cursorColor: Color(0xffffc40c), - style: TextStyle( - fontSize: BaseStyle.fontSize28, - fontWeight: FontWeight.w600, - ), - controller: controller, - onChanged: (String value) {}, - maxLines: 1, - decoration: InputDecoration( - isDense: true, - contentPadding: EdgeInsets.symmetric( - vertical: 25.w, - ), - hintText: hintText, - border: InputBorder.none, //去掉输入框的下滑线 - fillColor: Color(0xfffff4d7), - filled: true, - hintStyle: TextStyle( - color: BaseStyle.color999999, - fontSize: BaseStyle.fontSize28, - fontWeight: FontWeight.normal, - ), - ), - ), - ), - isCode - ? Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - SizedBox( - width: 2, - height: 29.w, - child: DecoratedBox( - decoration: BoxDecoration(color: Color(0xffd8d8d8)), - ), - ), - SizedBox(width: 16.w), - InkWell( - child: Text( - _verifyStr, - style: TextStyle( - color: BaseStyle.color999999, - fontSize: BaseStyle.fontSize28, - fontWeight: FontWeight.w500, - ), - ), - onTap: null, - ), - ], - ) - : SizedBox(), - ], - ), - ); + Timer _timer; + bool get validPhone => RegexUtil.isMobileSimple(_phone.text); + + bool get _canGetCode { + bool timerActive = _timer?.isActive ?? false; + return (!timerActive) && validPhone; } Container _containerImage() { @@ -192,11 +115,72 @@ class _SignInPageState extends State { ); } + startTick() { + _timer = Timer.periodic(Duration(seconds: 1), (timer) { + if (timer.tick >= 60) { + _timer.cancel(); + _timer = null; + } + setState(() {}); + }); + } + + _buildTextField({ + String hint, + Widget prefix, + Widget suffix, + TextEditingController controller, + }) { + return Container( + margin: EdgeInsets.symmetric(horizontal: 82.w), + decoration: BoxDecoration( + color: Color(0xFFFFF4D7), + borderRadius: BorderRadius.circular(42.w), + ), + child: Row( + children: [ + 36.wb, + 82.hb, + prefix ?? SizedBox(), + 20.wb, + TextField( + controller: controller, + onChanged: (_) => setState(() {}), + decoration: InputDecoration( + isDense: true, + hintText: hint, + contentPadding: EdgeInsets.zero, + border: InputBorder.none, + hintStyle: TextStyle( + color: Color(0xFF999999), + fontSize: 28.sp, + ), + ), + ).expand(), + suffix != null + ? Container( + height: 29.w, + width: 2.w, + color: Color(0xFFD8D8D8), + ) + : SizedBox(), + suffix ?? SizedBox(), + ], + ), + ); + } + + @override + void dispose() { + _timer?.cancel(); + super.dispose(); + } + @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, - appBar: _appBar(), + appBar: _appBar, body: SingleChildScrollView( child: Container( child: GestureDetector( @@ -225,11 +209,45 @@ class _SignInPageState extends State { ), ), SizedBox(height: 89.w), - _containerTextField( - AssetsImage.PHONELOGO, _phone, '请输入手机号码', false), - SizedBox(height: 27.w), - _containerTextField( - AssetsImage.CODELOGO, _code, '请输入验证码', true), + _buildTextField( + hint: '请输入手机号', + controller: _phone, + prefix: Image.asset( + R.ASSETS_IMAGES_PHONE_LOGO_PNG, + height: 50.w, + width: 50.w, + ), + ), + 26.hb, + _buildTextField( + prefix: Image.asset( + R.ASSETS_IMAGES_CODE_LOGO_PNG, + height: 50.w, + width: 50.w, + ), + hint: '请输入验证码', + controller: _code, + suffix: MaterialButton( + height: 82.w, + shape: StadiumBorder(), + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + child: Text( + _timer?.isActive ?? false + ? '${60 - _timer.tick}' + : '获取验证码', + style: TextStyle( + color: BaseStyle.color999999, + fontSize: BaseStyle.fontSize28, + fontWeight: FontWeight.w500, + ), + ), + onPressed: _canGetCode + ? () { + startTick(); + } + : null, + ), + ), SizedBox(height: 59.w), _inkWellLogin(), Row( diff --git a/lib/pages/tab_navigator.dart b/lib/pages/tab_navigator.dart index 26b51098..8d4030bb 100644 --- a/lib/pages/tab_navigator.dart +++ b/lib/pages/tab_navigator.dart @@ -1,3 +1,4 @@ +import 'package:akuCommunity/utils/logger_view.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -27,6 +28,9 @@ class _TabNavigatorState extends State { @override void initState() { super.initState(); + Future.delayed(Duration(milliseconds: 300), () { + if (mounted) LoggerFAB.openLogger(context); + }); _pages = [ HomePage(), // MarketPage(), diff --git a/lib/utils/logger_card.dart b/lib/utils/logger_card.dart new file mode 100644 index 00000000..366c0e57 --- /dev/null +++ b/lib/utils/logger_card.dart @@ -0,0 +1,62 @@ +import 'package:dio/dio.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:velocity_x/velocity_x.dart'; + +class LoggerCard extends StatelessWidget { + final dynamic data; + const LoggerCard({Key key, this.data}) : super(key: key); + Widget _buildDioErr() { + DioError error = data; + return VxBox( + child: Column( + children: [ + Row( + children: [ + Spacer(), + Chip( + backgroundColor: Colors.greenAccent, + label: Text(error.request.method), + ), + ], + ), + Text(error.message), + Text(error.request.path), + Text(error.request.method), + ], + ), + ) + .height(200) + .p4 + .red200 + .margin(EdgeInsets.all(5)) + .shadowSm + .make() + .material(color: Colors.transparent); + } + + Widget _buildResponse() { + Response response = data; + return Card( + child: Column( + children: [ + Text(response.statusCode.toString()), + ], + ), + ); + } + + Widget _getLoggerView() { + switch (data.runtimeType) { + case DioError: + return _buildDioErr(); + case Response: + return _buildResponse(); + default: + return Text("UNKNOW"); + } + } + + @override + Widget build(BuildContext context) => _getLoggerView(); +} diff --git a/lib/utils/logger_data.dart b/lib/utils/logger_data.dart new file mode 100644 index 00000000..a341c3ad --- /dev/null +++ b/lib/utils/logger_data.dart @@ -0,0 +1,14 @@ +class LoggerData { + static int maxCount = 100; + static List _data = []; + static List get data => _data; + + static addData(dynamic item) { + if (_data.length < maxCount) + _data.insert(0, item); + else { + _data.removeLast(); + _data.insert(item, 0); + } + } +} diff --git a/lib/utils/logger_view.dart b/lib/utils/logger_view.dart new file mode 100644 index 00000000..ed5702a3 --- /dev/null +++ b/lib/utils/logger_view.dart @@ -0,0 +1,98 @@ +import 'package:akuCommunity/utils/logger_card.dart'; +import 'package:akuCommunity/utils/logger_data.dart'; +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; + +class LoggerFAB extends StatefulWidget { + static openLogger(BuildContext context) { + Overlay.of(context).insert(OverlayEntry( + builder: (context) { + return LoggerFAB(); + }, + )); + } + + LoggerFAB({Key key}) : super(key: key); + + @override + _LoggerFABState createState() => _LoggerFABState(); +} + +class _LoggerFABState extends State { + double _x = 100; + double _y = 100; + + double get screenWidth => MediaQuery.of(context).size.width; + double get screenHeight => MediaQuery.of(context).size.height; + bool _moving = false; + @override + Widget build(BuildContext context) { + return Stack( + children: [ + AnimatedPositioned( + duration: _moving ? Duration.zero : Duration(milliseconds: 300), + left: _x - 25, + top: _y - 25, + child: GestureDetector( + onPanStart: (detail) { + _moving = true; + }, + onPanUpdate: (details) { + setState(() { + _x = details.globalPosition.dx; + _y = details.globalPosition.dy; + }); + }, + onPanEnd: (detail) { + if (_x < 100) _x = 50; + if (_y < 100) _y = 75; + if (_x > screenWidth - 50) _x = screenWidth - 50; + if (_y > screenHeight - 50) _y = screenHeight - 50; + _moving = false; + setState(() {}); + }, + onTap: () { + Get.to(LoggerView()); + }, + child: Container( + decoration: BoxDecoration( + color: Colors.yellowAccent, + borderRadius: BorderRadius.circular(25), + ), + height: 50, + width: 50, + child: Icon(Icons.code), + ), + ), + ) + ], + ); + } +} + +class LoggerView extends StatefulWidget { + LoggerView({Key key}) : super(key: key); + init() {} + + @override + _LoggerViewState createState() => _LoggerViewState(); +} + +class _LoggerViewState extends State { + @override + void initState() { + super.initState(); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(), + body: ListView.builder( + itemBuilder: (context, index) => + LoggerCard(data: LoggerData.data[index]), + itemCount: LoggerData.data.length, + ), + ); + } +} diff --git a/lib/utils/net_util.dart b/lib/utils/net_util.dart index ab3acfd9..0e546065 100644 --- a/lib/utils/net_util.dart +++ b/lib/utils/net_util.dart @@ -1,17 +1,25 @@ import 'dart:convert'; +import 'package:akuCommunity/utils/logger_data.dart'; import 'package:bot_toast/bot_toast.dart'; import 'package:dio/dio.dart'; import 'package:akuCommunity/constants/api.dart'; +import 'package:logger/logger.dart'; class NetUtil { Dio _dio; + Logger _logger; static final NetUtil _netUtil = NetUtil._internal(); factory NetUtil() => _netUtil; NetUtil._internal() { + _logger = Logger( + printer: PrettyPrinter( + methodCount: 2, + errorMethodCount: 4, + )); BaseOptions options = BaseOptions( baseUrl: API.host, connectTimeout: API.networkTimeOut, @@ -22,12 +30,25 @@ class NetUtil { if (_dio == null) _dio = Dio(options); } + ///call auth after login + auth(String token) { + _logger.d('setToken@$token'); + _dio.options.headers.putIfAbsent('App-Admin-Token', () => token); + } + Future get( String path, { Map params, }) async { try { Response res = await _dio.get(path, queryParameters: params); + _logger.v({ + 'path': res.request.path, + 'header': res.request.headers, + 'params': res.request.queryParameters, + 'data': res.data, + }); + LoggerData.addData(res); return BaseModel.fromJson(res.data); } on DioError catch (e) { _parseErr(e); @@ -36,8 +57,13 @@ class NetUtil { } _parseErr(DioError err) { + _logger.v({ + 'type': err.type.toString(), + 'message': err.message, + }); + LoggerData.addData(err); _makeToast(String message) { - BotToast.showText(text: '$message\_${err.response.statusCode}'); + BotToast.showText(text: '$message\_${err?.response?.statusCode ?? ''}'); } switch (err.type) { diff --git a/pubspec.lock b/pubspec.lock index 55fd2497..fc6dea75 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -170,7 +170,7 @@ packages: source: hosted version: "1.15.0-nullsafety.3" common_utils: - dependency: transitive + dependency: "direct main" description: name: common_utils url: "https://pub.flutter-io.cn" @@ -602,6 +602,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.1.6" + logger: + dependency: "direct main" + description: + name: logger + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.9.4" logging: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index f366a5f4..9ec8a958 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -32,7 +32,8 @@ dependencies: # UI库 flui: 0.9.2 # 工具类 - flustars: ^0.3.2 + flustars: + common_utils: # icons flutter_icons: 1.1.0 extended_image: 0.9.0 @@ -98,6 +99,7 @@ dependencies: velocity_x: ^1.3.1 hive: hive_flutter: + logger: ^0.9.4 dev_dependencies: flutter_test: