privacy blur
a cross-platform application to obfuscate sensitive data from images, targeting ios and android devices. mainly written in dart with the flutter sdk.
the app is available on google playstore and the the appstore now!
## what does the app do?
the project is aiming to provide a free, clean and simple solution for users to manipulate image data.
no in-app purchases. no ads. no watermark. no hassle. free forever because privacy shouldn’t cost anything. free because we care!
code style
we have some restrictions about the code style of this project.
you can find all requirements in the contributing
features
- face detection
- blur / pixelate effect
- fine / coarse grain effect
- round / square area
- export to your camera roll
flutter sdk setup
the app is running on flutter sdk 2.2 and dart 2.13.
before working with the project, please make sure to run flutter upgrade
.
windows
click here:
installation guide on windows
macos
click here:
installation guide on macos
building
the app is targeted for ios and android on phones and tablets.
desktop and web platform may cause issues and are currently not planned.
it’s recommended to run:
flutter clean
flutter pub get
as soon as building issues appear.
ios-building
flutter guide for building on ios
you will need an macos machine to be able to run a flutter ios application.
please also make sure you installed the correct version of cocoapods and xcode on your machine.
for deployment information visit: deployment guide ios
android-building
flutter guide for building on android
no further requirements for building an android version.
for deployment information visit: deployment guide android
structure
lib/--+--main.dart (entry point)
|
+--resources/--- images, fonts, strings, etc...
|
+--------src/--+----- app.dart (some inital code)
+---router.dart (navigation handling)
+-------di.dart (dependency injection)
|
|
+--screens/--screen_name/
| +--helpers/-- (garbage place, like many events and states)
| +--widgets/-- (internal widgets for this screen)
| |
| +--repo.dart
| +--bloc.dart
| +--view.dart
|
|
+--widgets/-- (common widgets for application)
|
|
+----utils/-- (some utils for application if necessary)
architecture
in order to build a maintainable, scalable and testable project, the app is build with an architectural pattern and file structure.
for readability purposes we limited the file size to 300 lines.
navigation
the navigation is kept relatively simple with flutter navigation v1.
the router.dart
file includes all routing and navigation logic in 2 classes.
- screennavigator: implements and provides methods to enable routing
- approuter: declares all routes and their configuration
to be able to test each route individually the approuter has multiple constructors. a constructor must also be defined for each route.
the constructor then overwrites what is the actual initial route and boots up at the specified location.
each route receives the dependency injection instance, and the approuter instance itself. optional arguments follow as 3rd parameter.
dependency injection
the dependency injection class is used to inject all testable parts of the application in the right order.
it holds the provider for different blocs and repositories. the instance of the class must be passed to the view, so that they can access all dependencies in the provider/consumer structure.
bloc
in dart its useful to work with streams, and the provider pattern. that’s why we chose the popular bloc pattern.
it makes sense to implement a bloc for each screen, but only if it needs to hold and manage data. usually the bloc receives the corresponding repositories from the dependency injection.
the bloc class file is located in the same directory as the screen view.
each view is using events
to trigger state
updates inside the bloc, which are consumed by the view again.
events and states are located inside the helpers’ directory on the screens’ folder.
to be able to decide which state should be emitted, the bloc consumes events and handles the decision based on that.
it’s important to always yield something after each event, otherwise the ui won’t be updated and blocked.
stream<samplestate> mapeventtostate(sampleevent event) async* {
if (event is eventone) {
yield* handlestateonechange(event);
} else if (event is eventtwo) {
yield* handlestatetwochange(event);
} ...
}
the bloc works as the logic component of the screen and separates all logic from the ui. thus, the bloc also shouldn’t handle any ui parts.
views
each view represents the widget, that is used as screen in the navigation route. it should only define the ui and consume/use state.
primarily it’s better to implement screens as statelesswidgets and only rerender the parts of the screen which really need to be rendered after state updates.
a bloc-screen is wrapped by a multiblocprovider, which gets the corresponding bloc from the dependency injection and makes it available through context for the nested blocconsumer.
return multiblocprovider(
providers: [_di.getsamplebloc()],
child: blocconsumer<samplebloc, samplestate>(...)
on simple ui data changes as displaying a toast message we used the buildwhen condition of the bloc consumer to prevent a whole rerender. only the messagebar is drawn above the screen by the listener.
blocconsumer<samplebloc, samplestate>(
listenwhen: (_, curstate) => (curstate is simplestate),
buildwhen: (_, curstate) => !(curstate is simplestate),
listener: (_, state) {
showmessageonsimplestate(...)
}
...
most parts of the views are separated in own methods. this improves readability and modularity. as soon the file size exceeds 300 lines or widgets grow to a huge size it makes sense to move the widgets them to another file.
widgets
widgets exist on common and screen scope level. common widgets are located in src/widgets
and screen widgets in src/screen_name/widgets
.
widgets on common level shouldn’t implement screen specific configurations and should be made reusable with minimal setup.
widgets on screen scope are either only used in this place, or their usage is very specific to the screen.
stateful widgets
best practise for widgets which hold state is to also manage it internally. for example:
class widgetstate extends state<widgetwithstate> {
late double _initialvalue;
@override
void didupdatewidget(covariant deferredslider oldwidget) {
_initialvalue = widget._initialvalue;
super.didupdatewidget(oldwidget);
}
@override
void initstate() {
_initialvalue = widget._initialvalue;
super.initstate();
}
... widget build ...
void onchanged(doublevalue) {
setstate(() {
_initialvalue = doublevalue;
});
this.widget.onchanged(doublevalue);
}
}
adaptive widgets
the app is targeted for ios and android, but needs to look different on each platform.
that’s why we split platform specific code (ios/android) inside src/widgets/adaptive_widgets_builder.dart
.
we saw, that flutter is starting to build adaptive versions of widgets.
when they will complete with this, we can remove and replace “builders” inside this file with the adaptive version of flutter widgets.
internationalization
the app is translated into:
- :us: english (default language)
- :de: german
translations live inside the lib/resources/i18n
directory.
to add new languages you need to do the following:
- add lang code to supported languages
var delegate = await localizationdelegate.create(
...
supportedlocales: [..., '<language_code>']);
- add json file for lang code in
lib/resources/i18n/<lang_code>.json
- copy structure from an existing .json file
- as soon as new texts are added, the new keys need to be generated. run: flutter
flutter pub run build_runner build --delete-conflicting-outputs
theme
the theme is split into different platforms and additionally thememodes for android.
theme_provider.dart
- iostheme (providing thememode split ios themedata)
- light (providing light android themedata)
- dark (providing dark android themedata)
dark and light mode are supported and depend on the system preference.
// appbuilder widget
thememode: thememode.system,
assets/resources
all assets/resources can be found in lib/resouces
.
images, icons and other common resources should be placed here.
icons
currently, the app is using a custom icon font which includes 4 icons for the image editing tools in the toolbar selection.
fonts can be generated here: fluttericon.com.
do only include icons that are used inside the project!
additional flutter provides default icons that can be used either from cupertinoicons
or icons
class.
to handle icons from one place the project has the icons_provider class.
this is the place where one iconname can be defined and can have extra logic to split between platforms.
images
images can be added here. to handle different screen resolutions with image sizes there is the possibility to add an image in x2 and x3 sizes for the referring screens.
on dart side its enough to load it like this: lib/resources/images/<image_name>
. dart handles the split between the resolutions.
currently, the app only uses the app logo which is essential on launch-/splash and main-screen. its also recommended limiting the number of static image files, due to the fact that they extend the project size by a large amount.
strings
in this place all text strings that are used inside the application. it is planned to add i18n and i10n on a later stage of the project.
all strings inside here need to be moved to the translation .aab files then.
imagefilter library description
this library is a singleton. it uses very much memory while working, so its very bad idea to create few instances of this class.
class can load and filter images.
supported filters:
- pixelate
- linear blur (not gaussian, too slow)
class features:
- preview of changes before commit
- undo changes in some special area (erase tool)
- processing squared and rounded areas
- processing multiple areas in one transaction
- overriding processed pixels all together with selected filter
how to work with this class:
import 'dart:ui' as img_tools;
final file = file(filename);
final imagefilter = imageappfilter();
var completer = completer<img_tools.image>();
img_tools.decodeimagefromlist(file.readasbytessync(), (result) {
completer.complete(result);
});
var image = await completer.future;
/// very important to use await here!!!
var filteredimage = await imagefilter.setimage(_blocstate.image);
imagefilter.transactionstart();
imagefilter.setmatrix(matrixapppixelate(20));
imagefilter.apply2circlearea(200, 200, 150);
/// for preview before saving (only changed area of image will be updated)
filteredimage = await imagefilter.getimage();
imagefilter.setmatrix(matrixappblur(20));
imagefilter.apply2circlearea(400, 400, 250);
/// save to cached image, after that you can not cancel changes
/// only if you load image again.
imagefilter.transactioncommit();
/// get saved image after transaction completed (complete merged image without changed part)
filteredimage = await imagefilter.getimage();
methods
- static void setmaxprocessedwidth(int) – default 1000 for blur speed optimization we need to know maximum processed area width
- future<imagefilterresult> setimage(dart:ui.image) – set image for filtering.
- future<imagefilterresult> getimage() – if transactions is active, will return only changed area without background image. if transaction is closed – will return full updated images.
- void transactionstart() – open transaction for changes
- void transactioncancel() – reset changes and close transaction
- void transactioncommit() – apply changes from transaction and close transaction
- void setfilter(imageappmatrix newmatrix) – set current filter
- void apply2squarearea(int x1, int y1, double radius) – apply selected filter to square area with center and radius
- void apply2circlearea(int x1, int y1, double radius) – apply selected filter to circle area with center and radius
- void cancelcurrent() – cancel all current changes in active transaction
- void cancelsquare(int, int, int, int) – cancel changes in square area
- void cancelcircle(int, int, int) – cancel changes in circle area
filters
- matrixapppixelate(int blocksize)
- matrixappblur(int blocksize) – linear blur
testing
run flutter test
command in project root or test folder. all test-files must be ended with _test.dart suffix
dependencies
do only add null-safety dependencies!
the application runs in sound null safety. consequently, all dependencies added need to follow this restriction.
- flutter_styled_toast: used for alerts
- image_size_getter: get image sizes to decide if image should be rotated
- flutter_exif_rotation: used for initial rotation on image load
- url_launcher: used to open a link in main screen
- image_picker: used to get the access to the image library
- image: used for image file handling
- image_gallery_saver: used to save image to library
- permission_handler: used for accessing os permission status
- path_provider: used to read os image paths
- bloc: library for architectural pattern class
- flutter_bloc: flutter additional features for dart bloc library
- mockito: used for mocks in testing
- bloc_test: used for testing blocs
- flutter_translate: adding i18n to the app
- flutter_translate_gen: used to generate variables for static usage of translations
Comments are closed.