У меня есть экран поиска с вкладкой, которая содержит 2 вкладки.При запуске каждая вкладка содержит пустой контейнер.Когда пользователь запускает поиск, я хочу создать сетку для каждой вкладки на основе результатов.
Это мой код, пока тело приложения
Widget tvScreen = new Container();
Widget movieScreen = new Container();
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(text: "TV Shows"),
Tab(text: "Movies"),
]),
centerTitle: true,
title: Padding(
padding: const EdgeInsets.only(right: 17.0),
child: new TextField(
autofocus: true,
style: new TextStyle(
fontSize: 18.0,
),
controller: _searchField,
decoration: InputDecoration(
hintText: "Enter TV Show or Movie...."
),
),
),
actions: <Widget>[
IconButton(
splashColor: Colors.white,
icon: Icon(Icons.search),
onPressed: _runSearch,
),
],
// MD2: make the color the same as the background.
backgroundColor: backgroundColor,
// Remove box-shadow
elevation: 18.00,
),
body: TabBarView(
children: [
tvScreen,
movieScreen,
]),
),
),
);
Вот так я получаюданные из API
Future<String> getResults() async{
List<ResultsModel> _searchResults = [];
if (_searchField.text.isEmpty != true){
String _searchParam = _searchField.text.replaceAll(r" ", "%20");
String _apiKeyMovie = "https://api.themoviedb.org/3/search/movie?"
"api_key=b52e4dea6f4284&language=en-US&"
"query=$_searchParam&include_adult=true";
var data = await http.get(_apiKeyMovie);
var jsonData = jsonDecode(data.body);
print("Show JSON");
print(jsonData);
if (jsonData != null) {
int totalResults = jsonData["results"].length;
if (totalResults != 0){
int counter = 0;
while (counter != totalResults) {
if (jsonData["results"][counter]["poster_path"] != null){
ResultsModel result = ResultsModel(
true, jsonData["results"][counter]["title"],
jsonData["results"][counter]["id"],
"http://image.tmdb.org/t/p/w185/${jsonData["results"][counter]["poster_path"]}");
_searchResults.add(result);
counter = counter + 1;
} else counter = counter + 1;
}
}
}
}
searchResults = _searchResults;
return "Done!";
Переключатель сделан нажатием кнопки
_runSearch(){
getResults();
setState(() {
//Change tabview contents
tvScreen = _uiBodyTV();
movieScreen = _uiBodyMovie();
});