# Category: Coding & Scripting

## Flutter: rounded corners for images

There are different possibilities to create a rounded corner of images:

## ClipRRect

ClipRRect(
child: Image.network(
'https://example.com/image.jpg',
height: 100.0,
width: 100.0,
),
),

## BoxDecoration

Container(
height: 100.0,
width: 100.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage('https://example.com/image.jpg'),
),
),
),

Photo by Chaitanya Tvs on Unsplash

## brew: install Java on macOS

The following steps will guide you through the installation of Java on macOS.

First, check the available Java related formulas:

% brew search java

==> Formulae
app-engine-java              java                         javacc                       jslint4java                  pdftk-java
google-java-format           java11                       javarepl                     libreadline-java

Currently, there are two different version of Java: java and java11. To check the version of both, you can use the following commands:

% brew info java

openjdk: stable 16.0.1 (bottled) [keg-only]
Development kit for the Java programming language
https://openjdk.java.net/
Not installed
License: GPL-2.0-only with Classpath-exception-2.0
% brew info java11

openjdk@11: stable 11.0.10 (bottled) [keg-only]
Development kit for the Java programming language
https://openjdk.java.net/
Not installed
License: GPL-2.0-only

Depending on your requirements, you can install one of the above. For me, some of the libraries I use in Dart are currently not compatible with the latest Java version (16.0.1), so I decided to install Java 11 with LTS (long term support).

% brew install java11

This will install Java version 11.0.10 as listed in the output above. The output also shows the following hints:

For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH, run:
echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc For compilers to find openjdk@11 you may need to set: export CPPFLAGS="-I/usr/local/opt/openjdk@11/include" For me it was necessary to called the specified command, so that the system finds the java binary: sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk To see if Java was installed correctly, you can check the version of Java:  % java --version openjdk 11.0.10 2021-01-19 OpenJDK Runtime Environment (build 11.0.10+9) OpenJDK 64-Bit Server VM (build 11.0.10+9, mixed mode) That’s it! Photo by Adam Wilson on Unsplash ## Git: create empty commit to trigger an action Sometimes it’s necessary to trigger an action for CI/CD, e.g. when you use GitLab. To do this without any changes on the code base, you can create an empty commit. For this, git has a command: git commit --allow-empty -m "Trigger Build"  ## Flutter: enable scroll-to-top for nested Scaffolds (e.g. in IndexedStack) When using nested Scaffolds (e.g. in combination with IndexedStack), the PrimaryScrollController is not usable by default. An IndexedStack will load all subviews so scroll-to-top will change all scrollable views at the same time, even if they are not visible or it simply does not work, because the PrimaryScrollController can only be attached to a single Scaffold. To overcome this issue, the scrolls_to_top package can be used. This also works for nested Scaffolds. The functionality of the package is described in this post (here is the english translation). The following code example shows the usage. This is how to use ScrollsToTop within each children of IndexedStack:  final _scrollController = ScrollController(); @override Widget build(BuildContext context) { return Scaffold( primary: true, body: ScrollsToTop( onScrollsToTop: (event) { // onScrollsToTop will be called on each touch event, so check if the view is currently visible if (!widget.isOnScreen) return; _scrollController.animateTo( event.to, duration: event.duration, curve: event.curve, ); }, child: ListView.builder( itemBuilder: _itemBuilder, itemCount: 100, controller: _scrollController, ), ), ); } ## Flutter: add drag handle to ReorderableListView By default, ReorderableListView only shows drag handles on desktop (GitHub). To enable a drag handle inside a ReorderableListView, it is possible to add the following code into the child’s subtree: ReorderableDragStartListener( index: index, child: const Icon(Icons.drag_handle), ), A full example usage with a very simple list: var moviesTitles = ['Inception', 'Heat', 'Spider Man']; ReorderableListView( onReorder: (oldIndex, newIndex) { // Handle reorder }, children: moviesTitles.map((movie) { var index = moviesTitles.indexOf(movie); return ListTile( key: Key('${index}'),
tileColor: Colors.white,
title: Text(movie),
trailing: ReorderableDragStartListener(
index: index,
child: const Icon(Icons.drag_handle),
),
onTap: () {
// Handle tap
},
);
}).toList(),
);

This will result in the following output:

## Adjust text color to be readable on light and dark backgrounds of user interfaces

