utils.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:grpc/grpc.dart';
  4. export 'logger.dart';
  5. import 'package:common_pub/common_pub.dart';
  6. import 'styles/color_schemes.g.dart';
  7. import 'view/login/login_view.dart';
  8. Future<void> tryApi(Future<void> Function() call, {
  9. String? errTitle,
  10. Future<bool> Function(GrpcError err)? onError,
  11. Future<void> Function()? onSuccess,
  12. VoidCallback? onFinally,
  13. }) async{
  14. Future<bool> handleError(GrpcError err) async {
  15. if(onError!= null){
  16. return onError(err);
  17. }
  18. if(err.code == StatusCode.unauthenticated){
  19. if (await LoginView.to()){
  20. try{
  21. await call();
  22. }catch(e){
  23. Get.snackbar('出错了', '未知错误');
  24. }
  25. }
  26. return true;
  27. }
  28. return false;
  29. }
  30. await tryCatchGrpc(
  31. call,
  32. onError: handleError,
  33. onSuccess: onSuccess,
  34. onFinally: onFinally,
  35. );
  36. }
  37. class Preview extends StatelessWidget{
  38. const Preview({super.key, required this.child});
  39. final Widget child;
  40. @override
  41. Widget build(BuildContext context) {
  42. return child;
  43. }
  44. }
  45. void runPreview(Widget child){
  46. runApp(GetMaterialApp(
  47. theme: ThemeData(useMaterial3: true, colorScheme: lightColorScheme),
  48. home: Preview(child: child)));
  49. }