Skip to content

Commit

Permalink
[timetable] crop the wallpaper
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Sep 24, 2024
1 parent d29976c commit f822ff3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
4 changes: 4 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
/>
</intent-filter>
</activity>
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand Down
10 changes: 8 additions & 2 deletions lib/timetable/p13n/page/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class _TimetableBackgroundEditorState extends ConsumerState<TimetableBackgroundE
FileImage(renderImageFile),
);
if (added) {
if (!context.mounted) return;
if (!context.mounted) return;
context.push("/timetable/palettes/custom");
}
},
Expand Down Expand Up @@ -261,7 +261,13 @@ class _TimetableBackgroundEditorState extends ConsumerState<TimetableBackgroundE
);
if (fi == null) return;
if (!mounted) return;
final file = File(fi.path);
var file = File(fi.path);
if (Platform.isAndroid || Platform.isIOS) {
final croppedFile = await cropImage(context, file);
if (croppedFile != null) {
file = croppedFile;
}
}
setState(() {
rawPath = fi.path;
renderImageFile = file;
Expand Down
24 changes: 24 additions & 0 deletions lib/utils/images.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:universal_platform/universal_platform.dart';
import 'package:path/path.dart' as path;

Expand Down Expand Up @@ -28,3 +30,25 @@ Future<void> copyCompressedImageToTarget({
await source.copy(target);
}
}

Future<File?> cropImage(BuildContext context, File imageFile) async {
final croppedFile = await ImageCropper().cropImage(
sourcePath: imageFile.path,
uiSettings: [
AndroidUiSettings(
aspectRatioPresets: [
CropAspectRatioPreset.original,
CropAspectRatioPreset.square,
],
),
IOSUiSettings(
aspectRatioPresets: [
CropAspectRatioPreset.original,
CropAspectRatioPreset.square,
],
),
],
);
if (croppedFile == null) return null;
return File(croppedFile.path);
}
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.2.0"
image_cropper:
dependency: "direct main"
description:
name: image_cropper
sha256: fe37d9a129411486e0d93089b61bd326d05b89e78ad4981de54b560725bf5bd5
url: "https://pub.dev"
source: hosted
version: "8.0.2"
image_cropper_for_web:
dependency: transitive
description:
name: image_cropper_for_web
sha256: "34256c8fb7fcb233251787c876bb37271744459b593a948a2db73caa323034d0"
url: "https://pub.dev"
source: hosted
version: "6.0.2"
image_cropper_platform_interface:
dependency: transitive
description:
name: image_cropper_platform_interface
sha256: e8e9d2ca36360387aee39295ce49029362ae4df3071f23e8e71f2b81e40b7531
url: "https://pub.dev"
source: hosted
version: "7.0.0"
image_picker:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ dependencies:
super_context_menu: ^0.8.22
animated_size_and_fade: ^4.0.0
dismissible_page: ^1.0.2

image_cropper: ^8.0.2
# Utils
# dart.io.Platform API for Web
universal_platform: ^1.1.0
Expand Down
3 changes: 3 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
<script type="application/javascript" src="/assets/packages/flutter_inappwebview_web/assets/web/web_support.js" defer></script>
<!-- cropperjs -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.6.2/cropper.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/cropperjs/1.6.2/cropper.min.js"></script>
</head>
</head>
<body>
Expand Down

0 comments on commit f822ff3

Please sign in to comment.