YSFutureNetWork.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'package:dio/dio.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'YSNetWorking.dart';
  6. typedef widgetCallback = Widget Function(dynamic value);
  7. String base = 'http://192.168.1.77:8082';
  8. int outTime = 20000;
  9. getFeatureDisplayShowData(HttpModel model) async{
  10. String url = base+model.api;
  11. SharedPreferences prefs = await SharedPreferences.getInstance();
  12. String token = prefs.getString('token');
  13. Map<String, dynamic> httpHeaders = {
  14. 'Accept': 'application/json,*/*',
  15. 'Content-Type': 'application/json',
  16. if(model.isToken==true)'Authentication': token??''
  17. };
  18. Response response;
  19. if(model.type==requestType.put){
  20. response = await Dio().put(url,data: model.parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
  21. }else if(model.type==requestType.post){
  22. response = await Dio().post(url,data: model.parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
  23. }else if(model.type==requestType.delete){
  24. response = await Dio().delete(url,data: model.parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
  25. }else{
  26. response = await Dio().get(url,queryParameters: Map<String, dynamic>.from(model.parameter),options: Options(receiveTimeout: outTime,headers: httpHeaders));
  27. }
  28. print(response.data);
  29. return response.data['data'];
  30. }
  31. futureWidget({widgetCallback data,HttpModel model}){
  32. return FutureBuilder(
  33. future: getFeatureDisplayShowData(model),
  34. builder: (context, snapshot) {
  35. var widget;
  36. if (snapshot.connectionState == ConnectionState.done) {
  37. if (snapshot.hasError) {
  38. widget = Icon(
  39. Icons.error,
  40. color: Colors.red,
  41. size: 48,
  42. );
  43. } else {
  44. widget = data(snapshot);
  45. }
  46. } else {
  47. widget = Padding(
  48. padding: EdgeInsets.all(20),
  49. child: CircularProgressIndicator(),
  50. );
  51. }
  52. return Center(
  53. child: Container(
  54. child: widget,
  55. ),
  56. );
  57. },
  58. );
  59. }
  60. class HttpModel{
  61. final requestType type;
  62. final String api;
  63. final parameter;
  64. final VoidCallback refresh;
  65. final bool isToken;
  66. HttpModel(this.type, this.api, this.parameter, this.refresh, this.isToken);
  67. }