123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import 'package:dio/dio.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'YSNetWorking.dart';
- typedef widgetCallback = Widget Function(dynamic value);
- String base = 'http://192.168.1.77:8082';
- int outTime = 20000;
- getFeatureDisplayShowData(HttpModel model) async{
- String url = base+model.api;
- SharedPreferences prefs = await SharedPreferences.getInstance();
- String token = prefs.getString('token');
- Map<String, dynamic> httpHeaders = {
- 'Accept': 'application/json,*/*',
- 'Content-Type': 'application/json',
- if(model.isToken==true)'Authentication': token??''
- };
- Response response;
- if(model.type==requestType.put){
- response = await Dio().put(url,data: model.parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else if(model.type==requestType.post){
- response = await Dio().post(url,data: model.parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else if(model.type==requestType.delete){
- response = await Dio().delete(url,data: model.parameter,options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }else{
- response = await Dio().get(url,queryParameters: Map<String, dynamic>.from(model.parameter),options: Options(receiveTimeout: outTime,headers: httpHeaders));
- }
- print(response.data);
- return response.data['data'];
- }
- futureWidget({widgetCallback data,HttpModel model}){
- return FutureBuilder(
- future: getFeatureDisplayShowData(model),
- builder: (context, snapshot) {
- var widget;
- if (snapshot.connectionState == ConnectionState.done) {
- if (snapshot.hasError) {
- widget = Icon(
- Icons.error,
- color: Colors.red,
- size: 48,
- );
- } else {
- widget = data(snapshot);
- }
- } else {
- widget = Padding(
- padding: EdgeInsets.all(20),
- child: CircularProgressIndicator(),
- );
- }
- return Center(
- child: Container(
- child: widget,
- ),
- );
- },
- );
- }
- class HttpModel{
- final requestType type;
- final String api;
- final parameter;
- final VoidCallback refresh;
- final bool isToken;
- HttpModel(this.type, this.api, this.parameter, this.refresh, this.isToken);
- }
|