Untitled

mail@pastecode.io avatar
unknown
dart
2 years ago
4.9 kB
3
Indexable
Never
import 'package:flutter/material.dart';
import 'package:watchlist_practice/data/api/api_service.dart';
import 'package:watchlist_practice/data/model/searched_movies.dart';
import 'package:watchlist_practice/ui/moviedetails/movie_details.dart';
import 'package:watchlist_practice/widgets/list_tiles.dart';

class SearchMovies extends StatefulWidget {
  const SearchMovies({Key? key}) : super(key: key);

  @override
  State<SearchMovies> createState() => _SearchMoviesState();
}

class _SearchMoviesState extends State<SearchMovies> {
  final editingController = TextEditingController();
  String query = '';
  ApiService getApi = ApiService();
  late Future<SearchedMovies> _apiData;

  @override
  void initState() {
    _apiData = getApi.getSearchedMoviesList(query);
    setState(() {
      editingController.text = '';
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Widget searchField() {
      return Container(
        margin: EdgeInsets.symmetric(
          horizontal: 12.0,
          vertical: 12.0,
        ),
        child: TextField(
          controller: editingController,
          onSubmitted: (value) {
            setState(() {
              query = editingController.text;
              _apiData = getApi.getSearchedMoviesList(query);
            });
          },
          decoration: InputDecoration(
              prefixIcon: Icon(Icons.search),
              hintText: "Search for Movies",
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8.0),
                borderSide: BorderSide(color: Colors.blue),
              )),
        ),
      );
    }

    Widget _listSearchMovies() {
      return Flexible(
          child: FutureBuilder<SearchedMovies>(
              future: _apiData,
              builder: (context, snapshot) {
                var data = snapshot.data;
                if (data == null) {
                  return Center(
                    child: Container(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.search,
                            size: 150.0,
                            color: Colors.grey,
                          ),
                          Text(
                            'Searched Movies\nwill Displayed Here!',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              fontSize: 20.0,
                              color: Colors.grey,
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                } else if (data.totalResults == 0) {
                  return Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          Icons.error,
                          color: Colors.grey,
                          size: 50.0,
                        ),
                        Text(
                          'No Movies Found!',
                          style: TextStyle(
                            fontSize: 22.0,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                  );
                } else if (!snapshot.hasData) {
                  Center(
                    child: CircularProgressIndicator(),
                  );
                }
                return ListView.builder(
                    itemCount: data.results.length,
                    itemBuilder: (context, index) {
                      return InkWell(
                        onTap: () {
                          Navigator.of(context)
                              .push(MaterialPageRoute(builder: (context) {
                            return MovieDetails(
                                movieId: data.results[index].id.toString());
                          }));
                        },
                        child: ListTiles(
                            imageUrl: data.results[index].posterPath,
                            movieTitle: data.results[index].title,
                            movieRating:
                                data.results[index].voteAverage.toString()),
                      );
                    });
              }));
    }

    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            searchField(),
            _listSearchMovies(),
          ],
        ),
      ),
    );
  }
}