Most modern user interfaces are supporting different color schemes for day and night: the so called light and dark modes. Selecting a text color for each of those modes is not a big deal and it’s the way to go when designing the user interface.

In some cases, the text color is driven by the displayed contents. In the example below, the tint color is matched to the color of the drink. The global tint color of this app is totally different, but this color adjustment gives a very nice effect. But as you might already see, there is a small problem when it comes to very light or very dark colors: each color either has a good readability on light or dark backgrounds. Some colors might fit to both, but that’s not always the case. In the example below, the light yellow is still visible, but when it comes to small icons or small text, the details are lost.

To overcome this issue, a simple solution is to select two colors for each recipe so that each mode has a different one. That’s fine, but it might totally change the effect of this colored pages.

## Can we calculate a suitable color?

Some time ago, there was an article about Black or white text on a colour background? In this one, I described different algorithms to calculate the best text color (black or white) for a colored background. But now, we need the opposite: a colored text that has a good readability on white (light) or black (dark) backgrounds.

When we look at HSL and HSV/HSB color models, we already have a value for ‘lightness’ or ‘brightness’. The idea is to find a color that matches a given hue and saturation and that has a brightness which is readable on light and dark background. For this, we can use different algorithms. Very good results could be achieved with a ‘Weighted W3C Formula‘. This formula take into consideration that the human eye perceives some of the primary colors darker than others.

f'(x) = r ? 0.299 + g ? 0.587 + b ? 0.11

Each color that is located at the border between the black and white overlay is suitable for light and dark backgrounds.

Step 1: convert the given color to HSV/HSB

Step 2: keep hue and saturation constant and adjust the brightness (make the color lighter or darker)

Step 3: convert the HSV/HSB value back to the required color format

## Implementation in PHP

A simple calculation for a given RGB color is shown below. The classes used in this snippet are available on GitHub. The code checks the initial brightness of the color and lightens or darkens the values until the ‘border’ calculated by the ‘Weighted W3C Formula’ is reached. This is the case for the value 127, the total range of the brightness is 0 to 255.

$hsv = Convert::rgb2hsv($rgb);

$step = 0.01;$brightness = Calculate::weightedW3C($rgb); if ($brightness < 127) {
while ($brightness < 127 &&$hsv[2] >= 0 && $hsv[2] <= 1) {$hsv[2] += $step;$brightness = Calculate::weightedW3C(Convert::hsv2rgb($hsv)); } } else { while ($brightness > 127 && $hsv[2] >= 0 &&$hsv[2] <= 1) {
$hsv[2] -=$step;
$brightness = Calculate::weightedW3C(Convert::hsv2rgb($hsv));
}
}

return Convert::hsv2rgb($hsv); ## Some examples But how does this result look for different colors? Let’s start with some dark colors. Those are fine for a light background, but they become unreadable on a dark one. The top colors show the input color (before) and the color below shows the output of the calculation above (after). And now let’s look at some light colors which are fine for dark backgrounds, but they are totally unreadable on light backgrounds. The last color is similar to the example at the beginning and as you can see, the optimized color has a much better readability. This could be achieved for both light and dark colors. The code example shown above is written in PHP. An adoption should be easily possible for any other coding or scripting language The algorithm mentioned in this post is also available on GitHub https://github.com/mixable/color-utils. This package is usable with composer: composer require mixable/color-utils The optimized color can be calculated with: use Mixable\Color\Calculate; // ...$hex = '#ffcc00';
$optimizedColor = Calculate::readableColorForLightAndDarkBackground($hex);

## Flutter: expand TextField height to match parent widget

To expand the height of TextField to match the parents widgets height, the following code can be used:

Container(
height: 500.0,
child: TextField(
expands: true,
minLines: null,
maxLines: null,
...
),
),


The important thing is, that both minLines and maxLines need to be set to null.

To set the height of the Container to match it’s parent or even the complete screen, height can be set to double.infinity.

## Enable debugging output in PHPUnit

When running PHPunit there are only dots and letters for each test by default:

To enable debug output and get some more details about the tests running, simply add the logging section to phpunit.xml.dist:

    <logging>
<log type="testdox-text" target="php://stdout"/>
</logging>

This will create a debug output and helps to track the tests:

In my case, this helped when my code reached an infinite loop due to an error. This results in a RuntimeException without any outputs or log messages. The process just ended with:

[Symfony\Component\Process\Exception\RuntimeException]
The process has been signaled with signal "11".