|
| 1 | +import 'package:dataconnect/models/movie.dart'; |
| 2 | +import 'package:dataconnect/movie_state.dart'; |
| 3 | +import 'package:dataconnect/router.dart'; |
| 4 | +import 'package:dataconnect/widgets/list_movies.dart'; |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:firebase_core/firebase_core.dart'; |
| 8 | +import 'firebase_options.dart'; |
| 9 | +import 'package:flutter_web_plugins/url_strategy.dart'; |
| 10 | +import 'util/auth.dart'; |
| 11 | +import 'movies_connector/movies.dart'; |
| 12 | + |
| 13 | +bool isSetup = false; |
| 14 | +void main() async { |
| 15 | + WidgetsFlutterBinding.ensureInitialized(); |
| 16 | + usePathUrlStrategy(); |
| 17 | + try { |
| 18 | + await Firebase.initializeApp( |
| 19 | + options: DefaultFirebaseOptions.currentPlatform, |
| 20 | + ); |
| 21 | + isSetup = true; |
| 22 | + |
| 23 | + await Auth.instance.init(); |
| 24 | + int port = 443; |
| 25 | + String hostName = Uri.base.host; |
| 26 | + bool isSecure = true; |
| 27 | + if (!kIsWeb) { |
| 28 | + hostName = '10.0.2.2'; |
| 29 | + port = 9403; |
| 30 | + isSecure = false; |
| 31 | + } |
| 32 | + MoviesConnector.instance.dataConnect |
| 33 | + .useDataConnectEmulator(hostName, port, isSecure: isSecure); |
| 34 | + } catch (_) { |
| 35 | + // The user hasn't run ./installDeps.sh yet |
| 36 | + } |
| 37 | + |
| 38 | + runApp(const MyApp()); |
| 39 | +} |
| 40 | + |
| 41 | +class MyApp extends StatelessWidget { |
| 42 | + const MyApp({super.key}); |
| 43 | + |
| 44 | + // This widget is the root of your application. |
| 45 | + @override |
| 46 | + Widget build(BuildContext context) { |
| 47 | + return MaterialApp.router( |
| 48 | + theme: ThemeData.dark(), |
| 49 | + routerConfig: router, |
| 50 | + ); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +class MyHomePage extends StatefulWidget { |
| 55 | + const MyHomePage({super.key}); |
| 56 | + |
| 57 | + @override |
| 58 | + State<MyHomePage> createState() => _MyHomePageState(); |
| 59 | +} |
| 60 | + |
| 61 | +class _MyHomePageState extends State<MyHomePage> { |
| 62 | + List<ListMoviesMovies> _topMovies = []; |
| 63 | + List<ListMoviesMovies> _latestMovies = []; |
| 64 | + bool _showMessage = true; |
| 65 | + |
| 66 | + @override |
| 67 | + void initState() { |
| 68 | + super.initState(); |
| 69 | + |
| 70 | + if (isSetup) { |
| 71 | + MovieState.getTopTenMovies().then((res) { |
| 72 | + if (res.data.movies.isNotEmpty) { |
| 73 | + if (mounted) { |
| 74 | + setState(() { |
| 75 | + _showMessage = false; |
| 76 | + _topMovies = res.data.movies; |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + MovieState.getTopTenMovies().then((res) { |
| 83 | + setState(() { |
| 84 | + _latestMovies = res.data.movies; |
| 85 | + }); |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @override |
| 91 | + Widget build(BuildContext context) { |
| 92 | + return Scaffold( |
| 93 | + body: SafeArea( |
| 94 | + child: SingleChildScrollView( |
| 95 | + child: _showMessage || !isSetup |
| 96 | + ? Center( |
| 97 | + child: Column( |
| 98 | + mainAxisAlignment: MainAxisAlignment.center, |
| 99 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 100 | + children: [ |
| 101 | + Padding( |
| 102 | + padding: const EdgeInsets.all(30.0), |
| 103 | + child: isSetup |
| 104 | + ? Text( |
| 105 | + 'Go to the Firebase Data Connect extension, and click start Emulators. ${_latestMovies.isEmpty ? 'Then open dataconnect/moviedata_insert.gql and the click "Run(local)".' : ''} Then, refresh the page.') |
| 106 | + : const NumberedList(bullets: [ |
| 107 | + 'Make sure you already have a Firebase Project', |
| 108 | + 'In the sidebar, open The Firebase Data Connect Extension', |
| 109 | + 'Sign into your google account associated with your firebase project', |
| 110 | + 'Select your firebase project', |
| 111 | + 'Open a new terminal (by clicking the + icon below) and Run "flutterfire configure -y -a com.example.dataconnect"', |
| 112 | + 'Refresh this preview page' |
| 113 | + ])) |
| 114 | + ], |
| 115 | + )) |
| 116 | + : Column( |
| 117 | + children: <Widget>[ |
| 118 | + ListMovies( |
| 119 | + title: 'Top 10 Movies', |
| 120 | + movies: _topMovies |
| 121 | + .map( |
| 122 | + (e) => Movie( |
| 123 | + id: e.id, |
| 124 | + title: e.title, |
| 125 | + imageUrl: e.imageUrl, |
| 126 | + description: e.description), |
| 127 | + ) |
| 128 | + .toList()), |
| 129 | + ListMovies( |
| 130 | + title: 'Latest Movies', |
| 131 | + movies: _latestMovies |
| 132 | + .map( |
| 133 | + (e) => Movie( |
| 134 | + id: e.id, |
| 135 | + title: e.title, |
| 136 | + imageUrl: e.imageUrl, |
| 137 | + description: e.description), |
| 138 | + ) |
| 139 | + .toList()), |
| 140 | + ], |
| 141 | + ), |
| 142 | + )), |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +class NumberedList extends StatelessWidget { |
| 148 | + const NumberedList({super.key, required this.bullets}); |
| 149 | + |
| 150 | + final List<String> bullets; |
| 151 | + |
| 152 | + @override |
| 153 | + Widget build(BuildContext context) { |
| 154 | + return Container( |
| 155 | + alignment: Alignment.centerLeft, |
| 156 | + padding: const EdgeInsets.fromLTRB(16, 15, 16, 16), |
| 157 | + child: Column( |
| 158 | + children: bullets |
| 159 | + .asMap() |
| 160 | + .keys |
| 161 | + .map((index) => Row(children: [ |
| 162 | + Text( |
| 163 | + "${index + 1}.", |
| 164 | + style: const TextStyle( |
| 165 | + fontSize: 16, height: 1.55, color: Colors.white), |
| 166 | + ), |
| 167 | + const SizedBox( |
| 168 | + width: 5, |
| 169 | + ), |
| 170 | + Expanded( |
| 171 | + child: Text( |
| 172 | + bullets[index], |
| 173 | + textAlign: TextAlign.left, |
| 174 | + softWrap: true, |
| 175 | + style: const TextStyle( |
| 176 | + fontSize: 16, |
| 177 | + color: Colors.white, |
| 178 | + height: 1.55, |
| 179 | + ), |
| 180 | + ), |
| 181 | + ) |
| 182 | + ])) |
| 183 | + .toList())); |
| 184 | + } |
| 185 | +} |
0 commit comments