<?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 limboy on Medium]]></title>
        <description><![CDATA[Stories by limboy on Medium]]></description>
        <link>https://medium.com/@limboy?source=rss-f95f863c81e4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GUmZqU9RxWhl9tFCufuyJg.png</url>
            <title>Stories by limboy on Medium</title>
            <link>https://medium.com/@limboy?source=rss-f95f863c81e4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 12:15:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@limboy/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[Flutter Layout in a nutshell]]></title>
            <link>https://medium.com/@limboy/flutter-layout-in-a-nutshell-f2ed3cb66d72?source=rss-f95f863c81e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2ed3cb66d72</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[layout]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flexbox]]></category>
            <dc:creator><![CDATA[limboy]]></dc:creator>
            <pubDate>Thu, 10 Jan 2019 17:23:21 GMT</pubDate>
            <atom:updated>2019-01-10T17:23:21.413Z</atom:updated>
            <content:encoded><![CDATA[<p>After struggle with Flutter’s Layout System for a while, it seems i have passed the aha moment, so let’s talk about it.</p><h3>Key Concepts</h3><p>there are some key concepts in Flutter’s Layout System. knowing them is essential to help understanding layout.</p><h4>Unbounded Constraints</h4><blockquote>either the maximum width or the maximum height is set to double.INFINITY</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*awh-Ik4CLGQKH_aGlB_MSQ.png" /><figcaption>ScrollView and its descendant</figcaption></figure><p>ScrollView and its descendant like ListView or GridView are most seen, other widgets that can set width or height to double.INFINITY also counts. sometimes they are called <strong>as big as possible.</strong></p><h4>Flex</h4><blockquote>when in bounded constraints, try to be as big as possible in that direction.</blockquote><blockquote>when in unbounded constraints, try to fit their children in that direction.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*OAmZGeFre8gDtZjzVP1eDw.png" /><figcaption>Row and Column</figcaption></figure><p>Row and Column are Flex instances, if you want more control, Flex widget is also available. they can be used with Flexible widgets, but not restricted to. (a Text widget in Column is totally fine.)</p><h4>Flexible</h4><p>Use it within Flex. Flexible declares what percent space to use.</p><p>i.e. flex = 1 means 1/all, if all = 1 + 1, then it’s 50%.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*CdjDM6FQQSoxcGZPDj756A.png" /><figcaption>Expanded</figcaption></figure><p>Expanded is most seen as a Flexible instance, it will fill the available space in the main axis. (i.e. horizontally for Row or vertically for a Column)</p><h3>Key Widgets</h3><h4>Container</h4><blockquote>Containers with no children try to be as big as possible unless the incoming constraints are unbounded, in which case they try to be as small as possible. Containers with children size themselves to their children. The width, height, and <a href="https://docs.flutter.io/flutter/widgets/Container/constraints.html">constraints</a> arguments to the constructor override this.</blockquote><pre>return MaterialApp(<br>  home: Scaffold(<br>    body: Container(<br>      color: Colors.yellow,<br>    ),<br>  ),<br>);</pre><p>a container without child will take up all the available space like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*2oHDHIgFk-B6KmjOIjIoJw.png" /></figure><p>if width or/and height is set, it will adhere.</p><pre>return MaterialApp(<br>  home: Scaffold(<br>    body: Container(<br>      color: Colors.yellow,<br>      width: 100,<br>      height: 100,<br>    ),<br>  ),<br>);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*iUOSCYwYKoc0T1AWUMFq7w.png" /></figure><p>if it has a Text child, it will respect its size, children always comes first.</p><pre>return MaterialApp(<br>  home: Scaffold(<br>    body: Container(<br>      color: Colors.yellow,<br>      child: Text(&#39;hello&#39;),<br>    ),<br>  ),<br>);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*OGaA4YiUBfW-_mIBrWGAMA.png" /></figure><p>besides these, it can align children, make padding / margin, etc. quite useful.</p><h4>Stack</h4><p>Stack is like css’s absolute layout. you can overlap several children in a simple way, for example having some text and an image.</p><blockquote>Each child of a <a href="https://docs.flutter.io/flutter/widgets/Stack-class.html">Stack</a> widget is either <em>positioned</em> or <em>non-positioned</em>. Positioned children are those wrapped in a <a href="https://docs.flutter.io/flutter/widgets/Positioned-class.html">Positioned</a> widget that has at least one non-null property. The stack sizes itself to contain all the non-positioned children, which are positioned according to <a href="https://docs.flutter.io/flutter/widgets/Stack/alignment.html">alignment</a>.</blockquote><p>so if a child is not positioned, Stack will use fit and alignment to put it to a suitable position.</p><pre>Stack(<br>  fit: StackFit.loose,<br>  alignment: Alignment.center,<br>  children: &lt;Widget&gt;[<br>    Text(&#39;world&#39;),<br>    Positioned(<br>     bottom: 10,<br>     child: Text(&#39;hello&#39;),<br>   )<br> ],<br>),</pre><p>what StackFit.loose saying is if child is not larger than me, use your size. that’s what it looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*g7Y5-mQiPPYushr_N9i3gQ.png" /></figure><p>meanwhile StackFit.expand will put all non positioned children to stack’s size. that’s what it looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*MFpUVNZXlRmhVa8A0LrXIg.png" /></figure><p>Text(‘world’) is now as large as stack, so alignment.center seems no effect.</p><h4>Row and Column</h4><p>they are Flex instances, which can put children vertically or horizontally.</p><p>crossAxisAlignment means how to align the other side. if it’s Row how to align vertically, if it’s Column how to align horizontally.</p><p>mainAxisSize by default its MainAxisSize.max, if you want it to be just the Row’s width or Column’s height, set it to MainAxisSize.min.</p><h4>SizedBox</h4><p>use SizedBox to get fixed size box.</p><h4>SafeArea</h4><p>use SafeArea will indent the child by enough to avoid the status bar at the top of the screen(get rid of iPhoneX’s Notch).</p><h3>Principles</h3><h4>Don’t put unbounded constraints widget within Flex</h4><p>Column is Flex, if you put a ListView in it like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*pjoZ8oTju1c8DgwSKtLenA.png" /></figure><p>you will get layout errors</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1KwFf2_DgBxy9L8o0B39qg.png" /></figure><p>because Column which is a Flex instance don’t know how to put an <strong>as big as possible</strong> widget in it. the fix is simple, just set ListView shrinkWrap = true. after doing so, ListView will shrink to min size. now Column know how to put it.</p><p>Expanded is usable within Column or Row, because it’s a Flexible, born to work with Flex widgets.</p><h4>Don’t nest unbounded constraints widgets</h4><p>i.e. a ListView in ListView</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*8ks2zQ6ldMIT6nCYBqanlA.png" /></figure><p>the error is like previous. only one component can be <strong>as big as possible </strong>in same direction. if you really want to combine 2 or more scrollviews, NestedScrollView may be helpful.</p><h4>Don’t set flex other than 0 in unbounded widgets</h4><p>because the space is unlimited, you can’t split it into like 1:2. if you do, this error is waiting for you:</p><blockquote>RenderFlex children have non-zero flex but incoming height constraints are unbounded.</blockquote><h3>Quiz</h3><p>will these code make Hello World wrapped in 100x100 square?</p><pre>return MaterialApp(<br>  home: Container(<br>    alignment: Alignment.center,<br>    constraints: BoxConstraints.tight(Size(100, 100)),<br>    decoration: BoxDecoration(color: Colors.yellow),<br>    child: Text(&#39;Hello World&#39;),<br>  ),<br>);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*YogmW7wjfuXqRmt19XHFRg.png" /></figure><p>The answer is No. it will behave like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/1*dw7vO_GrNlukc3H_ov_MHQ.png" /></figure><p>MaterialApp will make its first child stretch fo fit screen. so even constraints is given, after doing some calculation, system think your constraints should be ignored.</p><p>the right way to achieve the expected effect is to wrap a Text widget in a Center or Align widget.</p><h4>What if I want to get parent’s constraints?</h4><p>use LayoutBuilder widget to get parent’s constraints.</p><p>In certain situation it’s convenient to use’s parent’s constraints to help building child’s UI. Parent doesn’t have a size until the child has a size, all it has is constraints, i.e. maxWidth.</p><pre>// borrowed from <a href="https://stackoverflow.com/a/41558369/94962">https://stackoverflow.com/a/41558369/94962</a></pre><pre>var container = Container(<br>  // Toggling width from 100 to 300 will change what is rendered<br>  // in the child container<br>  width: 100.0,<br>  // width: 300.0<br>  child: LayoutBuilder(<br>    builder: (BuildContext context, BoxConstraints constraints) {<br>      if(constraints.maxWidth &gt; 200.0) {<br>        return Text(&#39;BIG&#39;);<br>      } else {<br>        return Text(&#39;SMALL&#39;);<br>      }<br>    }<br>  ),<br>);</pre><h4>How do I get screen’s size?</h4><p>use MediaQuery, besides size, it has other device info like devicePixelRatio.</p><p>So that’s what I think are important to understand flutter’s layout, hope it helps. if there are any mistakes welcome point out. :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2ed3cb66d72" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Architecture Flutter App the Bloc_Redux Way]]></title>
            <link>https://medium.com/@limboy/architecture-flutter-app-the-bloc-redux-way-47b09fe2959e?source=rss-f95f863c81e4------2</link>
            <guid isPermaLink="false">https://medium.com/p/47b09fe2959e</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[bloc]]></category>
            <dc:creator><![CDATA[limboy]]></dc:creator>
            <pubDate>Sat, 05 Jan 2019 12:11:08 GMT</pubDate>
            <atom:updated>2019-01-05T12:11:08.738Z</atom:updated>
            <content:encoded><![CDATA[<p>here’s the github link: <a href="https://github.com/lzyy/bloc_redux">https://github.com/lzyy/bloc_redux</a></p><p>After using flutter for some time, it seems there are no the go-to way to handle state and manage data flow. well, there are, one is <a href="https://github.com/brianegan/flutter_redux">flutter_redux</a>, and the other is <a href="https://github.com/felangel/bloc">flutter_bloc</a>. Let’s talk flutter_redux first.</p><p>It’s kind of the official implementation of redux. mainly combined with two parts: StoreProvider and StoreConnector<em>. </em>the former used to hold Store, the latter used to react to new state.</p><pre>// Every time the button is tapped, an action is dispatched and<br>// run through the reducer. After the reducer updates the state,<br>// the Widget will be automatically rebuilt with the latest<br>// count. No need to manually manage subscriptions or Streams!<br>new StoreConnector&lt;int, String&gt;(<br>  converter: (store) =&gt; store.state.toString(),<br>  builder: (context, count) {<br>    return new Text(<br>      count,<br>      style: Theme.of(context).textTheme.display1,<br>    );<br>  },<br>)</pre><p>the problem of StoreConnector is whenever state updated, it will be triggered. even the state contains 10 properties, and only 1 bool property is updated.</p><pre>// Creates the base [NextDispatcher].<br>//<br>// The base NextDispatcher will be called after all other middleware provided<br>// by the user have been run. Its job is simple: Run the current state through<br>// the reducer, save the result, and notify any subscribers.<br>NextDispatcher _createReduceAndNotify(bool distinct) {<br>  return (dynamic action) {<br>    final state = reducer(_state, action);</pre><pre>if (distinct &amp;&amp; state == _state) return;</pre><pre>_state = state;<br>    _changeController.add(state);<br>  };<br>}</pre><p>that’s the redux notify mechanism. as long as the state are not equal, _changeController is notified. in react-redux, there’s some optimization, using connect&#39;s mapStateToProps , the component will be rerender only when the props it care updated. but flutter_redux can’t do this.</p><p>now let’s dive into flutter_bloc, before talk about it, let’s talk about bloc, which is promoted by google fellows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*WJMUsjhRqwj40NQ7ovWJYw.png" /></figure><p>it’s more of an idea rather than spec or protocol. flutter_bloc is an implementation of bloc. after looking into the code, it seems not quite different than redux, but added some concepts like BlocSupervisor BlocDelegate Transaction , make it more complex. state handling is no different than redux, even worse, without == check.</p><pre>void _bindStateSubject() {<br>  Event currentEvent;</pre><pre>(transform(_eventSubject) as Observable&lt;Event&gt;).concatMap((Event event) {<br>    currentEvent = event;<br>    return mapEventToState(_stateSubject.value, event);<br>  }).forEach(<br>    (State nextState) {<br>      final transition = Transition(<br>        currentState: _stateSubject.value,<br>        event: currentEvent,<br>        nextState: nextState,<br>      );<br>      BlocSupervisor().delegate?.onTransition(transition);<br>      onTransition(transition);<br>      _stateSubject.add(nextState);<br>    },<br>  );<br>}</pre><p>nextState is directly add to _stateSubject without checking equal. meanwhile author seems to encourage using multi blocs which is not handy to use. (which bloc should i dispatch to?)</p><pre>return BlocBuilder&lt;LoginEvent, LoginState&gt;(<br>  bloc: widget.loginBloc,<br>  builder: (<br>    BuildContext context,<br>    LoginState loginState,<br>  ) {<br>    if (_loginSucceeded(loginState)) {<br>      widget.authBloc.dispatch(Login(token: loginState.token));<br>      widget.loginBloc.dispatch(LoggedIn());<br>    }<br>  }<br>);</pre><p>So, these two libraries are not suited for my needs, let’s make another wheel!</p><h4>Bloc_Redux</h4><p>the problem with flutter_redux is its state handling. reducer return a new state, and it’s hard to separate modified properties, even it can, there should be a map to hold these relationships, too complicated.</p><p>Flutter already provides StreamBuilder , let widgets listen the streams they like, when an action dispatched, change these streams content. that’s it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5kWI6Kg2sUj9QUTSapOWxQ.png" /></figure><p>the reducer’s role is replaced by bloc.</p><pre>/// Action<br>///<br>/// every action should extends this class<br>abstract class BRAction&lt;T&gt; {<br>  T payload;<br>}</pre><pre>/// State<br>///<br>/// Input are used to change state.<br>/// usually filled with StreamController / BehaviorSubject.<br>/// handled by blocs.<br>///<br>/// implements disposable because stream controllers needs to be disposed.<br>/// they will be called within store&#39;s dispose method.<br>abstract class BRStateInput implements Disposable {}</pre><pre>/// Output are streams.<br>/// followed by input. like someController.stream<br>/// UI will use it as data source.<br>abstract class BRStateOutput {}</pre><pre>/// State<br>///<br>/// Combine these two into one.<br>abstract class BRState&lt;T extends BRStateInput, U extends BRStateOutput&gt; {<br>  T input;<br>  U output;<br>}</pre><pre>/// Bloc<br>///<br>/// like reducers in redux, but don&#39;t return a new state.<br>/// when they found something needs to change, just update state&#39;s input<br>/// then state&#39;s output will change accordingly.<br>typedef Bloc&lt;T extends BRStateInput&gt; = void Function(BRAction action, T input);</pre><pre>/// Store<br>///<br>/// widget use `store.dispatch` to send action<br>/// store will iterate all blocs to handle this action<br>///<br>/// if this is an async action, blocs can dispatch another action<br>/// after data has received from remote.<br>abstract class BRStore&lt;T extends BRStateInput, U extends BRState&gt;<br>    implements Disposable {<br>  List&lt;Bloc&lt;T&gt;&gt; blocs;<br>  U state;</pre><pre>void dispatch(BRAction action) {<br>    blocs.forEach((f) =&gt; f(action, state.input));<br>  }</pre><pre>dispose() {<br>    state.input.dispose();<br>  }<br>}</pre><p>that’s the core code. State is separated into StateInput and StateOutput , StateInput is handled by bloc, StateOutput is consumed by widgets to receive latest data.</p><p>Store has a dispose method, because it will be added to StoreProvider, when it disposed, the streams can be closed safely. Let’s see a demo</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*tV3URLf2QDzJTzKP6m04mg.gif" /></figure><pre>/// Actions<br>class ColorActionSelect extends BRAction&lt;Color&gt; {}</pre><pre>/// State<br>class ColorStateInput extends BRStateInput {<br>  final BehaviorSubject&lt;Color&gt; selectedColor = BehaviorSubject();<br>  final BehaviorSubject&lt;List&lt;ColorModel&gt;&gt; colors = BehaviorSubject();</pre><pre>dispose() {<br>    selectedColor.close();<br>    colors.close();<br>  }<br>}</pre><pre>class ColorStateOutput extends BRStateOutput {<br>  StreamWithInitialData&lt;Color&gt; selectedColor;<br>  StreamWithInitialData&lt;List&lt;ColorModel&gt;&gt; colors;</pre><pre>ColorStateOutput(ColorStateInput input) {<br>    selectedColor = StreamWithInitialData(<br>        input.selectedColor.stream, input.selectedColor.value);<br>    colors = StreamWithInitialData(input.colors.stream, input.colors.value);<br>  }<br>}</pre><pre>class ColorState extends BRState&lt;ColorStateInput, ColorStateOutput&gt; {<br>  ColorState() {<br>    input = ColorStateInput();<br>    output = ColorStateOutput(input);<br>  }<br>}</pre><pre>/// Blocs<br>Bloc&lt;ColorStateInput&gt; colorSelectHandler = (action, input) {<br>  if (action is ColorActionSelect) {<br>    input.selectedColor.add(action.payload);<br>    var colors = input.colors.value<br>        .map((colorModel) =&gt; colorModel<br>          ..isSelected = colorModel.color.value == action.payload.value)<br>        .toList();<br>    input.colors.add(colors);<br>  }<br>};</pre><pre>/// Store<br>class ColorStore extends BRStore&lt;ColorStateInput, ColorState&gt; {<br>  ColorStore() {<br>    state = ColorState();<br>    blocs = [colorSelectHandler];</pre><pre>// init<br>    var _colors = List&lt;ColorModel&gt;.generate(<br>        30, (int index) =&gt; ColorModel(RandomColor(index).randomColor()));<br>    _colors[0].isSelected = true;<br>    state.input.colors.add(_colors);<br>    state.input.selectedColor.add(_colors[0].color);<br>  }<br>}</pre><p>Store is like brain, it receives actions and make blocs handle these actions to change streams accordingly. widgets will receive streams’ latest data.</p><pre>class ColorsWidget extends StatelessWidget {<br>  <a href="http://twitter.com/override">@override</a><br>  Widget build(BuildContext context) {<br>    final store = StoreProvider.of&lt;ColorStore&gt;(context);<br>    final colors = store.state.output.colors;</pre><pre>return StreamBuilder&lt;List&lt;ColorModel&gt;&gt;(<br>      stream: colors.stream,<br>      initialData: colors.initialData,<br>      builder: (context, snapshot) {<br>        final colors = snapshot.data;<br>        return SliverGrid.count(<br>            crossAxisCount: 6,<br>            children: colors.map((colorModel) {<br>              return GestureDetector(<br>                onTap: () {<br>                  store.dispatch(<br>                      ColorActionSelect()..payload = colorModel.color);<br>                },<br>                child: Container(<br>                  decoration: BoxDecoration(<br>                      color: colorModel.color,<br>                      border: Border.all(width: colorModel.isSelected ? 4 : 0)),<br>                ),<br>              );<br>            }).toList());<br>      },<br>    );<br>  }<br>}</pre><p>StreamBuilder is used to receive StateOutput stream’s value, and use store.dispatch to send action(one page one store). It’s that simple.</p><p>Source: <a href="https://github.com/lzyy/bloc_redux">https://github.com/lzyy/bloc_redux</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47b09fe2959e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>