Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
first_flutter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lee Jaehyeok
first_flutter
Commits
a99745a4
Commit
a99745a4
authored
5 months ago
by
Lee Jaehyeok
Browse files
Options
Downloads
Patches
Plain Diff
Update main.dart
parent
c3b3e24c
Branches
master
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/main.dart
+77
-14
77 additions, 14 deletions
lib/main.dart
with
77 additions
and
14 deletions
lib/main.dart
+
77
−
14
View file @
a99745a4
import
'dart:convert'
;
import
'package:english_words/english_words.dart'
;
import
'package:flutter/material.dart'
;
import
'package:flutter/rendering.dart'
;
import
'package:provider/provider.dart'
;
import
'package:http/http.dart'
as
http
;
void
main
()
{
runApp
(
MyApp
());
runApp
(
ChangeNotifierProvider
(
create:
(
context
)
=
>
MyAppState
(),
child:
const
MyApp
(),
),
);
}
class
MyApp
extends
StatelessWidget
{
...
...
@@ -12,15 +21,26 @@ class MyApp extends StatelessWidget {
@override
Widget
build
(
BuildContext
context
)
{
return
ChangeNotifierProvider
(
create:
(
context
)
=
>
MyAppState
(),
child:
MaterialApp
(
var
appState
=
context
.
watch
<
MyAppState
>();
return
MaterialApp
(
title:
'Namer App'
,
theme:
ThemeData
(
useMaterial3:
true
,
colorScheme:
ColorScheme
.
fromSeed
(
seedColor:
Colors
.
blu
e
),
colorScheme:
ColorScheme
.
fromSeed
(
seedColor:
Colors
.
deepOrang
e
),
),
home:
MyHomePage
(),
home:
FutureBuilder
<
List
<
WordPair
>>(
future:
appState
.
futureFavorites
,
builder:
(
context
,
snapshot
)
{
if
(
snapshot
.
connectionState
==
ConnectionState
.
waiting
)
{
return
const
Center
(
child:
CircularProgressIndicator
());
}
else
if
(
snapshot
.
hasError
)
{
return
Center
(
child:
Text
(
'Error:
${snapshot.error}
'
),);
}
else
if
(
snapshot
.
hasData
)
{
return
MyHomePage
();
}
else
{
return
const
Center
(
child:
Text
(
'No favorites yer.'
));
}
}
),
);
}
...
...
@@ -28,20 +48,63 @@ class MyApp extends StatelessWidget {
class
MyAppState
extends
ChangeNotifier
{
var
current
=
WordPair
.
random
();
List
<
WordPair
>
favorites
=
[];
late
Future
<
List
<
WordPair
>>
futureFavorites
;
MyAppState
()
{
futureFavorites
=
fetchFavorites
();
}
void
getNext
()
{
current
=
WordPair
.
random
();
notifyListeners
();
}
var
favorites
=
<
WordPair
>[];
Future
<
List
<
WordPair
>>
fetchFavorites
()
async
{
final
response
=
await
http
.
get
(
Uri
.
parse
(
'http://localhost:3000/likes'
)
);
List
<
WordPair
>
list
=
[];
var
jsonData
=
jsonDecode
(
response
.
body
);
for
(
Map
<
String
,
dynamic
>
obj
in
jsonData
)
{
List
<
String
>
res
=
obj
[
"id"
]
.
split
(
'_'
);
list
.
add
(
WordPair
(
res
[
0
],
res
[
1
]));
}
favorites
=
[..
.
list
];
notifyListeners
();
return
list
;
}
void
toggleFavorite
()
{
Future
<
void
>
toggleFavorite
()
async
{
var
uri
=
"http://localhost:3000/likes/
${current.first}
_
${current.second}
"
;
if
(
favorites
.
contains
(
current
))
{
final
http
.
Response
resp
=
await
http
.
delete
(
Uri
.
parse
(
uri
));
if
(
resp
.
statusCode
!=
200
)
{
return
;
}
favorites
.
remove
(
current
);
favorites
=
[..
.
favorites
];
}
else
{
favorites
.
add
(
current
);
print
(
"1"
);
final
http
.
Response
resp
=
await
http
.
post
(
Uri
.
parse
(
uri
),
headers:
<
String
,
String
>
{
'Content-Type'
:
'application/json; charset=UTF-8'
,
},
body:
jsonEncode
(<
String
,
dynamic
>{
'collectionId'
:
"
${current.first}
_
${current.second}
"
,
'like'
:
true
,
})
);
print
(
"2"
);
if
(
resp
.
statusCode
!=
200
)
{
return
;
}
favorites
=
[..
.
favorites
,
current
];
}
print
(
favorites
);
notifyListeners
();
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment