Floating Action Button - Hide on scroll down - SwiftUI

I wanted to create a floating action button in SwiftUI which disappears or hides when scrolling down. For that a few tricks where needed.

FabDemo.gif

Final Look

It is not possible to get the ScrollEvent directly from SwiftUI so the trick is to use a transperant GeometryReader which changes its origin position compared to a defined coordinate space (inspired by this) when scrolling down. When using the origin as offset I can simply determine whether the user scrolls up or down. Here is the code:

Note: I still have a small inconsistency when scrolling to the bottom of the list as the overscroll of the ScrollView causes the last action to be a scroll up which leads to that the Floating action button is showing up when scrolled all the way to the bottom.

Update your Flutter project to Flutter 2.0

Upgrade and Migrate Flutter project to 2.0

Follow these steps:

I want to update my current project to Flutter 2.0 to make use of the new dart sound null-safety
— Problem InShort
  1. Run flutter upgrade in the terminal to upgrade Flutter
  2. Run dart migrate to run the dart migration tool
  3. Solve all errors which the migration tool shows
  4. Run flutter pub outdated --mode=null-safety to print all outdated packages
Screenshot 2021-03-09 at 21.18.05.png

You can see if the packages you depend upon support null-safety.

  1. Run flutter pub upgrade --null-safety to upgrade all packages automatically
  2. Check the code for errors and solve them
  3. Run dart migrate again and it should now be successfull. Follow the link to checkout the proposed changes
  4. Press the "Apply Migration" button
  5. Check the code for errors again and fix them

Congratulations, when finished you should now be able to run the app with sound null-safety.

Run flutter run in the command line and the application should run with the command line displaying:

Screenshot 2021-03-09 at 22.14.28.png

Custom ThemeData Flutter - Extension

In Flutter it is possible to adapt the colors of the app to the current theme of the app - light or dark theme. However, the ThemeData object only has a set of predefined colors which can be changed e.g. scaffoldBackgroundColor, color of the AppBarTheme etc. If you wanted a specific color for a icon in your app which changes also according to the theme, it is not possible to specify on the ThemeData.

With this code it is possible to do just that with a CustomThemeData:

I need a custom Color which changes, when the AppTheme changes.
— Problem InShort

The CustomAppTheme is an InheritedWidget and therefore the CustomAppThemeData can be retrieved the same way ThemeData is.

CustomAppThemeData can be appended with your custom colors.

It is important to note that the CustomAppThemeData is only available in child widgets of the CustomAppTheme widget, so it is desired to have the CustomAppTheme as high up in the widget tree as possible.

The implemenation is very similar to the internal implementation of the ThemeData usage, it is very fast.