我正在尝试使用Navigator.push()
从一个屏幕导航到另一个屏幕。我有一个GridView
,并且有GridTile
个onTap我正在打开另一个屏幕。但是,即使有Scaffold
,第二个屏幕也始终是空白的。网络呼叫没有问题,因为来自网络的所有数据都已正确加载。我通过打印进行了验证。这是两个屏幕:
第一个屏幕:
import 'package:flutter/material.dart';
import '../models/item_model.dart';
import '../blocs/movies_bloc.dart';
import 'movie_detail.dart';
class MovieList extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return MovieListState();
}
}
class MovieListState extends State<MovieList> {
@override
void initState() {
super.initState();
print("initState called");
bloc.fetchAllMovies();
}
@override
void dispose() {
// TODO: implement dispose
bloc.dispose();
print("dispose called");
super.dispose();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Popular Movies'),
),
body: StreamBuilder(
stream: bloc.allMovies,
builder: (context, AsyncSnapshot<ItemModel> snapshot) {
if (snapshot.hasData) {
return buildList(snapshot);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
return Center(child: CircularProgressIndicator());
},
),
);
}
Widget buildList(AsyncSnapshot<ItemModel> snapshot) {
return GridView.builder(
itemCount: snapshot.data.results.length,
gridDelegate:
new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return GridTile(
child: InkResponse(
enableFeedback: true,
child: Image.network(
'https://image.tmdb.org/t/p/w185${snapshot.data
.results[index].poster_path}',
fit: BoxFit.cover,
),
onTap: () => openDetailPage(snapshot.data, index),
),
);
});
}
openDetailPage(ItemModel data, int index) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
MovieDetail(
title: data.results[index].title,
posterUrl: data.results[index].poster_path,
description: data.results[index].overview,
releaseDate: data.results[index].release_date,
);
}),
);
}
}
第二个屏幕:
import 'package:flutter/material.dart';
class MovieDetail extends StatelessWidget {
final posterUrl;
final description;
final releaseDate;
final String title;
MovieDetail({Key key, this.title, this.posterUrl, this.description, this.releaseDate}) : super(key: key);
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Movie Details'),
),
body: Text(
"Sample text",
style: TextStyle(color: Colors.white),
),
);
}
}
这是怎么回事?
答案 0 :(得分:1)
您的MaterialPageRoute
的生成器存在问题:它不返回任何内容。
添加return语句,应该没问题:
MaterialPageRoute(builder: (context) {
return MovieDetail(
title: data.results[index].title,
posterUrl: data.results[index].poster_path,
description: data.results[index].overview,
releaseDate: data.results[index].release_date,
);
}),