Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
N
netrain_flutter_app
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
邹志胜
netrain_flutter_app
Commits
1c94789d
Commit
1c94789d
authored
Jul 20, 2021
by
“Icebear”
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复response.data的格式问题
parent
bbbf4e98
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
76 additions
and
13 deletions
+76
-13
LoginApi.dart
netrain_flutter_app/lib/zhangfeng/LoginApi.dart
+10
-2
LoginPage.dart
netrain_flutter_app/lib/zhangfeng/LoginPage.dart
+38
-0
BaseHttpModel.dart
netrain_flutter_app/lib/zhangfeng/Network/BaseHttpModel.dart
+2
-4
BaseHttpModel.g.dart
...in_flutter_app/lib/zhangfeng/Network/BaseHttpModel.g.dart
+2
-2
HttpManager.dart
netrain_flutter_app/lib/zhangfeng/Network/HttpManager.dart
+23
-4
pubspec.yaml
netrain_flutter_app/pubspec.yaml
+1
-1
No files found.
netrain_flutter_app/lib/zhangfeng/LoginApi.dart
View file @
1c94789d
...
...
@@ -7,8 +7,7 @@ class LoginApi {
///示例请求
static
loginRequest
(
Map
<
String
,
dynamic
>
param
,
OnResult
onResult
)
{
return
HttpManager
.
getInstance
().
request
(
'auth/oauth/token'
,
parameters:
param
,
onResult:
(
data
,
errorMsg
){
BaseHttpModel
httpModel
=
data
;
UserModel
userModel
=
UserModel
.
fromJson
(
httpModel
.
data
);
UserModel
userModel
=
UserModel
.
fromJson
(
data
.
data
);
UserModel
.
saveModel
(
userModel
);
print
(
UserModel
.
getModel
().
token
);
if
(
onResult
!=
null
){
...
...
@@ -16,4 +15,13 @@ class LoginApi {
}
});
}
///上传头像
static
uploadAvatarRequest
(
String
avatarPath
,
OnResult
onResult
)
{
return
HttpManager
.
getInstance
().
request
(
'file/upload'
,
parameters:
null
,
method:
"POST"
,
imagePath:
avatarPath
,
onResult:
(
data
,
errorMsg
){
if
(
onResult
!=
null
){
onResult
(
data
,
errorMsg
);
}
});
}
}
netrain_flutter_app/lib/zhangfeng/LoginPage.dart
View file @
1c94789d
import
'package:flustars/flustars.dart'
;
import
'package:flutter/material.dart'
;
import
'package:flutter/widgets.dart'
;
import
'package:fluttertoast/fluttertoast.dart'
;
import
'package:netrain_flutter_app/zhangfeng/UserModel.dart'
;
import
'LoginApi.dart'
;
import
'package:netrain_flutter_app/common/Images.dart'
;
import
'package:image_picker/image_picker.dart'
;
//从相册里面选择图片或者拍照获取照片
import
'dart:io'
;
class
LoginRequestPage
extends
StatefulWidget
{
final
String
title
=
"登录页"
;
...
...
@@ -16,6 +19,7 @@ class _LoginState extends State<LoginRequestPage> {
// This widget is the root of your application.
final
phoneTextField
=
TextEditingController
();
final
pwdTextField
=
TextEditingController
();
PickedFile
_image
;
@override
void
dispose
()
{
...
...
@@ -51,6 +55,25 @@ class _LoginState extends State<LoginRequestPage> {
Fluttertoast
.
showToast
(
msg:
'退出成功'
);
}
Future
getImage
()
async
{
var
image
=
await
ImagePicker
.
platform
.
pickImage
(
source
:
ImageSource
.
gallery
);
_upLoadImage
(
image
);
//上传图片
setState
(()
{
_image
=
image
;
});
}
_upLoadImage
(
PickedFile
image
)
async
{
LoginApi
.
uploadAvatarRequest
(
image
.
path
,
(
httpModel
,
error
){
if
(
error
!=
null
){
Fluttertoast
.
showToast
(
msg:
error
);
}
else
{
Fluttertoast
.
showToast
(
msg:
'更改成功'
);
}
});
}
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
...
...
@@ -111,6 +134,21 @@ class _LoginState extends State<LoginRequestPage> {
onPressed:
_loginAction
,
),
),
Container
(
margin:
EdgeInsets
.
only
(
top:
50
),
width:
100
,
height:
100
,
child:
ElevatedButton
(
child:
Text
(
"上传头像"
),
onPressed:
getImage
,
),
),
Container
(
margin:
EdgeInsets
.
only
(
top:
50
),
width:
100
,
height:
100
,
child:
Image
.
asset
(
_image
!=
null
?
_image
.
path
:
Images
.
lake
),
),
],
),
),
// This trailing comma makes auto-formatting nicer for build methods.
...
...
netrain_flutter_app/lib/zhangfeng/Network/BaseHttpModel.dart
View file @
1c94789d
...
...
@@ -2,12 +2,10 @@ import 'package:json_annotation/json_annotation.dart';
part
'BaseHttpModel.g.dart'
;
@JsonSerializable
()
class
BaseHttpModel
{
final
data
;
final
code
;
@JsonKey
(
name:
'message'
)
final
msg
;
int
code
;
String
msg
;
BaseHttpModel
(
this
.
data
,
this
.
code
,
this
.
msg
);
...
...
netrain_flutter_app/lib/zhangfeng/Network/BaseHttpModel.g.dart
View file @
1c94789d
...
...
@@ -9,8 +9,8 @@ part of 'BaseHttpModel.dart';
BaseHttpModel
_$BaseHttpModelFromJson
(
Map
<
String
,
dynamic
>
json
)
{
return
BaseHttpModel
(
json
[
'data'
],
json
[
'code'
],
json
[
'msg'
],
json
[
'code'
]
as
int
,
json
[
'msg'
]
as
String
,
);
}
...
...
netrain_flutter_app/lib/zhangfeng/Network/HttpManager.dart
View file @
1c94789d
import
'dart:convert'
;
import
'package:dio/dio.dart'
;
import
'package:netrain_flutter_app/zhangfeng/Network/BaseHttpModel.dart'
;
import
'package:netrain_flutter_app/zhangfeng/UserModel.dart'
;
import
'BaseHttpModel.dart'
;
import
'UrlPath.dart'
;
typedef
OnResult
=
void
Function
(
BaseHttpModel
t
,
String
errorMsg
)
;
typedef
OnResult
=
void
Function
(
BaseHttpModel
httpModel
,
String
errorMsg
)
;
class
HttpManager
{
static
HttpManager
_instance
=
HttpManager
.
_internal
();
...
...
@@ -48,15 +50,32 @@ class HttpManager {
request
(
String
url
,
{
parameters
,
method
=
'GET'
,
OnResult
onResult
})
async
{
OnResult
onResult
,
String
imagePath
})
async
{
parameters
=
parameters
??
{};
if
(
onResult
==
null
){
return
;
}
try
{
FormData
formdata
;
if
(
imagePath
!=
null
){
var
name
=
imagePath
.
substring
(
imagePath
.
lastIndexOf
(
"/"
)
+
1
,
imagePath
.
length
);
var
suffix
=
name
.
substring
(
name
.
lastIndexOf
(
"."
)
+
1
,
name
.
length
);
formdata
=
FormData
.
fromMap
({
"file"
:
await
MultipartFile
.
fromFile
(
imagePath
,
//图片路径
filename:
name
,
)
});
}
parameters
=
new
Map
<
String
,
dynamic
>.
from
(
parameters
);
Map
<
String
,
String
>
map
=
new
Map
<
String
,
String
>();
if
(
UserModel
.
getModel
()
!=
null
)
{
map
[
UserModel
.
getModel
().
header
]
=
UserModel
.
getModel
().
tokenHead
+
UserModel
.
getModel
().
token
;
}
Response
response
=
await
_dio
.
request
(
url
,
queryParameters:
parameters
,
options:
new
Options
(
method:
method
));
BaseHttpModel
httpModel
=
BaseHttpModel
.
fromJson
(
response
.
data
);
queryParameters:
parameters
,
data:
formdata
,
options:
new
Options
(
method:
method
,
headers:
map
));
Map
<
String
,
dynamic
>
result
=
json
.
decode
(
response
.
toString
());
BaseHttpModel
httpModel
=
BaseHttpModel
.
fromJson
(
result
);
if
(
httpModel
.
isSuccess
())
{
onResult
(
httpModel
,
null
);
}
else
{
...
...
netrain_flutter_app/pubspec.yaml
View file @
1c94789d
...
...
@@ -32,7 +32,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons
:
^0.1.3
image_picker
:
^0.8.1+3
image_picker
:
0.7.4
fluttertoast
:
^3.1.3
http
:
0.13.3
mqtt_client
:
^9.3.2
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment