1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import 'dart:io';
import 'package:clx_flutter_net/clx_flutter_net.dart';
import 'package:clx_flutter_util/clx_flutter_util.dart';
import 'package:flutter/material.dart';
const spProxyIp = "proxyIP"; // ip
const spProxyPort = "proxyPort"; // port
const spSwitchProxy = "switchProxy"; // 开关代理
bool setProxyFlag = false; // 设置代理标识 true 设置 false 不设置
/// dio 设置代理uri
void setProxyUri(Dio? dio, bool isProdEnv) {
setProxyFlag = !isProdEnv; //生产环境不设置代理
if (!setProxyFlag) return;
String? proxyIP = SpUtil.getString(spProxyIp);
String? proxyPort = SpUtil.getString(spProxyPort);
(dio?.httpClientAdapter as IOHttpClientAdapter?)?.createHttpClient = () {
HttpClient client = HttpClient();
// 是否设置代理:非生产环境,开启代理后,设置代理
bool isSetProxy = setProxyFlag &&
proxyIP != null &&
proxyIP.isNotEmpty &&
proxyPort != null &&
proxyPort.isNotEmpty &&
SpUtil.getBool(spSwitchProxy) == true;
if (isSetProxy) {
client.findProxy = (uri) => "PROXY $proxyIP:$proxyPort";
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
}
return client;
};
}
/// 设置代理widget
setProxyWidget(context) {
return (!setProxyFlag)
? Container()
: InkWell(
onTap: () => setProxyDialog(context),
child: Container(
height: 50.0,
padding: const EdgeInsets.only(left: 15.0, right: 15.0),
color: Colors.white,
margin: const EdgeInsets.only(top: 1.0),
child: const Row(
children: <Widget>[
Expanded(
child: Text("设置代理", style: TextStyle(fontSize: 15.0)),
),
Icon(Icons.arrow_forward_ios, color: Colors.grey, size: 16.0)
],
),
),
);
}
var ipController = TextEditingController();
var portController = TextEditingController();
/// 设置代理dialog
setProxyDialog(context) {
ipController.text = SpUtil.getString(spProxyIp) ?? "";
portController.text = SpUtil.getString(spProxyPort) ?? "";
showDialog(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: const Text("设置代理", textAlign: TextAlign.center),
children: [
_textFieldItem(title: "IP地址:", controller: ipController),
_textFieldItem(title: "端\t\t\t\t口:", controller: portController),
Row(
children: [
const SizedBox(width: 20.0),
const Text("开启代理:"),
const Spacer(),
Builder(builder: (context) {
return Switch(
activeColor: Colors.blue,
activeTrackColor: Colors.grey,
inactiveThumbColor: Colors.blue,
inactiveTrackColor: Colors.grey,
value: SpUtil.getBool(spSwitchProxy) ?? false,
onChanged: (bool value) {
SpUtil.putBool(spSwitchProxy, value);
(context as Element).markNeedsBuild();
});
}),
const SizedBox(width: 20.0),
],
),
InkWell(
onTap: () {
if (!_checkInfo()) {
return;
}
SpUtil.putString(
spProxyIp, ipController.value.text.toString().trim());
SpUtil.putString(
spProxyPort, portController.value.text.toString().trim());
exit(0);
},
child: Container(
margin: const EdgeInsets.only(
left: 30.0, right: 30.0, top: 30.0, bottom: 15.0),
padding: const EdgeInsets.symmetric(vertical: 12.0),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(50.0),
),
alignment: Alignment.center,
child: const Text("确定", style: TextStyle(color: Colors.white)),
),
)
],
);
},
);
}
///校验信息
bool _checkInfo() {
String ip = ipController.value.text.toString().trim();
String port = portController.value.text.toString().trim();
bool setProxy = SpUtil.getBool(spSwitchProxy) ?? false;
if (!setProxy) return true;
if (ip.isEmpty) {
ToastUtil.showToast("请输入ip地址");
return false;
}
if (port.isEmpty) {
ToastUtil.showToast("请输入端口号");
return false;
}
return true;
}
/// ip、端口 输入框
Widget _textFieldItem({title, controller}) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
margin: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey.shade300),
),
child: Row(
children: <Widget>[
Text(title, style: const TextStyle(fontSize: 15.0)),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 5.0),
child: TextField(
maxLines: 1,
controller: controller,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: const InputDecoration(
counterText: "",
border: InputBorder.none, //去掉下划线
),
),
),
),
],
),
);
}