2024-11-10 13:48:42 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
2024-11-08 16:09:46 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-11-10 13:48:42 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2024-11-21 14:55:00 +00:00
|
|
|
import 'package:responsive_framework/responsive_framework.dart';
|
2024-11-08 16:09:46 +00:00
|
|
|
|
|
|
|
class AppBackground extends StatelessWidget {
|
|
|
|
final Widget child;
|
2024-11-15 15:08:29 +00:00
|
|
|
final bool isLessOptimization;
|
2024-11-21 14:55:00 +00:00
|
|
|
final bool isRoot;
|
2024-11-15 15:08:29 +00:00
|
|
|
const AppBackground({
|
|
|
|
super.key,
|
|
|
|
required this.child,
|
|
|
|
this.isLessOptimization = false,
|
2024-11-21 14:55:00 +00:00
|
|
|
this.isRoot = false,
|
2024-11-15 15:08:29 +00:00
|
|
|
});
|
2024-11-08 16:09:46 +00:00
|
|
|
|
2024-11-15 15:08:29 +00:00
|
|
|
Widget _buildWithBackgroundImage(
|
|
|
|
BuildContext context,
|
|
|
|
File imageFile,
|
|
|
|
Widget child,
|
|
|
|
) {
|
2024-11-10 14:56:18 +00:00
|
|
|
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
|
|
|
|
|
2024-11-15 15:08:29 +00:00
|
|
|
if (isLessOptimization) {
|
|
|
|
final size = MediaQuery.of(context).size;
|
|
|
|
return Container(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
backgroundBlendMode: BlendMode.darken,
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
image: DecorationImage(
|
|
|
|
opacity: 0.2,
|
|
|
|
image: ResizeImage(
|
|
|
|
FileImage(imageFile),
|
|
|
|
width: (size.width * devicePixelRatio).round(),
|
|
|
|
height: (size.height * devicePixelRatio).round(),
|
|
|
|
policy: ResizeImagePolicy.fit,
|
|
|
|
),
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
child: child,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Container(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: LayoutBuilder(
|
|
|
|
builder: (context, constraints) {
|
|
|
|
return Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
backgroundBlendMode: BlendMode.darken,
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
image: DecorationImage(
|
|
|
|
opacity: 0.2,
|
|
|
|
image: ResizeImage(
|
|
|
|
FileImage(imageFile),
|
|
|
|
width: (constraints.maxWidth * devicePixelRatio).round(),
|
|
|
|
height: (constraints.maxHeight * devicePixelRatio).round(),
|
|
|
|
policy: ResizeImagePolicy.fit,
|
|
|
|
),
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-11-10 13:48:42 +00:00
|
|
|
return ScaffoldMessenger(
|
|
|
|
child: FutureBuilder(
|
|
|
|
future:
|
|
|
|
kIsWeb ? Future.value(null) : getApplicationDocumentsDirectory(),
|
|
|
|
builder: (context, snapshot) {
|
2024-11-21 14:55:00 +00:00
|
|
|
if (isRoot ||
|
|
|
|
ResponsiveBreakpoints.of(context).smallerOrEqualTo(MOBILE)) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
final path = '${snapshot.data!.path}/app_background_image';
|
|
|
|
final file = File(path);
|
|
|
|
if (file.existsSync()) {
|
|
|
|
return _buildWithBackgroundImage(context, file, child);
|
|
|
|
}
|
2024-11-10 13:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-21 14:55:00 +00:00
|
|
|
final backgroundColor =
|
|
|
|
ResponsiveBreakpoints.of(context).largerThan(MOBILE)
|
|
|
|
? Colors.transparent
|
|
|
|
: Theme.of(context).colorScheme.surface;
|
|
|
|
|
2024-11-10 13:48:42 +00:00
|
|
|
return Material(
|
2024-11-21 14:55:00 +00:00
|
|
|
color: backgroundColor,
|
2024-11-10 13:48:42 +00:00
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2024-11-08 16:09:46 +00:00
|
|
|
}
|
|
|
|
}
|