|Summary||Learn how to build and deploy a Flutter application on Ubuntu Core by leveraging Ubuntu Frame|
|Author||Bugra Aydogar email@example.com|
Flutter is an open-source UI software development kit created by Google. It is used to develop cross-platform applications. With Flutter, you can build natively compiled applications for mobile, web, desktop, and embedded devices from a single codebase.
Flutter also introduced native Linux support for Wayland, making it possible to run Flutter apps on Linux. This provides developers with more opportunities to build new embedded graphic applications for kiosks, digital signage, infotainment, or any other edge device.
But Flutter and other UI toolkits are not enough for embedding graphic applications into edge devices. For this, you need tools that provide orchestration services between functionalities, such as input modalities, display mode, windows behaviours, interfacing with GPU and more. This is where Ubuntu Frame comes in to ease the deployment of any graphic application.
What you’ll learn
In this tutorial, you will learn how to generate, build and snap a Flutter application on Ubuntu Core by leveraging Ubuntu Frame.
What you’ll need
An Ubuntu desktop running any current release of Ubuntu or an Ubuntu Virtual Machine on another OS.
A ‘Target Device’ from one of the following:
A device running Ubuntu Core.
This guide shows you you how to set up a supported device. If there’s no supported image that fits your needs you can create your own image.
- Using a Virtual Machine (VM). You don’t need to have a physical “Target Device”, you can follow the tutorial with Ubuntu Core in a VM. This guide shows you how to set up an Ubuntu Core VM.
- Using Ubuntu Classic. You don’t have to use Ubuntu Core. You can use also a “Target Device” with Ubuntu Classic. Read this guide to understand how to run kiosk snaps on your desktop, as the particular details won’t be repeated here.
- A device running Ubuntu Core.
To be able to create and build Flutter applications, you would need to have Flutter and its tools installed. There are different ways to install Flutter on your Ubuntu Classic, but the recommended way is using the snap.
sudo snap install flutter --classic
In addition to this, the snapcraft tool must be installed so that you can create snap out of your Flutter application. You can use the following command to install snapcraft on Ubuntu Classic system.
sudo snap install snapcraft --classic
Now, we are ready to create our Flutter application.
Creating and Building Flutter Applications
Flutter does not enable desktop support by default. You have to explicitly configure your Flutter CLI tool. The rest is creating the project and running it. Easy peasy !
flutter config --enable-linux-desktop # enable desktop support flutter create flutterdemo # your flutter application name cd flutterdemo # Go to your app's root directory flutter run -d linux # Start your flutter application.
Once you run the above commands, you have to see the following screen on your Ubuntu Classic.
Perfect. Now our Flutter application is working fine on Ubuntu Classic. As a next step, we will create snap to be able to run it on Ubuntu Core.
Creating the Snap
Creating a snap package from your existing applications is quite easy task thanks to the snapcraft CLI tool.
cd your-app-directory # go to your Flutter app's root directory snapcraft init # initiate your snap project
Now, it needs a little bit of attention. Here is the initial version of the snapcraft.yaml file of our application. Let’s call our application “super-cool-app”.
name: super-cool-app version: 0.1.0 summary: Super Cool App description: Super Cool App confinement: strict base: core18 grade: stable slots: dbus-super-cool-app: interface: dbus bus: session name: com.example.flutterdemo # replace with your app name apps: super-cool-app: command: flutterdemo # replace with your app name extensions: [flutter-master] plugs: - network slots: - dbus-super-cool-app parts: super-cool-app: source: . plugin: flutter flutter-target: lib/main.dart # The main entry-point file of the application
Your snapcraft.yaml is ready to be built. It is suggested to use lxd while building snaps so that your machine will not be polluted.
If everything goes well, you will have your snap package in your directory. You can install it by using the following command. We have to use dangerous flag since we did not deploy our application to the Snap Store.
sudo snap install <your-app-name.snap> --dangerous # install your snap to your ubuntu classic system super-cool-app # run your snap, replace with your own snap app
Now, you should be able to see it working on the Desktop. That’s great. You converted your application into the snap package format and made it possible to run on your Desktop
We’re not done just yet. We have to adjust our snapcraft.yaml file more to make it run on Ubuntu Core by leveraging ubuntu-frame.
Adjustments for Ubuntu Core
There are three points that we need to take care of in order to run the Flutter applications on Ubuntu Core.
Currently, the Ubuntu Core platform does not have the required fonts that are needed by Flutter applications.
UI apps generally must be available upon boot and therefore, to satisfy this, we have to create a daemon.
Since Flutter apps are running on top of ubuntu-frame, we have to make the necessary adjustments for ubuntu-frame integration.
Here is the snapcraft.yaml where all the mentioned issues are addressed.
name: super-cool-app version: 0.1.0 summary: Super Cool App description: Super Cool App that does everything! confinement: strict base: core18 grade: stable apps: daemon: daemon: simple restart-condition: always command-chain: - bin/run-daemon - bin/wayland-launch - bin/gl-launch command: bin/flutterdemo extensions: [flutter-master] # Where "master" defines which Flutter channel to use for the build super-cool-app: command-chain: - bin/wayland-launch - bin/gl-launch command: flutterdemo extensions: [flutter-master] # Where "master" defines which Flutter channel to use for the build parts: super-cool-app: source: . plugin: flutter flutter-target: lib/main.dart # The main entry-point file of the application mir-kiosk-snap-launch: plugin: dump source: https://github.com/MirServer/mir-kiosk-snap-launch.git override-build: $SNAPCRAFT_PART_BUILD/build-with-plugs.sh opengl pulseaudio wayland stage-packages: - inotify-tools # Some arm devices (notably RPi4) only work with gles gles-on-arm: plugin: nil override-build: | snapcraftctl build mkdir -p $SNAPCRAFT_PART_INSTALL/bin/ if [ "$SNAPCRAFT_TARGET_ARCH" != "arm64" ]; then echo 'exec "$@"' > $SNAPCRAFT_PART_INSTALL/bin/gl-launch else echo 'GDK_GL=gles exec "$@"' > $SNAPCRAFT_PART_INSTALL/bin/gl-launch fi chmod a+x $SNAPCRAFT_PART_INSTALL/bin/gl-launch assets: plugin: nil stage-packages: - dmz-cursor-theme - fonts-dejavu - fonts-freefont-ttf - fonts-ubuntu layout: /usr/share/fonts: bind: $SNAP/usr/share/fonts /etc/fonts: bind: $SNAP/etc/fonts /usr/share/icons: bind: $SNAP/usr/share/icons
Re-create your snap after these modifications, and it will be ready for testing on Ubuntu Core.
Running Your Flutter Application Snap on Ubuntu Core
You can use qemu and pre-built Ubuntu Core18 or Ubuntu Core20 images that are available to test the generated snap.
Assuming you already have an Ubuntu Core system up and running, just install ubuntu-frame using
snap install ubuntu-frame
Then, transfer your snap via scp. Finally install it by using;
sudo snap install <your-app-name.snap> --dangerous
After a couple of seconds, your application will appear on your screen!
You made it!
If you are looking for more advanced Flutter Applications, you can find the reference snapcraft.yaml file for the Flutter Gallery Demo app that is provided by Flutter community to show case most of the features that Flutter offers.
And if you have a kiosk or digital signage product and need help deploying an Flutter app, talk to us!