Commit 4541c590 authored by Dio Maulana's avatar Dio Maulana

pisah api popular

parent 0b4af86c
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
import 'package:movie_app/config.dart';
class ApiPopular {
final String vote_average;
final String title;
final String poster_path;
final String id;
ApiPopular(this.vote_average, this.title, this.poster_path, this.id);
factory ApiPopular.createPopular(Map<String, dynamic> json) {
return ApiPopular(json['vote_average'].toString(), json['title'],
json['poster_path'], json['id'].toString());
}
static Future<List<ApiPopular>> getPopular() async {
String apiUrl = "https://api.themoviedb.org/3/movie/popular?api_key=" +
apiKey +
"&language=en-US&page=1";
var apiResult = await http.get(Uri.parse(apiUrl));
var jsonObject = json.decode(apiResult.body);
List<dynamic> listPopular = (jsonObject as Map<String, dynamic>)['results'];
List<ApiPopular> populars = [];
for (int i = 0; i < listPopular.length; i++)
populars.add(ApiPopular.createPopular(listPopular[i]));
return populars;
}
}
class ApiUpComing {
final String vote_average;
final String title;
final String poster_path;
final String id;
ApiUpComing(this.vote_average, this.title, this.poster_path, this.id);
factory ApiUpComing.createUpComing(Map<String, dynamic> json) {
return ApiUpComing(json['vote_average'].toString(), json['title'],
json['poster_path'], json['id'].toString());
}
static Future<List<ApiUpComing>> getUpcoming() async {
String apiUrl = "https://api.themoviedb.org/3/movie/upcoming?api_key=" +
apiKey +
"&language=en-US&page=1";
var apiResult = await http.get(Uri.parse(apiUrl));
var jsonObject = json.decode(apiResult.body);
List<dynamic> listUpComing =
(jsonObject as Map<String, dynamic>)['results'];
List<ApiUpComing> upcomings = [];
for (int i = 0; i < listUpComing.length; i++)
upcomings.add(ApiUpComing.createUpComing(listUpComing[i]));
print(upcomings);
return upcomings;
}
}
import 'package:flutter/material.dart';
import 'package:movie_app/pages/home.dart';
import 'package:movie_app/pages/main_page.dart';
import 'package:movie_app/pages/movie_detail.dart';
......@@ -17,6 +18,7 @@ class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
// onGenerateRoute: (settings) {
// if (settings.name == '/detail') {
// final args = settings.arguments.toString();
......@@ -31,7 +33,7 @@ class _MyAppState extends State<MyApp> {
// },
initialRoute: '/',
routes: {
'/': (context) => MainPages(),
'/': (context) => HomePage(),
'/detail': (context) => detailMovie(
// ModalRoute.of(context)!.settings.arguments as ScreenArguments),
ModalRoute.of(context)!.settings.arguments as String),
......
......@@ -11,7 +11,6 @@ class movieDetail {
factory movieDetail.fromJson(dynamic json) {
var nameGenre = [];
for (int i = 0; i < json['genres'].length; i++) {
print(json['genres'][i]['name']);
var genreName = '';
if (json['genres'][i]['name'] == null) {
genreName = 'Thriller';
......
import 'package:http/http.dart' as http;
class PostRateResult {
final int status_code;
final String status_message;
PostRateResult(this.status_code, this.status_message);
factory PostRateResult.fromJson(dynamic json) {
return PostRateResult(
json['status_code'] as int, json['status_message'] as String);
}
}
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const MyStatelessWidget(),
),
);
}
}
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: const Text('showModalBottomSheet'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.amber,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet'),
ElevatedButton(
child: const Text('Close BottomSheet'),
onPressed: () => Navigator.pop(context),
)
],
),
),
);
},
);
},
),
);
}
}
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Index 0: Home',
style: optionStyle,
),
Text(
'Index 1: Business',
style: optionStyle,
),
Text(
'Index 2: School',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Business',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'School',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
import 'package:flutter/material.dart';
class AccountPage extends StatefulWidget {
const AccountPage({Key? key}) : super(key: key);
@override
_AccountPageState createState() => _AccountPageState();
}
class _AccountPageState extends State<AccountPage> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Account Page'),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:movie_app/config.dart';
import 'package:movie_app/pages/account.dart';
import 'package:movie_app/pages/main_page.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _selectedIndex = 0;
final screens = [
MainPages(),
AccountPage(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: screens[_selectedIndex],
bottomNavigationBar: BottomNavigationBar(
backgroundColor: backgroundColor,
items: [
BottomNavigationBarItem(
icon: Icon(
Icons.home,
color: textColor,
),
label: 'Home'),
BottomNavigationBarItem(
icon: Icon(
Icons.person,
color: textColor,
),
label: 'Account')
],
currentIndex: _selectedIndex,
onTap: (index) => setState(() => _selectedIndex = index),
unselectedItemColor: textColor,
selectedItemColor: Colors.blue,
),
);
}
}
......@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:movie_app/apiMovie.dart';
import 'package:movie_app/pages/movie_detail.dart';
import '../config.dart';
......@@ -14,22 +15,32 @@ class MainPages extends StatefulWidget {
}
class _MainPagesState extends State<MainPages> {
// int _selectedIndex = 0;
var _populars = [];
var _upComing = [];
bool isLoading = true;
void getPopularData(String apiKey) async {
var response = await http.get(Uri.parse(
"https://api.themoviedb.org/3/movie/popular?api_key=" +
apiKey +
"&language=en-US&page=1"));
var jsonObject = jsonDecode(response.body);
List<dynamic> listPopular = (jsonObject as Map<String, dynamic>)['results'];
// void getPopularData(String apiKey) async {
// var response = await http.get(Uri.parse(
// "https://api.themoviedb.org/3/movie/popular?api_key=" +
// apiKey +
// "&language=en-US&page=1"));
// var jsonObject = jsonDecode(response.body);
// List<dynamic> listPopular = (jsonObject as Map<String, dynamic>)['results'];
// setState(() {
// _populars = listPopular;
// isLoading = false;
// });
// }
void getPopular() async {
var apiPopular = await ApiPopular.getPopular();
// var apiUpComing = await ApiUpComing.getUpcoming();
setState(() {
_populars = listPopular;
isLoading = false;
_populars = apiPopular;
// _upComing = apiUpComing;
});
}
......@@ -52,7 +63,7 @@ class _MainPagesState extends State<MainPages> {
void initState() {
// TODO: implement initState
super.initState();
getPopularData(apiKey);
getPopular();
getUpComingData(apiKey);
}
......@@ -148,10 +159,10 @@ class _MainPagesState extends State<MainPages> {
popularCard(
context,
"https://image.tmdb.org/t/p/w500" +
_populars[i]['poster_path'],
_populars[i]['title'],
_populars[i]['vote_average'].toString(),
_populars[i]['id'].toString()),
_populars[i].poster_path,
_populars[i].title,
_populars[i].vote_average.toString(),
_populars[i].id.toString()),
],
),
),
......@@ -207,7 +218,29 @@ class _MainPagesState extends State<MainPages> {
],
)
],
));
),
// bottomNavigationBar: BottomNavigationBar(
// backgroundColor: backgroundColor,
// items: [
// BottomNavigationBarItem(
// icon: Icon(
// Icons.home,
// color: textColor,
// ),
// label: 'Home'),
// BottomNavigationBarItem(
// icon: Icon(
// Icons.person,
// color: textColor,
// ),
// label: 'Account')
// ],
// currentIndex: _selectedIndex,
// onTap: (index) => setState(() => _selectedIndex = index),
// unselectedItemColor: textColor,
// selectedItemColor: Colors.blue,
// ),
);
}
Widget popularCard(BuildContext context, String image, String title,
......
......@@ -7,6 +7,7 @@ import 'package:movie_app/config.dart';
import 'package:http/http.dart' as http;
import '../models/movie_detail_api.dart';
import '../models/post_rate_model.dart';
class detailMovie extends StatefulWidget {
// ScreenArguments arg;
......@@ -21,6 +22,34 @@ class detailMovie extends StatefulWidget {
class _detailMovieState extends State<detailMovie> {
double rating = 0;
movieDetail? _movieDetail;
PostRateResult? _postRateResult;
void postRate(double rating, String apiKey, String movieId) async {
var response = await http.post(
Uri.parse("https://api.themoviedb.org/3/movie/" +
movieId +
"/rating?api_key=" +
apiKey),
body: {
"value": rating.toString(),
"guest_session_id": "aa9837cfeac9f0f53b69d75fd11a001e"
});
var json = jsonDecode(response.body);
setState(() {
_postRateResult = PostRateResult.fromJson(json);
});
final snackBar = SnackBar(
backgroundColor: backgroundColor,
content: Text(
_postRateResult == null ? '' : _postRateResult!.status_message,
style: TextStyle(
color: textColor, fontSize: 15, fontWeight: FontWeight.w800),
),
duration: Duration(seconds: 2),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
void getDetailMovie(String apiKey, String movieId) async {
var response = await http.get(Uri.parse(
"https://api.themoviedb.org/3/movie/" +
......@@ -44,7 +73,7 @@ class _detailMovieState extends State<detailMovie> {
@override
Widget build(BuildContext context) {
// print(apiKey);
// print(_postRateResult == null ? '' : _postRateResult!.status_message);
return Scaffold(
backgroundColor: backgroundColor,
body: Stack(
......@@ -254,7 +283,8 @@ class _detailMovieState extends State<detailMovie> {
BorderRadius.circular(25)),
color: Colors.yellow,
onPressed: () {
print("You rate this movie : $rating");
postRate(rating, apiKey, widget.id);
Navigator.pop(context);
})
],
),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment