Skip to content

Commit

Permalink
Completed App v1
Browse files Browse the repository at this point in the history
  • Loading branch information
adarshbalu committed Jul 1, 2020
1 parent 86bb1e5 commit 70b3c67
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 42 deletions.
127 changes: 89 additions & 38 deletions lib/screens/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HomeScreen extends StatefulWidget {

class _HomeScreenState extends State<HomeScreen> {
final TextEditingController controller = TextEditingController();
int selectedPage = 1;

final _fullSearchKey = GlobalKey<FormState>();

Expand Down Expand Up @@ -321,6 +322,8 @@ class _HomeScreenState extends State<HomeScreen> {
}

Widget showResults(BuildContext context, List<Movie> moviesFound) {
int totalResults = int.parse(moviesFound[0].totalResults);
int pages = (totalResults / 10).ceil();
String type = 'all';
String year = '';
return Form(
Expand Down Expand Up @@ -464,44 +467,41 @@ class _HomeScreenState extends State<HomeScreen> {
],
),
),
],
),
),
SizedBox(
height: 9,
),
Container(
margin: EdgeInsets.only(top: 8),
child: RaisedButton.icon(
color: Color(0xff4A148C),
textColor: Colors.white,
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
onPressed: () {
if (_fullSearchKey.currentState.validate())
startSearch(
context: context,
title: controller.text,
page: 1,
type: type,
year: year);
else
Scaffold.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.grey.shade200,
content: Text(
'Enter required fields',
style: TextStyle(color: Colors.black),
),
Container(
child: RaisedButton.icon(
color: Color(0xff4A148C),
textColor: Colors.white,
padding:
EdgeInsets.symmetric(horizontal: 8, vertical: 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
onPressed: () {
if (_fullSearchKey.currentState.validate())
startSearch(
context: context,
title: controller.text,
page: 1,
type: type,
year: year);
else
Scaffold.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.grey.shade200,
content: Text(
'Enter required fields',
style: TextStyle(color: Colors.black),
),
),
);
},
icon: Icon(Icons.search),
label: Text(
'Search',
style: TextStyle(fontSize: 18),
),
);
},
icon: Icon(Icons.search),
label: Text(
'Search',
style: TextStyle(fontSize: 18),
),
),
),
],
),
),
Padding(
Expand All @@ -510,7 +510,10 @@ class _HomeScreenState extends State<HomeScreen> {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
icon: Icon(Icons.movie),
icon: Icon(
Icons.movie,
color: Color(0xff4A148C),
),
onPressed: () {},
),
SizedBox(
Expand Down Expand Up @@ -574,6 +577,54 @@ class _HomeScreenState extends State<HomeScreen> {
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
selectedPage > 1
? Padding(
padding: EdgeInsets.only(left: 8.0),
child: RaisedButton.icon(
onPressed: () {
selectedPage--;
startSearch(
context: context,
title: controller.text,
page: selectedPage,
type: type,
year: year);
},
icon: Icon(Icons.navigate_before),
color: Color(0xff4A148C),
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
label: Text('Previous Page')),
)
: SizedBox(),
selectedPage < pages
? Padding(
padding: EdgeInsets.only(right: 8.0),
child: RaisedButton.icon(
onPressed: () {
selectedPage++;
startSearch(
context: context,
title: controller.text,
page: selectedPage,
type: type,
year: year);
},
icon: Icon(Icons.navigate_next),
color: Color(0xff4A148C),
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
label: Text('Next Page')),
)
: SizedBox()
],
),
],
),
),
Expand Down
13 changes: 9 additions & 4 deletions lib/services/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class MovieRepository implements AbstractMovieRepository {
production: data['Production'] ?? 'N/A',
website: data['Website'] ?? 'N/A',
response: data['Response'],
posterURL: data['Poster'] ?? '');
posterURL: data['Poster'] == 'N/A'
? 'https://images.unsplash.com/photo-1485846234645-a62644f84728?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60'
: data['Poster']);
} else
throw NetworkError();
}
Expand All @@ -77,24 +79,27 @@ class MovieRepository implements AbstractMovieRepository {
if (type.isNotEmpty && type != '') {
if (type != 'all') finalURL = finalURL + '&type=$type';
}

// finalURL = finalURL + '&page=$page';
if (page != null) finalURL = finalURL + '&page=$page';

NetworkHelper networkHelper = NetworkHelper(finalURL);
var data = await networkHelper.getData();
bool resultsFound = data['Response'] == 'True';
int totalResults =
int.tryParse(data['totalResults']) ?? (resultsFound ? 1 : 0);
List<Movie> moviesFound = [];

if (resultsFound && totalResults >= 1) {
List movieList = data['Search'];
for (var m in movieList) {
moviesFound.add(Movie(
totalResults: data['totalResults'],
title: m['Title'],
year: m['Year'],
imdbID: m['imdbID'],
type: m['Type'],
posterURL: m['Poster']));
posterURL: m['Poster'] == 'N/A'
? 'https://images.unsplash.com/photo-1485846234645-a62644f84728?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60'
: m['Poster']));
}

return moviesFound;
Expand Down

0 comments on commit 70b3c67

Please sign in to comment.