Skip to content

Instantly share code, notes, and snippets.

@figengungor
Created August 13, 2018 08:57
Show Gist options
  • Save figengungor/489836e38bb283df0d07f1e53aba22fd to your computer and use it in GitHub Desktop.
Save figengungor/489836e38bb283df0d07f1e53aba22fd to your computer and use it in GitHub Desktop.
GridView Scroll Position Question Related Snippets
class HomePage extends StatefulWidget {
final MovieBloc bloc;
HomePage({this.bloc});
@override
HomePageState createState() {
return new HomePageState();
}
}
class HomePageState extends State<HomePage> {
int _currentIndex = 0;
@override
void dispose() {
widget.bloc.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
bottomNavigationBar: BottomNavigationBar(
onTap: (index) {
setState(() {
_currentIndex = index;
});
if (index == 0) {
widget.bloc.moviesType.add(MovieType.Popular);
} else if (index == 1) {
widget.bloc.moviesType.add(MovieType.TopRated);
} else {
widget.bloc.moviesType.add(MovieType.NowPlaying);
}
},
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.favorite), title: Text('Popular')),
BottomNavigationBarItem(
icon: Icon(Icons.star), title: Text('Top Rated')),
BottomNavigationBarItem(
icon: Icon(Icons.play_arrow), title: Text('Now Playing')),
],
),
body: StreamBuilder<UnmodifiableListView<Movie>>(
initialData: UnmodifiableListView<Movie>([]),
stream: widget.bloc.movies,
builder: (BuildContext context,
AsyncSnapshot<UnmodifiableListView<Movie>> snapshot) {
if (snapshot.hasData) {
return MovieList(snapshot.data);
} else if (snapshot.hasError) {
return Center(child: Text(snapshot.error.toString()));
} else {
return Center(child: CircularProgressIndicator());
}
}),
);
}
}
import 'package:flutter/material.dart';
import 'package:popular_movies/model/movie.dart';
import 'package:popular_movies/pages/movie_item.dart';
class MovieList extends StatefulWidget {
final List<Movie> movies;
MovieList(this.movies);
@override
_MovieListState createState() {
return new _MovieListState();
}
}
class _MovieListState extends State<MovieList> {
ScrollController _scrollController;
@override
void initState() {
_scrollController = ScrollController();
super.initState();
}
@override
void didUpdateWidget(MovieList oldWidget) {
print("didUpdateWidget MovieList");
super.didUpdateWidget(oldWidget);
}
@override
Widget build(BuildContext context) {
return GridView.builder(
controller: _scrollController,
itemCount: widget.movies.length,
itemBuilder: (BuildContext context, int index) {
return MovieItem(widget.movies[index]);
},
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 2 / 3,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment