Skip to content

Commit 98b2a0d

Browse files
committed
query for top 10 movies and latest movies
1 parent 413afdb commit 98b2a0d

File tree

1 file changed

+81
-63
lines changed

1 file changed

+81
-63
lines changed

dataconnect/lib/main.dart

Lines changed: 81 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MyApp extends StatelessWidget {
2121
@override
2222
Widget build(BuildContext context) {
2323
return MaterialApp(
24-
title: 'Flutter Demo',
24+
title: 'Firebase Data Connect',
2525
theme: ThemeData(
2626
// This is the theme of your application.
2727
//
@@ -41,7 +41,7 @@ class MyApp extends StatelessWidget {
4141
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
4242
useMaterial3: true,
4343
),
44-
home: const MyHomePage(title: 'Flutter Demo Home Page'),
44+
home: const MyHomePage(title: 'Data Connect Flutter'),
4545
);
4646
}
4747
}
@@ -65,7 +65,9 @@ class MyHomePage extends StatefulWidget {
6565
}
6666

6767
class _MyHomePageState extends State<MyHomePage> {
68-
List<ListMoviesMovies> _movies = [];
68+
List<ListMoviesMovies> _topMovies = [];
69+
List<ListMoviesMovies> _latestMovies = [];
70+
6971
@override
7072
void initState() {
7173
super.initState();
@@ -74,13 +76,26 @@ class _MyHomePageState extends State<MyHomePage> {
7476
/// comes back from the server.
7577
// MoviesConnector.instance.dataConnect
7678
// .useDataConnectEmulator('localhost', 9399);
77-
MoviesConnector.instance.listMovies().execute().then((res) {
79+
MoviesConnector.instance
80+
.listMovies()
81+
.orderByRating(OrderDirection.DESC)
82+
.limit(10)
83+
.execute()
84+
.then((res) {
85+
setState(() {
86+
_topMovies = res.data.movies;
87+
});
88+
});
89+
90+
MoviesConnector.instance
91+
.listMovies()
92+
.orderByReleaseYear(OrderDirection.DESC)
93+
.execute()
94+
.then((res) {
7895
setState(() {
79-
_movies = res.data.movies;
96+
_latestMovies = res.data.movies;
8097
});
8198
});
82-
MoviesConnector.instance.listMovies().ref().execute().then(() {} as FutureOr
83-
Function(QueryResult<ListMoviesData, ListMoviesVariables> value));
8499
}
85100

86101
void _refreshData() {
@@ -91,71 +106,74 @@ class _MyHomePageState extends State<MyHomePage> {
91106

92107
@override
93108
Widget build(BuildContext context) {
94-
// This method is rerun every time setState is called, for instance as done
95-
// by the _incrementCounter method above.
96-
//
97-
// The Flutter framework has been optimized to make rerunning build methods
98-
// fast, so that you can just rebuild anything that needs updating rather
99-
// than having to individually change instances of widgets.
100109
return Scaffold(
101110
appBar: AppBar(
102-
// TRY THIS: Try changing the color here to a specific color (to
103-
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
104-
// change color while the other colors stay the same.
105-
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
106-
// Here we take the value from the MyHomePage object that was created by
107-
// the App.build method, and use it to set our appbar title.
108-
title: Text(widget.title),
111+
title: const Text('Firebase Data Connect Flutter'),
109112
),
110-
body: Center(
111-
// Center is a layout widget. It takes a single child and positions it
112-
// in the middle of the parent.
113+
body: SingleChildScrollView(
113114
child: Column(
114-
// Column is also a layout widget. It takes a list of children and
115-
// arranges them vertically. By default, it sizes itself to fit its
116-
// children horizontally, and tries to be as tall as its parent.
117-
//
118-
// Column has various properties to control how it sizes itself and
119-
// how it positions its children. Here we use mainAxisAlignment to
120-
// center the children vertically; the main axis here is the vertical
121-
// axis because Columns are vertical (the cross axis would be
122-
// horizontal).
123-
//
124-
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
125-
// action in the IDE, or press "p" in the console), to see the
126-
// wireframe for each widget.
127-
mainAxisAlignment: MainAxisAlignment.center,
128115
children: <Widget>[
129-
FloatingActionButton(
130-
onPressed: _refreshData,
131-
tooltip: 'Refresh',
132-
child: const Icon(Icons.refresh),
133-
), // This trailing comma makes auto-formatting nicer for build methods.
134-
Center(
135-
child: Text(_movies.length > 1
136-
? "Congratulations! You have implemented all of the TODOs!"
137-
: "If you're seeing this, open lib/main.dart and implement the TODOs"),
116+
_buildMovieList('Top 10 Movies', _topMovies),
117+
_buildMovieList('Latest Movies', _latestMovies),
118+
],
119+
),
120+
),
121+
);
122+
}
123+
124+
Widget _buildMovieList(String title, List<ListMoviesMovies> movies) {
125+
return Column(
126+
crossAxisAlignment: CrossAxisAlignment.start,
127+
children: [
128+
Padding(
129+
padding: const EdgeInsets.all(8.0),
130+
child: Text(
131+
title,
132+
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
133+
),
134+
),
135+
SizedBox(
136+
height: 300, // Adjust the height as needed
137+
child: ListView.builder(
138+
scrollDirection: Axis.horizontal,
139+
itemCount: movies.length,
140+
itemBuilder: (context, index) {
141+
return _buildMovieItem(movies[index]);
142+
},
143+
),
144+
),
145+
],
146+
);
147+
}
148+
149+
Widget _buildMovieItem(ListMoviesMovies movie) {
150+
return Flexible(
151+
child: Container(
152+
width: 150, // Adjust the width as needed
153+
padding: const EdgeInsets.all(4.0),
154+
child: Card(
155+
child: Column(
156+
crossAxisAlignment: CrossAxisAlignment.start,
157+
children: [
158+
AspectRatio(
159+
aspectRatio: 9 / 16, // 9:16 aspect ratio for the image
160+
child: Image.network(
161+
movie.imageUrl,
162+
fit: BoxFit.cover,
163+
),
138164
),
139-
Expanded(
140-
child: ListView.builder(
141-
itemBuilder: (BuildContext context, int index) {
142-
return GestureDetector(
143-
child: Container(
144-
child: Card(
145-
child: Padding(
146-
padding: EdgeInsets.all(50.0),
147-
child: Text(
148-
_movies[index].title,
149-
style:
150-
TextStyle(fontWeight: FontWeight.bold),
151-
)))));
152-
},
153-
itemCount: _movies.length,
165+
const SizedBox(height: 8),
166+
Padding(
167+
padding: EdgeInsets.all(8.0),
168+
child: Text(
169+
movie.title,
170+
overflow: TextOverflow.ellipsis,
171+
style: const TextStyle(fontWeight: FontWeight.bold),
154172
),
155-
)
173+
),
156174
],
157175
),
158176
),
159-
);
177+
));
160178
}
161179
}

0 commit comments

Comments
 (0)