<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexander Arendar on Medium]]></title>
        <description><![CDATA[Stories by Alexander Arendar on Medium]]></description>
        <link>https://medium.com/@alexander.arendar?source=rss-830a218289b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*5eH7PB9CYHHzKKdH.jpg</url>
            <title>Stories by Alexander Arendar on Medium</title>
            <link>https://medium.com/@alexander.arendar?source=rss-830a218289b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 03:12:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexander.arendar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Inherited Widgets in Depth]]></title>
            <link>https://medium.com/swlh/inherited-widgets-in-depth-2151aa136f5c?source=rss-830a218289b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/2151aa136f5c</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Alexander Arendar]]></dc:creator>
            <pubDate>Wed, 24 Jul 2019 14:23:20 GMT</pubDate>
            <atom:updated>2019-07-25T13:27:20.104Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Al1EWQuLs13jY6TjAZx2fQ.jpeg" /><figcaption>Illustration by <a href="https://www.behance.net/danasavytska">Dana Savytska</a></figcaption></figure><p>Hardly Flutter could be a useful framework without <a href="https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html">InheritedWidget</a> class. This class is documented quite well but it is not a kind of widget that you put into you view and see some fancy UI immediately. So a lot of developers who just start learning the framework do not really pay a lot of attention to this class and what it brings. Let’s correct this and look a bit deeper than usual at the anatomy of this class and how it fits within the framework.</p><p>The first time when a developer usually gets an acquaintance with inherited widgets when learning Flutter is when one sees things like Theme.of(context) or MediaQuery.of(context). Both Theme and MediaQuery are inherited widgets. That gives an impression that inherited widgets are a convenient way to propagate some data from parent widgets to children widgets or grandchildren widgets or any descendant widgets. This impression is correct, but it is only a part of the story.</p><p>To illustrate this simplest use case, when some descendant widget needs to fetch data from direct or non-direct ancestor widget, let’s build a simple color provider.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ef93221389f3ec2718b816f8f5c1fdb/href">https://medium.com/media/7ef93221389f3ec2718b816f8f5c1fdb/href</a></iframe><p>InheritedWidget is an abstract class so we need to extend it and provide a missing implementation for the updateShouldNotify method. This method has nothing to do with the simplest use case we are looking at now, so let is just return <em>false</em> for now. Later we will analyze this method in details.</p><p>It is worth to mention that inheritance chain of the InheritedWidget class is <a href="https://api.flutter.dev/flutter/dart-core/Object-class.html"><em>Object</em></a> &gt; <a href="https://api.flutter.dev/flutter/foundation/Diagnosticable-class.html"><em>Diagnosticable</em></a> &gt; <a href="https://api.flutter.dev/flutter/foundation/DiagnosticableTree-class.html"><em>DiagnosticableTree</em></a> &gt; <a href="https://api.flutter.dev/flutter/widgets/Widget-class.html"><em>Widget</em></a> &gt; <a href="https://api.flutter.dev/flutter/widgets/ProxyWidget-class.html"><em>ProxyWidget</em></a> &gt; <em>InheritedWidget</em>. So inherited widget is a regular widget, though not an instance of stateful or stateless widget. In fact inherited widget is quite stateful, but not in the framework meaning of this word.</p><p>Also it is a subclass of a ProxyWidget which means it can have a child. In the code you can see that our color provider accepts a child in its constructor.</p><p>Below is an example of how to use this color provider:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee08b2ed364b4e1baae68b4859f3b349/href">https://medium.com/media/ee08b2ed364b4e1baae68b4859f3b349/href</a></iframe><p>As you can see you have the color provider almost up in the root of the widget tree, the child of our inherited widget ColorProvider is a MaterialApp, and deeper in the widgets tree a <em>Text</em> widget fetches color for its style attribute from this inherited widget. All works like it should:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*yVaentld3qHG7hmtu5Ye-Q.jpeg" /></figure><p>Now, how exactly the data is “inherited” and “propagated” from the top-level inherited widget to any location in our widgets tree below this inherited widget? And what exactly happens under the hood when ColorProvider.of(context).data gets executed?</p><p>BuildContext (which is available for any widget) declares this abstract method: InheritedWidget inheritFromWidgetOfExactType(Type targetType, { Object aspect });</p><p>So any widget can access an inherited widget of a particular type which encloses its context. This is why we can get a color from our color provider anywhere in the underlying part of our widgets tree.</p><p>Let’s see an implementation of this method which is provided in the <em>framework.dart</em> in the Element class (part of the Flutter):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7140a5b86451acfe108d5e55dddb3a78/href">https://medium.com/media/7140a5b86451acfe108d5e55dddb3a78/href</a></iframe><p>If you look what _inheritedWidgets is in this method then you will see it is a map: Map&lt;Type, InheritedElement&gt;.</p><p>We finally start to see what is going on. As it turns out <strong>every</strong> widget has internally such map were all available in its context inherited widgets are indexed by their type. If I put a break point there, start my app in the debug and examine the content of this map I can indeed see my ColorProvider among other things there:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZQKvZpE2_1YcRYLdqhMyeg.png" /></figure><p>If we dig a bit further into this method implementation we will see that after it gets an instance of the needed inherited widget from this map it then calls the inheritFromElement method and passes this instance there:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc146a0c635a859bdf3c822aa6296b04/href">https://medium.com/media/dc146a0c635a859bdf3c822aa6296b04/href</a></iframe><p>From this we can see that every widget internally maintains also a set of all inherited widgets that it depends on. This is all done implicitly for you as a developer: every time you use this pattern to access some inherited widget from your widget with help of static method <em>of</em>, it adds this inherited widget to the internal set _dependencies. This is logical, if you access some inherited widget then most likely you will use the data provided by this widget to build your own widgets. So you depend on this inherited widget and most likely you would like this dependency to be active and if the inherited widget changes that data you would like your widgets to rebuild. We will see later how it is implemented.</p><p><strong><em>Conclusion: every widget internally has a map of all available in its context inherited widgets indexed by their type. Also it has a set of all inherited widgets that it ever accessed and which are considered as dependencies.</em></strong></p><p>Let’s also look how this _inheritedWidgets map is constructed so at the time when your widget needs all available inherited widgets are already there. One of the basic methods in every widget’s element lifecycle is mount. If we look at its implementation in the base class <em>Element</em> we see following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9bed2de7ebe422e1a8a0857413b6791/href">https://medium.com/media/b9bed2de7ebe422e1a8a0857413b6791/href</a></iframe><p>Notice the _updateInheritance(); line there. Element class provides the following implementation for it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c18cefbcc79854ac952c465c3aa54da2/href">https://medium.com/media/c18cefbcc79854ac952c465c3aa54da2/href</a></iframe><p>So we see that any regular widget just copies the _inheritedWidgets map from its parent when it has one.</p><p>But InheritedWidget has it own element InheritedElement which overrides this behavior:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45ed8ca46845729500067415afbe493a/href">https://medium.com/media/45ed8ca46845729500067415afbe493a/href</a></iframe><p>In short, it also copies the _inheritedWidgets map from parent but also puts that particular inherited widget into this map.</p><p><strong><em>Conclusion: when framework mounts elements tree for our app it adds an entry to the map of inherited widgets every time a new inherited widget is mounter. All other widgets get the copy of this map. In the end of the process, effectively every widget has the map of all ancestor inherited widgets. Except for this it has a set of all inherited widgets it depends on.</em></strong></p><p>Now we have to look at <em>bool updateShouldNotify(covariant </em><a href="https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html"><em>InheritedWidget</em></a><em> oldWidget)</em> method of the <em>InheritedWidget</em> class. Documentation tells following:</p><blockquote>Whether the framework should notify widgets that inherit from this widget.</blockquote><blockquote>When this widget is rebuilt, sometimes we need to rebuild the widgets that inherit from this widget but sometimes we do not. For example, if the data held by this widget is the same as the data held by oldWidget, then we do not need to rebuild the widgets that inherited the data held by oldWidget.</blockquote><p>To illustrate this let’s evolve our color provider use case. Suppose we now need ability to have a color picker in our UI and when a user picks up a new color we would like the color provider to change its data and dependent on it children update their UI accordingly. So we may construct this ColorPicker stateful widget:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a6fa65b8272da154d8b02bbfdb6183f/href">https://medium.com/media/0a6fa65b8272da154d8b02bbfdb6183f/href</a></iframe><p>So this widget accepts a <em>child</em> argument, adds a UI layer to enable color selection and then embeds an already available inherited widget ColorProvider with that child below.</p><p>In our UI it is used as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f6fd7175d1960b83ba137604059e8c8/href">https://medium.com/media/7f6fd7175d1960b83ba137604059e8c8/href</a></iframe><p>So looks like everything is fine and I have my color picker working:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*GPCSWKD93IBONbzeA9RC3Q.jpeg" /></figure><p>Not really. When I try to select the green color in the color picker the text below does not update its color:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*r1FX4TndkNmccp0oD1w1Rw.jpeg" /></figure><p>That’s bad. We need to replace the old implementation of the updateShouldNotify method in our ColorProvider to this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2df412babb10cb549a6136f990fd8d8/href">https://medium.com/media/e2df412babb10cb549a6136f990fd8d8/href</a></iframe><p>This method get called by Flutter framework every time the inherited widget gets rebuild and gives you a chance to decide whether you would like widgets below this inherited widget and depending on its data to be rebuilt or not.<br>Now our color picker works appropriately:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*nn7WQXVlwaG-GF4-sVZ5pA.jpeg" /></figure><p><strong>Conclusion: every inherited widget needs to provide an implementation of the </strong><strong>updateShouldNotify method which decides whether dependent on it children will rebuild when the inherited widget’s data changes. It is needed in a more dynamic use cases when the inherited widget may be rebuilt by its parent with a new data.</strong></p><p>Now is time to look at one more interesting method of every <strong>stateful</strong> widget — didChangeDependencies().<br>Documentation tells us following:</p><blockquote>Called when a dependency of this <a href="https://api.flutter.dev/flutter/widgets/State-class.html">State</a> object changes.</blockquote><blockquote>For example, if the previous call to <a href="https://api.flutter.dev/flutter/widgets/State/build.html">build</a> referenced an <a href="https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html">InheritedWidget</a> that later changed, the framework would call this method to notify this object about the change.</blockquote><blockquote>This method is also called immediately after <a href="https://api.flutter.dev/flutter/widgets/State/initState.html">initState</a>. It is safe to call <a href="https://api.flutter.dev/flutter/widgets/BuildContext/inheritFromWidgetOfExactType.html">BuildContext.inheritFromWidgetOfExactType</a> from this method.</blockquote><blockquote>Subclasses rarely override this method because the framework always calls <a href="https://api.flutter.dev/flutter/widgets/State/build.html">build</a> after a dependency changes. Some subclasses do override this method because they need to do some expensive work (e.g., network fetches) when their dependencies change, and that work would be too expensive to do for every build.</blockquote><p>Let’s evolve our example a bit further. Suppose we now want to have our colored text blocks to also have a colored border, red border in case when the text color is green, and vice versa. Also suppose that every time user changes the color then we need to save this in some imaginary user preferences at backend side. If we knew nothing about the didChangeDependencies() method we could of course do this stuff inside our build() method, add some if-s there and it would work. But that would not be efficient and performance poor since our build() methods should be as slim and fast as possible in order to allow max FPS for our UI.</p><p>So instead we transform our colored text into a stateful widget, which keeps the color of text and border as state variables:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/225b44ccd4fd5a410e00c3ec2cefa429/href">https://medium.com/media/225b44ccd4fd5a410e00c3ec2cefa429/href</a></iframe><p>Notice the _saveColorStateInPreferences() method which emulates the expensive call to BE.</p><p>The widget behaves as it should and it is efficient from the performance point of view:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*dtaG76gF3W3aTOfmeT2ocQ.jpeg" /></figure><p>Now let’s put a break point inside our didChangeDependencies() method and restart the app in debug and look at the stack trace. First time the break point triggers on the app startup and we see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*dOnHumG-aQJjsHZOqLVI4Q.png" /></figure><p>This proves that didChangeDependencies() is called by framework at the first widget build so you have a chance to initialize your dependency on inherited widgets data.<br>If you let the app to continue running and change the color in the color picker then the breakpoint triggers again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*PmAYy_cf9CUREbiOKS1SHQ.png" /></figure><p>This time it is a more interesting case when framework calls this method ever time the inherited widget changes and its updateShouldNotify() method returns <em>true</em>.</p><p>To complete our understanding of how this all works inside let’s investigate the InheritedElement.notifyClients and <em>InheritedElement.notifyDependent </em>methods listed in the last stack trace:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fdc4e39ccb95f4ef3556d174f3cff26/href">https://medium.com/media/2fdc4e39ccb95f4ef3556d174f3cff26/href</a></iframe><p>This is quite straightforward if we strip out all checks: inherited widget maintains the map of all children who implicitly declared dependency on it by calling BuildContext.inheritFromWidgetOfExactType method. It is similar to how we have already seen earlier that every widget maintains a map of all inherited widgets it depends on. And for every such child the inherited widget calls the notifyDependent method which in turn calls the didChangeDependendies() on that child.</p><p><strong>Conclusion: every inherited widget maintains a map of dependent on it stateful children widgets. Every time the inherited widget rebuilds and changes its data framework calls the </strong><strong>didChangeDependecies() on every child found in that map.</strong></p><p>This is of course not all the details about <em>InheritedWidget</em>, there are more features and more interesting code in related framework parts but I hope this overview helped you to understand how inherited widget works and what is their potential. If you find any mistakes in my examples or I’ve missed something important — feel free to comment. All sources are available in this <a href="https://github.com/AlexanderArendar/inherited_research">GitHub repo</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2151aa136f5c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/inherited-widgets-in-depth-2151aa136f5c">Inherited Widgets in Depth</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dragging, zooming and placing object on indoor map with Flutter]]></title>
            <link>https://medium.com/@alexander.arendar/dragging-zooming-and-placing-object-on-indoor-map-with-flutter-67667ef415ec?source=rss-830a218289b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/67667ef415ec</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Alexander Arendar]]></dc:creator>
            <pubDate>Mon, 01 Jul 2019 23:56:43 GMT</pubDate>
            <atom:updated>2019-07-02T00:03:15.153Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve decided to implement a PoC for one feature which we probably will need in our product. Suppose that a user wants to upload to the application a drawing/plan of a building floor or some expo center and being able to navigate, zoom and place some objects on that indoor map. Also user would like those objects on the map to be clickable so the app can show some additional info/actions/etc. in case the user taps on the object.</p><p>One more thing which motivated me to spent time on this PoC was this <a href="https://stackoverflow.com/questions/50935470/flutter-create-infinite-screen">question on Stackoverflow</a> which had no answers at the time when I’ve encountered it.</p><p>The UI of the app looks like following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*w5w4kmJisTe7buuNx3B4Ug.jpeg" /></figure><p>I’ve tried to google if there is already something available for this in Flutter but didn’t find anything (probably my poor googling skills), so I’ve started to think and try what I can use to code it myself. I must admit that I am quite new to Flutter/Dart so the approach I describe below probably is not optimal.<br>Full code is available in <a href="https://github.com/AlexanderArendar/overflow">GitHub repo</a>.</p><p>First I’ve tried to use <a href="https://api.flutter.dev/flutter/widgets/OverflowBox-class.html">OverflowBox</a>, <a href="https://api.flutter.dev/flutter/widgets/SizedOverflowBox-class.html">SizedOverflowBox</a>, <a href="https://api.flutter.dev/flutter/widgets/ClipRect-class.html">ClipRect</a>, <a href="https://api.flutter.dev/flutter/widgets/Image-class.html">Image</a> and their different combinations but it was not ideal in different aspects. In the end I’ve stopped on using <a href="https://api.flutter.dev/flutter/dart-ui/Canvas-class.html">Canvas</a>, <a href="https://api.flutter.dev/flutter/rendering/CustomPainter-class.html">CustomPainter</a>, <a href="https://api.flutter.dev/flutter/dart-ui/Canvas/drawImageRect.html">drawImageRect</a> and <a href="https://api.flutter.dev/flutter/widgets/Stack-class.html">Stack</a> with <a href="https://api.flutter.dev/flutter/widgets/Positioned-class.html">Positioned</a>. Now let’s dig in.<br>My wish was to be able to use arbitrary <em>Widget</em>-s as objects I can place onto the map. So the abstraction to represent objects on the map is a following simple class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96bbc0598e06e3cca05bb80100acbc60/href">https://medium.com/media/96bbc0598e06e3cca05bb80100acbc60/href</a></iframe><p>It is convenient to use standard <a href="https://api.flutter.dev/flutter/dart-ui/Offset-class.html">Offset</a> class for object position on the map/drawing. <em>Offset(0, 0)</em> represents center of the map, while e.g. <em>Offset(-1, -1)</em> represents top left corner of the map.</p><p>Also standard <a href="https://api.flutter.dev/flutter/dart-ui/Size-class.html">Size</a> class is used to represent size of the object for <em>zoomLevel==1</em>. I want objects on the map to update their size when user zooms in/out.</p><p>The main widget which actually performs all calculation for positioning and rendering is called <em>ImageViewport</em>. It is a stateful widget and you can feel it’s design and purpose from the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea4383f1cec0e119ff02d54db35506b7/href">https://medium.com/media/ea4383f1cec0e119ff02d54db35506b7/href</a></iframe><p>So this widget accepts a zoom level, a map/drawing source as <a href="https://api.flutter.dev/flutter/painting/ImageProvider-class.html">ImageProvider</a> (it is convenient since it allows later to use an image from assets, file system or network) and a list of <em>MapObject</em>-s as parameters. The latter is needed if you have stored in your state set of objects on the map and want to load it and render later.</p><p>This <em>ImageViewport</em> is wrapped into another widget, <em>ZoomContainer</em> which places two icons atop of everything else and allows user to increase/decrease <em>zoomLevel</em> and rebuilds the underlying <em>ImageProvider</em> when <em>zoomLevel</em> changes. The whole code you can find in <a href="https://github.com/AlexanderArendar/overflow/blob/master/lib/main.dart">GitHub</a> and below is just a <em>build()</em> method of the <em>ZoomContainer</em> so you can get the idea:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7a807817e083ed01a5e831ee7ac24b0/href">https://medium.com/media/a7a807817e083ed01a5e831ee7ac24b0/href</a></iframe><p>Corresponding <em>ImageViewportState</em> is quite heavy on the code and you can view it in <a href="https://github.com/AlexanderArendar/overflow/blob/master/lib/main.dart">GitHub</a>. Here we will go through main conceptual pieces. First of all I use <a href="https://api.flutter.dev/flutter/widgets/LayoutBuilder-class.html">LayoutBuilder</a> at the top of widget hierarchy to be able to fit the map properly in the available space and react to events like device orientation change:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fca8473c0a6ff7e06424dd54620454de/href">https://medium.com/media/fca8473c0a6ff7e06424dd54620454de/href</a></iframe><p>Important thing to notice is that below the <em>LayoutBuilder</em> there is a <em>GestureDetector</em> to handle drag/pan events and to allow user to place new map object on the map by long press. It is worth to mention that when the map fits on the screen in one dimension, e.g. if it is long horizontally and zoom level is low, then only handling pan event will result in poor drag experience. So the app is smart and handles such cases by dynamically adding/removing <em>onPanUpdate</em>, <em>onHorizontalDragUpdate</em> and <em>onVerticalDragUpdate</em> handlers. <br>Inside the <em>GestureDetector</em> we have a <em>Stack</em>. At the bottom of the <em>Stack</em> we have <a href="https://api.flutter.dev/flutter/widgets/CustomPaint-class.html">CustomPaint</a> with a custom painter which actually renders corresponding to current <em>zoomLevel, </em>centerOffset, <em>devicePixelRatio</em> and underlying map/drawing bitmap <em>image.widht/height</em> piece of the original bitmap in our <em>ImageProvider</em>. Atop of this in our <em>Stack</em> goes a set of <em>Positioned</em> widgets each of which renders one <em>MapObject</em> from the <em>buildObjects()</em> invocation result.<br>For simplicity I’ve decided to have new map objects as a colored <em>Container</em> widgets with particular size. But it may be any <em>Widge</em>t as you can see from the code.</p><p>This <em>buildObjects()</em> function maps our collection <em>List&lt;MapObject&gt;</em> of object with relative offsets as coordinates to a list of <em>Positioned</em> widgets with actual absolute coordinates + it also wraps each <em>MapObject</em>’s child widget in a <em>Container</em> with proper size. Take a note that it wraps each in <em>GestureDetector</em> as well so the user can tap on the map object and see some reaction, in my case just a simple widget which renders some alert text in the proper position and allows user to close it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77a90b0f84f3e920ca719435d0b02fc2/href">https://medium.com/media/77a90b0f84f3e920ca719435d0b02fc2/href</a></iframe><p>Now our <em>CustomPainter</em> which renders needed part of the underlying bitmap image to our viewport. Its main workhorse is <em>Canvas.drawImageRect()</em> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98fef8bb5fe66f4fb13e31e6c6cd4a84/href">https://medium.com/media/98fef8bb5fe66f4fb13e31e6c6cd4a84/href</a></iframe><p>Interesting things are also calculations how to convert relative offsets for map objects to local coordinates and vice versa, how to track offset of the map center when user drags the map, how to update this center offset when the zoom level changes ,etc. Involved methods in the ImageViewportState are <em>_globaltoLocalOffset()</em>, <em>_localToGlobalOffset()</em>, <em>handleDrag()</em>. There’s nothing very complex there, but it’s too beefy to include here so you can look in the <a href="https://github.com/AlexanderArendar/overflow/blob/master/lib/main.dart">GitHub repo</a> and play with it yourself.</p><p>In the end I’ve come up with a proof of concept widget which allows me to embed it easily in my app, pass it default zoom level and a collection of map objects as you can see below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/defe6c414e98b5dae78d8ecf7db009d7/href">https://medium.com/media/defe6c414e98b5dae78d8ecf7db009d7/href</a></iframe><p>One more interesting thing to pay attention to is how to initialize the bitmap data from the <em>ImageProvider</em>. It is in the <em>didChangeDependencies()</em> method of the <em>_ImageViewportState</em> class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09d2642c60d3b35a61de0cfd2e4506a7/href">https://medium.com/media/09d2642c60d3b35a61de0cfd2e4506a7/href</a></iframe><p>This exercise proved me that it is possible to implement such features as “Indoor Maps” in Flutter. Of course in order to evolve it into some real production component a lot of things need to be done but it is clear what to use.<br>Hope this reading was useful for you. If you have any notes/suggestions/questions please comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67667ef415ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>