Movie Search
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:multimedia/data/movies.dart';
|
||||
import 'package:multimedia/log_printer.dart';
|
||||
import 'package:multimedia/widgets/movies/add/movie_search_list.dart';
|
||||
|
||||
import 'package:tmdb_api/tmdb_api.dart' as TMDB;
|
||||
|
||||
// ignore: must_be_immutable
|
||||
class AddMovieScreen extends StatefulWidget {
|
||||
AddMovieScreen({
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
State<AddMovieScreen> createState() => _AddMovieScreenState();
|
||||
}
|
||||
|
||||
class _AddMovieScreenState extends State<AddMovieScreen> {
|
||||
Logger logger = getLogger();
|
||||
|
||||
String movieTitleSearch = '';
|
||||
|
||||
String yearSearch = '';
|
||||
|
||||
List<Movie> movieList = [];
|
||||
|
||||
late MovieSearchList movieSearchList;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
movieSearchList = MovieSearchList(movieList: movieList);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Search Movie'),
|
||||
actions: [
|
||||
Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 250,
|
||||
child: TextField(
|
||||
style: TextStyle(color: Colors.white),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Movie Title',
|
||||
),
|
||||
onChanged: (text) {
|
||||
movieTitleSearch = text;
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
width: 10,
|
||||
),
|
||||
SizedBox(
|
||||
width: 50,
|
||||
child: TextField(
|
||||
style: TextStyle(color: Colors.white),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Year',
|
||||
),
|
||||
onChanged: (text) {
|
||||
yearSearch = text;
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
width: 10,
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
startSearch();
|
||||
},
|
||||
child: const Text('Search'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const VerticalDivider(
|
||||
width: 10,
|
||||
thickness: 3,
|
||||
indent: 0,
|
||||
endIndent: 0,
|
||||
color: Colors.grey,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.check_circle_outline),
|
||||
tooltip: 'Save changes',
|
||||
onPressed: () {
|
||||
// if (save()) {
|
||||
// Navigator.pop(context, movieDetails);
|
||||
// }
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.highlight_off),
|
||||
tooltip: 'Revert changes',
|
||||
onPressed: () {
|
||||
// Navigator.pop(context, null);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: MovieSearchList(
|
||||
movieList: movieList,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String parseString(dynamic data, String field) {
|
||||
if (data[field].toString() == Null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return data[field].toString();
|
||||
}
|
||||
|
||||
DateTime parseDate(dynamic data, String field) {
|
||||
if (data[field].toString() == Null) {
|
||||
return DateTime(1900, 1, 1);
|
||||
}
|
||||
return DateTime.parse(data[field].toString());
|
||||
}
|
||||
|
||||
double parseDouble(dynamic data, String field) {
|
||||
if (data[field] == Null) {
|
||||
return 0.0;
|
||||
}
|
||||
return data[field];
|
||||
}
|
||||
|
||||
int parseInt(dynamic data, String field) {
|
||||
if (data[field] == Null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return data[field];
|
||||
}
|
||||
|
||||
Future<void> startSearch() async {
|
||||
if (movieTitleSearch.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
int year = int.tryParse(yearSearch) ?? 0;
|
||||
|
||||
logger.i('Wir suchen ...');
|
||||
final tmdb = TMDB.TMDB(TMDB.ApiKeys(
|
||||
'a33271b9e54cdcb9a80680eaf5522f1b', 'apiReadAccessTokenv4'));
|
||||
|
||||
final response = await tmdb.v3.search.queryMovies(
|
||||
movieTitleSearch,
|
||||
year: year,
|
||||
language: 'de',
|
||||
);
|
||||
// logger.i(response);
|
||||
// logger.i(response['page']);
|
||||
// logger.i(response['results']);
|
||||
List<dynamic> movies = response['results'];
|
||||
|
||||
setState(
|
||||
() {
|
||||
movieList.clear();
|
||||
|
||||
for (int i = 0; i < movies.length; i++) {
|
||||
dynamic movie = movies[i];
|
||||
|
||||
String movieTitle = parseString(movie, 'title');
|
||||
String originalTitle = parseString(movie, 'original_title');
|
||||
String overview = parseString(movie, 'overview');
|
||||
DateTime releaseDate = parseDate(movie, 'release_date');
|
||||
String backdropPath = parseString(movie, 'backdrop_path');
|
||||
double voteAverage = parseDouble(movie, 'vote_average');
|
||||
int voteCount = parseInt(movie, 'vote_count');
|
||||
|
||||
logger.i(backdropPath);
|
||||
movieList.add(Movie(
|
||||
movieID: -1,
|
||||
movieTitle: movieTitle,
|
||||
originalTitle: originalTitle,
|
||||
overview: overview,
|
||||
releaseDate: releaseDate,
|
||||
state: 0,
|
||||
resolution: '',
|
||||
backdropPath: backdropPath,
|
||||
languages: '',
|
||||
productionCountries: '',
|
||||
productionCompanies: '',
|
||||
voteAverage: voteAverage,
|
||||
voteCount: voteCount,
|
||||
cast: '',
|
||||
crew: '',
|
||||
genre: '',
|
||||
localPath: '',
|
||||
));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:multimedia/data/movies.dart';
|
||||
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:multimedia/log_printer.dart';
|
||||
|
||||
// ignore: must_be_immutable
|
||||
class MovieSearchList extends StatefulWidget {
|
||||
MovieSearchList({
|
||||
super.key,
|
||||
required List<Movie> this.movieList,
|
||||
});
|
||||
|
||||
List<Movie> movieList = [];
|
||||
|
||||
@override
|
||||
State<MovieSearchList> createState() => _MovieSearchListState();
|
||||
}
|
||||
|
||||
class _MovieSearchListState extends State<MovieSearchList> {
|
||||
Logger logger = getLogger();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: widget.movieList
|
||||
.map(
|
||||
(movie) => Column(children: [
|
||||
Row(
|
||||
children: [
|
||||
movie.backdropPath == 'null'
|
||||
? const SizedBox(width: 200)
|
||||
: Image.network(
|
||||
'https://image.tmdb.org/t/p/w200${movie.backdropPath}'),
|
||||
Expanded(
|
||||
child: CheckboxListTile(
|
||||
title: Text(
|
||||
'${movie.movieTitle} (${movie.releaseDate.year})'),
|
||||
subtitle: Text(movie.overview),
|
||||
value: movie.state == 1,
|
||||
onChanged: (bool? value) {
|
||||
setState(() {
|
||||
if (movie.state == 1) {
|
||||
movie.state = 0;
|
||||
} else {
|
||||
movie.state = 1;
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const Divider(height: 0),
|
||||
]),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'package:multimedia/widgets/movies/add/add_screen.dart';
|
||||
import 'package:multimedia/widgets/movies/details/details_screen.dart';
|
||||
import 'package:multimedia/widgets/movies/filter_box.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
@@ -284,14 +285,6 @@ class _MoviesScreenState extends State<MoviesScreen> {
|
||||
// }
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.search_outlined),
|
||||
tooltip: 'Search for movie',
|
||||
onPressed: () {
|
||||
searchMovie();
|
||||
// Navigator.pop(context, null);
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.update_outlined),
|
||||
tooltip: 'Update movie',
|
||||
@@ -349,7 +342,11 @@ class _MoviesScreenState extends State<MoviesScreen> {
|
||||
}
|
||||
|
||||
void addMovie() {
|
||||
logger.i('add movie');
|
||||
final m = Navigator.push(
|
||||
context, MaterialPageRoute(builder: (ctx) => AddMovieScreen()));
|
||||
|
||||
if (m != null) {
|
||||
} else {}
|
||||
}
|
||||
|
||||
void searchMovie() {
|
||||
|
||||
Reference in New Issue
Block a user