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:flutter/material.dart';
import 'package:movie_app/pages/home.dart';
import 'package:movie_app/pages/main_page.dart'; import 'package:movie_app/pages/main_page.dart';
import 'package:movie_app/pages/movie_detail.dart'; import 'package:movie_app/pages/movie_detail.dart';
...@@ -17,6 +18,7 @@ class _MyAppState extends State<MyApp> { ...@@ -17,6 +18,7 @@ class _MyAppState extends State<MyApp> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
debugShowCheckedModeBanner: false,
// onGenerateRoute: (settings) { // onGenerateRoute: (settings) {
// if (settings.name == '/detail') { // if (settings.name == '/detail') {
// final args = settings.arguments.toString(); // final args = settings.arguments.toString();
...@@ -31,7 +33,7 @@ class _MyAppState extends State<MyApp> { ...@@ -31,7 +33,7 @@ class _MyAppState extends State<MyApp> {
// }, // },
initialRoute: '/', initialRoute: '/',
routes: { routes: {
'/': (context) => MainPages(), '/': (context) => HomePage(),
'/detail': (context) => detailMovie( '/detail': (context) => detailMovie(
// ModalRoute.of(context)!.settings.arguments as ScreenArguments), // ModalRoute.of(context)!.settings.arguments as ScreenArguments),
ModalRoute.of(context)!.settings.arguments as String), ModalRoute.of(context)!.settings.arguments as String),
......
...@@ -11,7 +11,6 @@ class movieDetail { ...@@ -11,7 +11,6 @@ class movieDetail {
factory movieDetail.fromJson(dynamic json) { factory movieDetail.fromJson(dynamic json) {
var nameGenre = []; var nameGenre = [];
for (int i = 0; i < json['genres'].length; i++) { for (int i = 0; i < json['genres'].length; i++) {
print(json['genres'][i]['name']);
var genreName = ''; var genreName = '';
if (json['genres'][i]['name'] == null) { if (json['genres'][i]['name'] == null) {
genreName = 'Thriller'; 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,
),
);
}
}
This diff is collapsed.
...@@ -7,6 +7,7 @@ import 'package:movie_app/config.dart'; ...@@ -7,6 +7,7 @@ import 'package:movie_app/config.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import '../models/movie_detail_api.dart'; import '../models/movie_detail_api.dart';
import '../models/post_rate_model.dart';
class detailMovie extends StatefulWidget { class detailMovie extends StatefulWidget {
// ScreenArguments arg; // ScreenArguments arg;
...@@ -21,6 +22,34 @@ class detailMovie extends StatefulWidget { ...@@ -21,6 +22,34 @@ class detailMovie extends StatefulWidget {
class _detailMovieState extends State<detailMovie> { class _detailMovieState extends State<detailMovie> {
double rating = 0; double rating = 0;
movieDetail? _movieDetail; 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 { void getDetailMovie(String apiKey, String movieId) async {
var response = await http.get(Uri.parse( var response = await http.get(Uri.parse(
"https://api.themoviedb.org/3/movie/" + "https://api.themoviedb.org/3/movie/" +
...@@ -44,7 +73,7 @@ class _detailMovieState extends State<detailMovie> { ...@@ -44,7 +73,7 @@ class _detailMovieState extends State<detailMovie> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// print(apiKey); // print(_postRateResult == null ? '' : _postRateResult!.status_message);
return Scaffold( return Scaffold(
backgroundColor: backgroundColor, backgroundColor: backgroundColor,
body: Stack( body: Stack(
...@@ -254,7 +283,8 @@ class _detailMovieState extends State<detailMovie> { ...@@ -254,7 +283,8 @@ class _detailMovieState extends State<detailMovie> {
BorderRadius.circular(25)), BorderRadius.circular(25)),
color: Colors.yellow, color: Colors.yellow,
onPressed: () { 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