Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaed8a8e

Browse files
committed
Tambahkan fitur ubah dan hapus data
1 parent979f09d commitaed8a8e

File tree

4 files changed

+129
-27
lines changed

4 files changed

+129
-27
lines changed

‎lib/src/api/api_service.dart‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,29 @@ class ApiService {
2626
returnfalse;
2727
}
2828
}
29+
30+
Future<bool>updateProfile(Profile data)async {
31+
final response=await client.put(
32+
"$baseUrl/profile/${data.id}",
33+
headers: {"content-type":"application/json"},
34+
body:profileToJson(data),
35+
);
36+
if (response.statusCode==200) {
37+
returntrue;
38+
}else {
39+
returnfalse;
40+
}
41+
}
42+
43+
Future<bool>deleteProfile(int id)async {
44+
final response=await client.delete(
45+
"$baseUrl/profile/$id",
46+
headers: {"content-type":"application/json"},
47+
);
48+
if (response.statusCode==200) {
49+
returntrue;
50+
}else {
51+
returnfalse;
52+
}
53+
}
2954
}

‎lib/src/ui/formadd/form_add_screen.dart‎

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import 'package:flutter_crud_api_sample_app/src/model/profile.dart';
55
finalGlobalKey<ScaffoldState> _scaffoldState=GlobalKey<ScaffoldState>();
66

77
classFormAddScreenextendsStatefulWidget {
8+
Profile profile;
9+
10+
FormAddScreen({this.profile});
11+
812
@override
913
_FormAddScreenStatecreateState()=>_FormAddScreenState();
1014
}
@@ -19,14 +23,27 @@ class _FormAddScreenState extends State<FormAddScreen> {
1923
TextEditingController _controllerEmail=TextEditingController();
2024
TextEditingController _controllerAge=TextEditingController();
2125

26+
@override
27+
voidinitState() {
28+
if (widget.profile!=null) {
29+
_isFieldNameValid=true;
30+
_controllerName.text= widget.profile.name;
31+
_isFieldEmailValid=true;
32+
_controllerEmail.text= widget.profile.email;
33+
_isFieldAgeValid=true;
34+
_controllerAge.text= widget.profile.age.toString();
35+
}
36+
super.initState();
37+
}
38+
2239
@override
2340
Widgetbuild(BuildContext context) {
2441
returnScaffold(
2542
key: _scaffoldState,
2643
appBar:AppBar(
2744
iconTheme:IconThemeData(color:Colors.white),
2845
title:Text(
29-
"Form Add",
46+
widget.profile==null?"Form Add":"Change Data",
3047
style:TextStyle(color:Colors.white),
3148
),
3249
),
@@ -43,6 +60,14 @@ class _FormAddScreenState extends State<FormAddScreen> {
4360
Padding(
4461
padding:constEdgeInsets.only(top:8.0),
4562
child:RaisedButton(
63+
child:Text(
64+
widget.profile==null
65+
?"Submit".toUpperCase()
66+
:"Update Data".toUpperCase(),
67+
style:TextStyle(
68+
color:Colors.white,
69+
),
70+
),
4671
onPressed: () {
4772
if (_isFieldNameValid==null||
4873
_isFieldEmailValid==null||
@@ -63,23 +88,31 @@ class _FormAddScreenState extends State<FormAddScreen> {
6388
int age=int.parse(_controllerAge.text.toString());
6489
Profile profile=
6590
Profile(name: name, email: email, age: age);
66-
_apiService.createProfile(profile).then((isSuccess) {
67-
setState(()=> _isLoading=false);
68-
if (isSuccess) {
69-
Navigator.pop(_scaffoldState.currentState.context);
70-
}else {
71-
_scaffoldState.currentState.showSnackBar(SnackBar(
72-
content:Text("Submit data failed"),
73-
));
74-
}
75-
});
91+
if (widget.profile==null) {
92+
_apiService.createProfile(profile).then((isSuccess) {
93+
setState(()=> _isLoading=false);
94+
if (isSuccess) {
95+
Navigator.pop(_scaffoldState.currentState.context);
96+
}else {
97+
_scaffoldState.currentState.showSnackBar(SnackBar(
98+
content:Text("Submit data failed"),
99+
));
100+
}
101+
});
102+
}else {
103+
profile.id= widget.profile.id;
104+
_apiService.updateProfile(profile).then((isSuccess) {
105+
setState(()=> _isLoading=false);
106+
if (isSuccess) {
107+
Navigator.pop(_scaffoldState.currentState.context);
108+
}else {
109+
_scaffoldState.currentState.showSnackBar(SnackBar(
110+
content:Text("Update data failed"),
111+
));
112+
}
113+
});
114+
}
76115
},
77-
child:Text(
78-
"Submit".toUpperCase(),
79-
style:TextStyle(
80-
color:Colors.white,
81-
),
82-
),
83116
color:Colors.orange[600],
84117
),
85118
)

‎lib/src/ui/home/home_screen.dart‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import'package:flutter/material.dart';
22
import'package:flutter_crud_api_sample_app/src/api/api_service.dart';
33
import'package:flutter_crud_api_sample_app/src/model/profile.dart';
4+
import'package:flutter_crud_api_sample_app/src/ui/formadd/form_add_screen.dart';
45

56
classHomeScreenextendsStatefulWidget {
67
@override
78
_HomeScreenStatecreateState()=>_HomeScreenState();
89
}
910

1011
class_HomeScreenStateextendsState<HomeScreen> {
12+
BuildContext context;
1113
ApiService apiService;
1214

1315
@override
@@ -18,6 +20,7 @@ class _HomeScreenState extends State<HomeScreen> {
1820

1921
@override
2022
Widgetbuild(BuildContext context) {
23+
this.context= context;
2124
returnSafeArea(
2225
child:FutureBuilder(
2326
future: apiService.getProfiles(),
@@ -65,7 +68,45 @@ class _HomeScreenState extends State<HomeScreen> {
6568
children:<Widget>[
6669
FlatButton(
6770
onPressed: () {
68-
// TODO: do something in here
71+
showDialog(
72+
context: context,
73+
builder: (context) {
74+
returnAlertDialog(
75+
title:Text("Warning"),
76+
content:Text(
77+
"Are you sure want to delete data profile ${profile.name}?"),
78+
actions:<Widget>[
79+
FlatButton(
80+
child:Text("Yes"),
81+
onPressed: () {
82+
Navigator.pop(context);
83+
apiService
84+
.deleteProfile(profile.id)
85+
.then((isSuccess) {
86+
if (isSuccess) {
87+
setState(() {});
88+
Scaffold.of(this.context)
89+
.showSnackBar(SnackBar(
90+
content:Text(
91+
"Delete data success")));
92+
}else {
93+
Scaffold.of(this.context)
94+
.showSnackBar(SnackBar(
95+
content:Text(
96+
"Delete data failed")));
97+
}
98+
});
99+
},
100+
),
101+
FlatButton(
102+
child:Text("No"),
103+
onPressed: () {
104+
Navigator.pop(context);
105+
},
106+
)
107+
],
108+
);
109+
});
69110
},
70111
child:Text(
71112
"Delete",
@@ -74,7 +115,10 @@ class _HomeScreenState extends State<HomeScreen> {
74115
),
75116
FlatButton(
76117
onPressed: () {
77-
// TODO: do something in here
118+
Navigator.push(context,
119+
MaterialPageRoute(builder: (context) {
120+
returnFormAddScreen(profile: profile);
121+
}));
78122
},
79123
child:Text(
80124
"Edit",

‎pubspec.lock‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.0.8"
10+
version: "2.1.0"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -66,7 +66,7 @@ packages:
6666
name: matcher
6767
url: "https://pub.dartlang.org"
6868
source: hosted
69-
version: "0.12.3+1"
69+
version: "0.12.5"
7070
meta:
7171
dependency: transitive
7272
description:
@@ -87,14 +87,14 @@ packages:
8787
name: pedantic
8888
url: "https://pub.dartlang.org"
8989
source: hosted
90-
version: "1.4.0"
90+
version: "1.5.0"
9191
quiver:
9292
dependency: transitive
9393
description:
9494
name: quiver
9595
url: "https://pub.dartlang.org"
9696
source: hosted
97-
version: "2.0.1"
97+
version: "2.0.2"
9898
sky_engine:
9999
dependency: transitive
100100
description: flutter
@@ -106,7 +106,7 @@ packages:
106106
name: source_span
107107
url: "https://pub.dartlang.org"
108108
source: hosted
109-
version: "1.5.4"
109+
version: "1.5.5"
110110
stack_trace:
111111
dependency: transitive
112112
description:
@@ -120,7 +120,7 @@ packages:
120120
name: stream_channel
121121
url: "https://pub.dartlang.org"
122122
source: hosted
123-
version: "1.6.8"
123+
version: "2.0.0"
124124
string_scanner:
125125
dependency: transitive
126126
description:
@@ -141,7 +141,7 @@ packages:
141141
name: test_api
142142
url: "https://pub.dartlang.org"
143143
source: hosted
144-
version: "0.2.2"
144+
version: "0.2.4"
145145
typed_data:
146146
dependency: transitive
147147
description:
@@ -157,4 +157,4 @@ packages:
157157
source: hosted
158158
version: "2.0.8"
159159
sdks:
160-
dart: ">=2.1.0 <3.0.0"
160+
dart: ">=2.2.0 <3.0.0"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp