|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:flutter_bloc_app_template/features/launches/bloc/launches_bloc.dart'; |
| 4 | +import 'package:flutter_bloc_app_template/generated/l10n.dart'; |
| 5 | +import 'package:flutter_bloc_app_template/widgets/empty_widget.dart'; |
| 6 | + |
| 7 | +class LaunchesScreen extends StatelessWidget { |
| 8 | + const LaunchesScreen({super.key}); |
| 9 | + |
| 10 | + @override |
| 11 | + Widget build(BuildContext context) => Scaffold( |
| 12 | + appBar: AppBar( |
| 13 | + title: Text(S.of(context).messagesTitle), |
| 14 | + ), |
| 15 | + body: RefreshIndicator( |
| 16 | + onRefresh: () async { |
| 17 | + await Future<void>.delayed(const Duration(seconds: 1)); |
| 18 | + }, |
| 19 | + child: SizedBox( |
| 20 | + height: MediaQuery.of(context).size.height, |
| 21 | + child: const LaunchesList(), |
| 22 | + ), |
| 23 | + ), |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +class LaunchesList extends StatelessWidget { |
| 28 | + const LaunchesList({super.key}); |
| 29 | + |
| 30 | + @override |
| 31 | + Widget build(BuildContext context) => |
| 32 | + BlocBuilder<LaunchesBloc, LaunchesState>( |
| 33 | + builder: (context, state) { |
| 34 | + if (state is LaunchesInitial) { |
| 35 | + return const Center( |
| 36 | + child: CircularProgressIndicator(), |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + if (state is LaunchesLoading) { |
| 41 | + return const Center( |
| 42 | + child: CircularProgressIndicator(), |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + if (state is LaunchesEmpty) { |
| 47 | + return Center( |
| 48 | + child: Text(S.of(context).emptyList), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + if (state is LaunchesLoaded) { |
| 53 | + var launches = state.launches; |
| 54 | + |
| 55 | + return ListView.builder( |
| 56 | + physics: const BouncingScrollPhysics(), |
| 57 | + padding: EdgeInsets.zero, |
| 58 | + shrinkWrap: true, |
| 59 | + primary: false, |
| 60 | + itemBuilder: (context, index) => ListTile( |
| 61 | + title: Text(launches[index].missionName ?? 'Name'), |
| 62 | + onTap: () { |
| 63 | + // TODO handle tap |
| 64 | + }, |
| 65 | + ), |
| 66 | + itemCount: launches.length, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + if (state is LaunchesLoadFailure) { |
| 71 | + return Text(S.of(context).error); // TODO |
| 72 | + } |
| 73 | + |
| 74 | + return EmptyWidget(); |
| 75 | + }, |
| 76 | + ); |
| 77 | +} |
0 commit comments