You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
5.9 KiB
173 lines
5.9 KiB
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:hadi_hoda_flutter/common_ui/resources/my_assets.dart';
|
|
import 'package:hadi_hoda_flutter/common_ui/resources/my_spaces.dart';
|
|
import 'package:hadi_hoda_flutter/common_ui/resources/my_text_style.dart';
|
|
import 'package:hadi_hoda_flutter/core/routers/my_routes.dart';
|
|
import 'package:hadi_hoda_flutter/core/status/base_status.dart';
|
|
import 'package:hadi_hoda_flutter/core/utils/convert_size.dart';
|
|
import 'package:hadi_hoda_flutter/core/utils/my_localization.dart';
|
|
import 'package:hadi_hoda_flutter/core/utils/screen_size.dart';
|
|
import 'package:hadi_hoda_flutter/core/widgets/error/error_state.dart';
|
|
import 'package:hadi_hoda_flutter/core/widgets/images/my_image.dart';
|
|
import 'package:hadi_hoda_flutter/features/download/domain/entities/download_entity.dart';
|
|
import 'package:hadi_hoda_flutter/features/download/presentation/bloc/download_bloc.dart';
|
|
import 'package:hadi_hoda_flutter/features/download/presentation/bloc/download_event.dart';
|
|
import 'package:hadi_hoda_flutter/features/download/presentation/bloc/download_state.dart';
|
|
import 'package:hadi_hoda_flutter/features/download/presentation/ui/widgets/download_loading_widget.dart';
|
|
|
|
/// Download page that supports downloading any combination of media types.
|
|
///
|
|
/// Pass [config] to auto-start downloading specific types when the page opens.
|
|
/// If [config] is null, the page assumes the download was already triggered
|
|
/// externally and just shows the progress.
|
|
class DownloadPage extends StatefulWidget {
|
|
final DownloadPageConfig config;
|
|
|
|
const DownloadPage({super.key, required this.config});
|
|
|
|
@override
|
|
State<DownloadPage> createState() => _DownloadPageState();
|
|
}
|
|
|
|
class _DownloadPageState extends State<DownloadPage> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final bloc = context.read<DownloadBloc>();
|
|
|
|
if (!bloc.isDownloading) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) {
|
|
bloc.add(StartDownloadEvent(toLevel: widget.config.downloadToLevel));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Container(
|
|
height: context.heightScreen,
|
|
width: context.widthScreen,
|
|
decoration: BoxDecoration(
|
|
gradient: const LinearGradient(
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
colors: [Color(0XFF00154C), Color(0XFF150532)],
|
|
),
|
|
image: DecorationImage(
|
|
image: const AssetImage(MyAssets.pattern),
|
|
scale: 3,
|
|
repeat: ImageRepeat.repeat,
|
|
colorFilter: ColorFilter.mode(
|
|
Colors.white.withValues(alpha: 0.2),
|
|
BlendMode.srcIn,
|
|
),
|
|
),
|
|
),
|
|
child: BlocConsumer<DownloadBloc, DownloadState>(
|
|
listener: (context, state) {
|
|
if (state.status is BaseComplete) {
|
|
if (widget.config.redirectTo == Routes.homePage) {
|
|
context.goNamed(Routes.homePage);
|
|
} else {
|
|
context.pushNamed(
|
|
widget.config.redirectTo,
|
|
pathParameters: widget.config.routeParams,
|
|
);
|
|
}
|
|
}
|
|
},
|
|
buildWhen: (previous, current) => previous.status != current.status,
|
|
builder: (context, state) {
|
|
if (state.status is BaseError) {
|
|
return Padding(
|
|
padding: EdgeInsets.symmetric(
|
|
vertical:
|
|
MediaQuery
|
|
.viewPaddingOf(context)
|
|
.bottom + MySpaces.s16,
|
|
horizontal: 60,
|
|
),
|
|
child: ErrorState(
|
|
onTap: () {
|
|
context.read<DownloadBloc>().add(
|
|
StartDownloadEvent(
|
|
toLevel: widget.config.downloadToLevel,
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
} else {
|
|
return Stack(
|
|
alignment: Alignment.center,
|
|
children: [_image(), _text(context), _loading(context)],
|
|
);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _image() {
|
|
return const Stack(
|
|
children: [
|
|
MyImage(image: MyAssets.hadiHoda),
|
|
PositionedDirectional(
|
|
start: MySpaces.s10,
|
|
top: MySpaces.s40,
|
|
child: MyImage(image: MyAssets.globe),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _text(BuildContext context) {
|
|
return PositionedDirectional(
|
|
bottom: 130,
|
|
child: Column(
|
|
spacing: MySpaces.s6,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8.0),
|
|
child: Text(context.translate.please_wait,
|
|
style: MYTextStyle.titr0.copyWith(fontSize: 24),
|
|
textAlign: TextAlign.center,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis),
|
|
),
|
|
StreamBuilder<DownloadEntity>(
|
|
initialData: DownloadEntity.empty(),
|
|
stream: context
|
|
.read<DownloadBloc>()
|
|
.loadingStream,
|
|
builder: (context, snapshot) =>
|
|
Text(
|
|
'Downloading ...${snapshot.data?.downloadedLevels}/${widget
|
|
.config.downloadToLevel}',
|
|
textAlign: TextAlign.center,
|
|
style: MYTextStyle.matn3,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Positioned _loading(BuildContext context) {
|
|
return Positioned(
|
|
bottom: MySpaces.s40,
|
|
child: DownloadLoadingWidget(
|
|
loadingStream: context
|
|
.read<DownloadBloc>()
|
|
.loadingStream,
|
|
),
|
|
);
|
|
}
|
|
}
|