Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
F
flutter_clx_base
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
flutter_clx_base
Commits
65ad0aa6
提交
65ad0aa6
authored
6月 02, 2023
作者:
shixiaochen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1、调试动态设置网络请求代理
上级
8a85159d
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
175 行增加
和
1 行删除
+175
-1
main.dart
example/lib/main.dart
+3
-1
dio_widget_test.dart
example/lib/sample/dio_widget_test.dart
+1
-0
base_dio.dart
lib/api/base_dio.dart
+3
-0
proxy.dart
lib/api/proxy.dart
+168
-0
没有找到文件。
example/lib/main.dart
浏览文件 @
65ad0aa6
...
...
@@ -5,8 +5,10 @@ import 'package:flutter_clx_base_example/sample.dart';
import
'package:flutter_clx_base_example/sample/dio/dio_utils.dart'
;
import
'package:flutter_localizations/flutter_localizations.dart'
;
void
main
(
)
{
void
main
(
)
async
{
WidgetsFlutterBinding
.
ensureInitialized
();
DioUtils
.
instance
.
initConfig
();
await
SpUtil
.
getInstance
();
runApp
(
const
MyApp
());
}
...
...
example/lib/sample/dio_widget_test.dart
浏览文件 @
65ad0aa6
...
...
@@ -44,6 +44,7 @@ class _DioWidgetTestState extends State<DioWidgetTest> {
);
},
child:
const
Text
(
'dio请求'
)),
setProxyWidget
(
context
,
dio:
DioUtils
.
instance
.
dio
,
isProEnv:
false
)
],
);
}
...
...
lib/api/base_dio.dart
浏览文件 @
65ad0aa6
...
...
@@ -5,9 +5,12 @@ import 'package:flutter_clx_base/api/transformer.dart';
import
'package:flutter_clx_base/utils/log_utils.dart'
;
import
'package:flutter_clx_base/utils/toast_util.dart'
;
import
'package:pretty_dio_logger/pretty_dio_logger.dart'
;
export
'package:dio/dio.dart'
;
export
'package:dio/io.dart'
;
export
'proxy.dart'
;
abstract
class
BaseDio
{
final
dio
=
Dio
(
BaseOptions
(
...
...
lib/api/proxy.dart
0 → 100644
浏览文件 @
65ad0aa6
import
'dart:io'
;
import
'package:flutter/material.dart'
;
import
'package:flutter_clx_base/flutter_clx_base.dart'
;
import
'base_dio.dart'
;
const
spProxyIp
=
"proxyIP"
;
// ip
const
spProxyPort
=
"proxyPort"
;
// port
const
spSwitchProxy
=
"switchProxy"
;
// 开关代理
/// dio 设置代理uri
void
setProxyUri
(
Dio
?
dio
,
bool
isProEnv
)
{
String
?
proxyIP
=
SpUtil
.
getString
(
spProxyIp
);
String
?
proxyPort
=
SpUtil
.
getString
(
spProxyPort
);
(
dio
?.
httpClientAdapter
as
IOHttpClientAdapter
?)?.
onHttpClientCreate
=
(
HttpClient
client
)
{
// 是否设置代理:非生产环境,开启代理后,设置代理
bool
isSetProxy
=
!
isProEnv
&&
proxyIP
!=
null
&&
proxyIP
.
isNotEmpty
&&
proxyPort
!=
null
&&
proxyPort
.
isNotEmpty
&&
SpUtil
.
getBool
(
spSwitchProxy
)
==
true
;
client
.
findProxy
=
(
uri
)
=>
isSetProxy
?
"PROXY
$proxyIP
:
$proxyPort
"
:
"DIRECT"
;
client
.
badCertificateCallback
=
(
X509Certificate
cert
,
String
host
,
int
port
)
=>
true
;
return
client
;
};
}
/// 设置代理widget
/// isProEnv 是否是生产环境,生产环境禁止抓包
setProxyWidget
(
context
,
{
required
Dio
?
dio
,
required
bool
isProEnv
})
{
logger
.
d
(
"===== isProEnv =
$isProEnv
"
);
return
(
isProEnv
)
?
Container
()
:
InkWell
(
onTap:
()
=>
setProxyDialog
(
context
,
dio
,
isProEnv
),
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
,
dio
,
isProEnv
)
{
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
());
setProxyUri
(
dio
,
isProEnv
);
},
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:
TextInputType
.
number
,
decoration:
const
InputDecoration
(
counterText:
""
,
border:
InputBorder
.
none
,
//去掉下划线
),
),
),
),
],
),
);
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论