Commit 6ab539f6 authored by ouxiang's avatar ouxiang

add ALNetWork

parent 49ddc366
import 'package:flutter/material.dart';
import 'battery.dart';
import 'assetsChannel.dart';
import 'layoutTest.dart';
import 'animationTest.dart';
import 'testPage/battery.dart';
import 'testPage/assetsChannel.dart';
import 'testPage/layoutTest.dart';
import 'testPage/animationTest.dart';
import 'testPage/netWorkTest.dart';
void main() => runApp(MyApp());
......@@ -20,6 +21,7 @@ class MyApp extends StatelessWidget {
"/assetschannel": assetsChannelPage,
"/layouttest": layoutTestPage,
"/animationtest": animationTestPage,
"/networktest": netWorkTestPage,
},
);
}
......@@ -39,6 +41,10 @@ class MyApp extends StatelessWidget {
Widget animationTestPage(BuildContext context) {
return AnimationPage();
}
Widget netWorkTestPage(BuildContext context) {
return NetWorkTestPage();
}
}
class MyHomePage extends StatefulWidget {
......@@ -96,6 +102,11 @@ class _MyHomePageState extends State<MyHomePage> {
color: Colors.greenAccent,
onPressed: (){Navigator.pushNamed(context, "/animationtest");},
),
FlatButton(
child: Text('animation test'),
color: Colors.greenAccent,
onPressed: (){Navigator.pushNamed(context, "/networktest");},
),
],
),
),
......
import 'package:dio/dio.dart';
import 'dart:async';
import 'dart:convert';
class NetWorkSuccess {
NetWorkSuccess({this.data});
final Map data;
Map get dataMap{
if (this.data.runtimeType == Map) {
return data;
}else return null;
}
}
class NetWorkError {
NetWorkError({this.error});
final Map error;
int get errorCode{
if (this.error.runtimeType == Map) {
return error['errorCode'];
}else return null;
}
}
class ALURL {
ALURL({this.host}) : assert(host != null);
final String host;
static final baseUrl = 'http://earth.igengmei.com';
String get originUrl => (baseUrl + host);
}
class ALNetWorkHeader {
Map<String, dynamic> get header{
return {
'Host': 'earth.igengmei.com',
'Accept': '*/*',
'Cookie': '_gm_token=fb20fe1550833249; sessionid=qntnckxv4n4nzrl49jmaesc5ylru92yt; _gtid=ae355f92310911e9905700163e0a7a995288',
'User-Agent': 'GMAlpha/1.3.0 (iPhone; iOS 12.1.2; Scale/2.00)',
'Accept-Language': 'en-CN;q=1, zh-Hans-CN;q=0.9',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive'
};
}
Map<String, dynamic> get params {
return {
'platform': 'iPhone',
'os_version':'12.1.2',
'version':'1.3.0',
'model':'iPhone%206s',
'release':'0',
'idfa':'119A3567-6C81-40EA-A3ED-A63F7DCAD86B',
'idfv':'78BE2D94-7252-4C18-A816-2CEE6350B076',
'device_id':'119A3567-6C81-40EA-A3ED-A63F7DCAD86B',
'channel':'App%20Store',
'app_name':'gengmeiios',
'current_city_id':'worldwide',
'lat':'0',
'lng':'0',
'is_WiFi':'(null)',
'phone_id':'iPhone8'
};
}
}
// 管理任务
class ALNetWorkTask {
ALNetWorkTask({this.success, this.error, this.url, this.serviceInstance});
final NetWorkSuccessCallback success;
final ALURL url;
final NetWorkErrorCallback error;
final Dio serviceInstance;
void cancle() {
}
}
typedef NetWorkSuccessCallback = void Function(NetWorkSuccess success);
typedef NetWorkErrorCallback = void Function(NetWorkError error);
typedef ProgressCallback = void Function(int count, int total);
class ALNetWork {
//static 修饰的变量,为类变量,通过类对象访问
// static final shareInstance = new ALNetWork();
//任务映射表
static Map<String, ALNetWorkTask> taskMap = {};
const ALNetWork(
{this.success,
this.error,
this.progress,
this.host,
this.params}) : assert(host != null);
final NetWorkSuccessCallback success;
final NetWorkErrorCallback error;
final ProgressCallback progress;
final String host;
final Map params;
ALURL get url => ALURL(host: this.host);
Future<void> post() async{
Response response;
Dio dio = new Dio();
dio.options.connectTimeout = 5000; //5s
dio.options.receiveTimeout = 5000;
dio.options.headers = {'user-agent': 'dio', 'common-header': 'xx'};
response = await dio.post(this.url.originUrl, data: this.params);
_handleNetWorkService(response);
}
Future<void> excuteGet() async{
Response response;
Dio dio = new Dio();
dio.options.headers = ALNetWorkHeader().header;
response = await dio.get(this.url.originUrl, queryParameters: ALNetWorkHeader().params);
_handleNetWorkService(response);
}
void _handleNetWorkService(Response response) {
var data = jsonDecode(response.toString());
if(response.statusCode == 200){
this.success(NetWorkSuccess(data: data));
}else{
this.error(NetWorkError(error: data));
}
}
void upload(args) {
}
void download(args) {
}
}
\ No newline at end of file
import 'package:flutter/material.dart';
import 'package:my_flutter/netWork/ALNetWork.dart';
void main() => runApp(MaterialApp(
home: NetWorkTestPage(),
));
class NetWorkTestPage extends StatefulWidget {
@override
_NetWorkTestPageState createState() => _NetWorkTestPageState();
}
class _NetWorkTestPageState extends State<NetWorkTestPage> {
String _string = 'lalala';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('NewtWork Test Page'),
),
body: Container(
padding: EdgeInsets.all(32.0),
child: Center(
child: Column(
children: <Widget>[
FlatButton(
child: Text('newtWorkTest'),
color: Colors.purpleAccent,
onPressed: _netWork,
),
Container(
color: Colors.greenAccent,
constraints: BoxConstraints(
maxWidth: double.infinity,
minWidth: 200.0,
maxHeight: 500,
minHeight: 200.0),
child: Text('data:' + _string),
),
],
),
),
),
);
}
void _netWork() {
ALNetWork(
success: (NetWorkSuccess success){
print(success.data);
setState(() {
_string = success.data.toString();
});
},
error: (NetWorkError error){
print(error.error);
setState(() {
_string = error.error.toString();
});
},
host: '/api/v1/index'
)
..excuteGet();
}
}
\ No newline at end of file
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.1"
async:
dependency: transitive
description:
......@@ -43,13 +29,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
cookie_jar:
dependency: transitive
description:
......@@ -57,13 +36,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
cupertino_icons:
dependency: "direct main"
description:
......@@ -95,20 +67,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
hybrid_stack_manager:
dependency: "direct main"
description:
name: hybrid_stack_manager
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1"
image:
dependency: transitive
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.7"
json_annotation:
dependency: "direct main"
description:
......@@ -137,20 +95,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.2"
path_provider:
dependency: transitive
description:
name: path_provider
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
quiver:
dependency: transitive
description:
......@@ -226,13 +170,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "3.3.1"
sdks:
dart: ">=2.0.0 <3.0.0"
flutter: ">=0.3.1 <2.0.0"
flutter: ">=0.1.4 <2.0.0"
......@@ -24,7 +24,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
fluro: ^1.3.4
hybrid_stack_manager: 0.1.1
# hybrid_stack_manager: 0.1.1
json_annotation: ^2.0.0
dio: ^2.0.0
shared_preferences: ^0.4.2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment