tc_network 0.0.3
tc_network: ^0.0.3 copied to clipboard
A new network Flutter project.
example/lib/main.dart
import 'dart:ffi';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:network/network.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _text = "Test network";
final _networkPlugin = Network();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
}
Future reachabilityStatusChangeBlock() async {
try {
_text =
await _networkPlugin.reachabilityStatusChangeBlock() ?? 'reachabilityStatusChangeBlock Error';
} on PlatformException {
_text = 'Failed to get platform Net Status.';
}
setState(() {
});
}
Future cancelAllRequest() async {
try {
_text =
await _networkPlugin.cancelAllRequest() ?? 'cancelAllRequest Error';
} on PlatformException {
_text = 'cancelAllRequest Error';
}
setState(() {
});
}
Future cancelRequestWithURL() async {
try {
_text = await _networkPlugin.cancelRequestWithURL(
{"url" : "testUrl"}
) ?? 'cancelRequestWithURL Error';
} on PlatformException {
}
setState(() {
});
}
Future openLog() async {
try {
_text = await _networkPlugin.openLog() ?? 'openLog Error';
} on PlatformException {
}
setState(() {
});
}
Future closeLog() async {
try {
_text = await _networkPlugin.closeLog() ?? 'closeLog Error';
} on PlatformException {
}
setState(() {
});
}
Future requestWithMethod() async {
try {
_text = await _networkPlugin.requestWithMethod(
{
"methodType" : "GET",
"url" : "https://reqres.in/api/users",
'params' : {
"p1" : "p1",
"p2" : "p2"
},
"header" : {
"h1" : "h1",
"h2" : "h2"
}
}
) ?? 'requestWithMethod Error';
} on PlatformException {
};
setState(() {
});
}
Future downloadWithURL() async {
try {
_text = await _networkPlugin.downloadWithURL(
{
"url" : "https://reqres.in/api/users",
'params' : {
"p1" : "p1",
"p2" : "p2"
},
"header" : {
"h1" : "h1",
"h2" : "h2"
}
}
) ?? "downloadWithURL Error";
} on PlatformException {
};
setState(() {
});
}
Future setRequestSerializer() async {
try {
_text = await _networkPlugin.setRequestSerializer("HTTP") ?? "setRequestSerializer Error";
} on PlatformException {
}
setState(() {
});
}
Future setResponseSerializer() async {
try {
_text = await _networkPlugin.setResponseSerializer("HTTP") ?? "setResponseSerializer Error";
} on PlatformException {
}
setState(() {
});
}
Future setTimeoutInterval() async {
try{
double time = 30;
_text = await _networkPlugin.setTimeoutInterval(time) ?? "setTimeoutInterval Error";
} on PlatformException {
}
setState(() {
});
}
Future setHTTPHeaderValue() async {
try{
_text = await _networkPlugin.setHTTPHeaderValue(
{
"param1" : "param1",
"param2" : "param2"
}
) ?? "setHTTPHeaderValue Error";
} on PlatformException {
}
setState(() {
});
}
// } else if ([@"reachabilityStatusChangeBlock" isEqualToString:call.method]) { /// 网络监听
// } else if ([@"cancelAllRequest" isEqualToString:call.method]) { /// 取消所有HTTP请求
// } else if ([@"cancelRequestWithURL" isEqualToString:call.method]) { /// 取消指定URL的HTTP请求
// } else if ([@"openLog" isEqualToString:call.method]) { /// 开启日志打印 (Debug级别)
// } else if ([@"closeLog" isEqualToString:call.method]) { /// 关闭日志打印,默认关闭
// } else if ([@"requestWithMethod" isEqualToString:call.method]) { /// 发起网络请求
// } else if ([@"downloadWithURL" isEqualToString:call.method]) { /// 下载文件
// } else if ([@"setRequestSerializer" isEqualToString:call.method]) { /// 设置网络请求参数的格式:默认为JSON格式
// } else if ([@"setResponseSerializer" isEqualToString:call.method]) { /// 设置服务器响应数据格式:默认为JSON格式
// } else if ([@"setTimeoutInterval" isEqualToString:call.method]) { /// 设置服务器响应时间 默认30s
// } else if ([@"setHTTPHeaderValue" isEqualToString:call.method]) { /// 设置请求头
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.yellow,
appBar: AppBar(
title: const Text('Plugin example app'),
),
// body: Center(
// child: Text('Running on: $_platformVersion\n'),
// ),
body: Container(
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
Container(
width: 300.0, // 固定宽度
height: 100.0, // 固定高度
color: Colors.red,
child: Center(
child: Text(
_text,
style: TextStyle(
fontSize: 20.0, // 设置字体大小
color: Colors.black,
),
),
),
),
SizedBox(height: 30,),
ElevatedButton(
child: Text("网络监听"),
onPressed: () {
reachabilityStatusChangeBlock();
},
),
ElevatedButton(
child: Text("取消所有HTTP请求"),
onPressed: () {
cancelAllRequest();
},
),
ElevatedButton(
child: Text("取消指定URL的HTTP请求"),
onPressed: () {
cancelRequestWithURL();
},
),
ElevatedButton(
child: Text("开启日志打印 (Debug级别)"),
onPressed: () {
openLog();
},
),
ElevatedButton(
child: Text("关闭日志打印,默认关闭"),
onPressed: () {
closeLog();
},
),
ElevatedButton(
child: Text("发起网络请求"),
onPressed: () {
requestWithMethod();
},
),
ElevatedButton(
child: Text("下载文件"),
onPressed: () {
downloadWithURL();
},
),
ElevatedButton(
child: Text("设置网络请求参数的格式:默认为JSON格式"),
onPressed: () {
setRequestSerializer();
},
),
ElevatedButton(
child: Text("设置服务器响应数据格式:默认为JSON格式"),
onPressed: () {
setResponseSerializer();
},
),
ElevatedButton(
child: Text("设置服务器响应时间 默认30s"),
onPressed: () {
setTimeoutInterval();
},
),
ElevatedButton(
child: Text("设置请求头"),
onPressed: () {
setHTTPHeaderValue();
},
),
],
),
)
),
);
}
}