<?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 Mikiyas Birhanu on Medium]]></title>
        <description><![CDATA[Stories by Mikiyas Birhanu on Medium]]></description>
        <link>https://medium.com/@mkbirhanu?source=rss-652b31e16cab------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3dlhKUaU3vH4mKxPvT988g.jpeg</url>
            <title>Stories by Mikiyas Birhanu on Medium</title>
            <link>https://medium.com/@mkbirhanu?source=rss-652b31e16cab------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:42:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mkbirhanu/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[Simplifying Flutter App Development with Common Loading Skeleton]]></title>
            <link>https://medium.com/@mkbirhanu/simplifying-flutter-app-development-with-common-loading-skeleton-62e5579f7774?source=rss-652b31e16cab------2</link>
            <guid isPermaLink="false">https://medium.com/p/62e5579f7774</guid>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[flutter-widget]]></category>
            <category><![CDATA[flutter-ui-templates]]></category>
            <dc:creator><![CDATA[Mikiyas Birhanu]]></dc:creator>
            <pubDate>Tue, 12 Dec 2023 13:48:49 GMT</pubDate>
            <atom:updated>2023-12-12T13:48:49.338Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Flutter, known for its expressive UI and fast development, becomes even more powerful when you streamline common patterns across your app. In this article, we’ll explore how to simplify Flutter app development by implementing a common loading skeleton and error handling wrapper for pages.</p><h3>The Challenge</h3><p>Developers often find themselves repeating code to handle loading states, error messages, and success scenarios across different pages in their Flutter applications. This redundancy can make the codebase harder to maintain and lead to inconsistencies in the user experience.</p><h3>The Solution</h3><p>To address this challenge, we’ve created a Flutter project that provides a common loading skeleton and error handling wrapper, making it easy to implement consistent user interfaces throughout your app. The focal point of this solution is the PageWrapper widget, a versatile tool that handles loading, error, and success states seamlessly.</p><h3>PageWrapper in Action</h3><h3>What is PageWrapper?</h3><p>PageWrapper is a reusable widget designed to simplify the implementation of loading and error states in Flutter pages. It encapsulates common logic, providing a clean and maintainable structure for your app.</p><pre>// page_wrapper.dart<br><br>import &#39;package:common_loading_skeleton/src/widgets/common/awesome_loading.dart&#39;;<br>import &#39;package:common_loading_skeleton/src/widgets/common/buttons/primary_button.dart&#39;;<br>import &#39;package:flutter/material.dart&#39;;<br><br>class PageWrapper extends StatefulWidget {<br>  final Future&lt;void&gt; Function() onInit;<br>  final Widget child;<br>  final String appBarTitle;<br>  final List&lt;Widget&gt;? appBarActions;<br>  final String? loadingMessage;<br>  final String? error;<br><br>  const PageWrapper({<br>    Key? key,<br>    required this.onInit,<br>    required this.child,<br>    required this.appBarTitle,<br>    this.appBarActions,<br>    this.loadingMessage,<br>    this.error,<br>  }) : super(key: key);<br><br>  @override<br>  _PageWrapperState createState() =&gt; _PageWrapperState();<br>}<br><br>class _PageWrapperState extends State&lt;PageWrapper&gt; {<br>  late Future&lt;void&gt; _initFuture;<br>  ScrollController scrollController = ScrollController();<br><br>  @override<br>  void initState() {<br>    super.initState();<br>    _initFuture = widget.onInit();<br>  }<br><br>  // Retry the initialization logic<br>  Future&lt;void&gt; onTryAgain() async {<br>    setState(() {<br>      _initFuture = widget.onInit();<br>    });<br>  }<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return Scaffold(<br>      appBar: AppBar(<br>        title: Text(widget.appBarTitle),<br>        actions: widget.appBarActions,<br>      ),<br>      body: SafeArea(<br>        child: SingleChildScrollView(<br>          controller: scrollController,<br>          child: Padding(<br>            padding: const EdgeInsets.all(8.0),<br>            child: FutureBuilder&lt;void&gt;(<br>              future: _initFuture,<br>              builder: (context, snapshot) {<br>                if (snapshot.connectionState == ConnectionState.waiting) {<br>                  // Loading state<br>                  return _buildLoading();<br>                } else if (snapshot.hasError || widget.error != null) {<br>                  // Error state<br>                  return _buildError(widget.error ?? &quot;Something went wrong&quot;);<br>                } else {<br>                  // Success state<br>                  return widget.child;<br>                }<br>              },<br>            ),<br>          ),<br>        ),<br>      ),<br>    );<br>  }<br><br>  // Widget for displaying loading state<br>  Widget _buildLoading() {<br>    return Container(<br>      alignment: Alignment.center,<br>      child: AwesomeLoading(<br>        loadingMessage: widget.loadingMessage,<br>      ),<br>    );<br>  }<br><br>  // Widget for displaying error state<br>  Widget _buildError(String message) {<br>    return Center(<br>      child: Column(<br>        mainAxisAlignment: MainAxisAlignment.center,<br>        children: [<br>          Text(message),<br>          const SizedBox(height: 16),<br>          PrimaryButton(onClick: onTryAgain, label: &quot;Try Again&quot;),<br>        ],<br>      ),<br>    );<br>  }<br>}</pre><h3>How to Use PageWrapper</h3><p>Using PageWrapper is straightforward. Wrap your page content with it, and provide an onInit function that represents the common logic for your page (e.g., API calls). PageWrapper takes care of displaying loading spinners and handling errors, allowing you to focus on building your app&#39;s features.</p><p>Here’s a basic example:</p><pre>// sample_item_list_view.dart<br><br>import &#39;package:common_loading_skeleton/src/services/sample_item_service.dart&#39;;<br>import &#39;package:common_loading_skeleton/src/wrappers/page_wrapper.dart&#39;;<br>import &#39;package:flutter/material.dart&#39;;<br><br>import &#39;../../models/sample_item.dart&#39;;<br>import &#39;../settings_view.dart&#39;;<br>import &#39;sample_item_details_view.dart&#39;;<br><br>class SampleItemListView extends StatefulWidget {<br>  const SampleItemListView({Key? key}) : super(key: key);<br><br>  static const routeName = &#39;/&#39;;<br><br>  @override<br>  State&lt;SampleItemListView&gt; createState() =&gt; _SampleItemListViewState();<br>}<br><br>class _SampleItemListViewState extends State&lt;SampleItemListView&gt; {<br>  late List&lt;SampleItem&gt; items;<br>  final SampleItemService sampleItemService = SampleItemService();<br>  late String? pageError;<br><br>  @override<br>  void initState() {<br>    super.initState();<br>    pageError = null;<br>    items = [];<br>    _fetchItems();<br>  }<br><br>  // Fetch items and handle errors<br>  Future&lt;void&gt; _fetchItems() async {<br>    try {<br><br>       await Future.delayed(const Duration(seconds: 10));<br><br>      final fetchedItems = await sampleItemService.getItems();<br>      setState(() {<br>        items = fetchedItems;<br>        pageError = null;<br>      });<br>    } catch (error) {<br>      print(error);<br>      setState(() {<br>        pageError = &quot;Failed to fetch items. Please try again.&quot;;<br>      });<br>    }<br>  }<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return PageWrapper(<br>        onInit: () async {<br>          // Common logic, such as API calls<br>          await _fetchItems();<br>        },<br>        appBarTitle: &#39;Sample Items&#39;,<br>        loadingMessage: &quot;Loading Items Please Wait&quot;,<br>        appBarActions: [<br>          IconButton(<br>            icon: const Icon(Icons.settings),<br>            onPressed: () {<br>              Navigator.restorablePushNamed(context, SettingsView.routeName);<br>            },<br>          ),<br>        ],<br>        error: pageError,<br>        child: ListView.builder(<br>          shrinkWrap: true,<br>          restorationId: &#39;sampleItemListView&#39;,<br>          itemCount: items.length,<br>          itemBuilder: (BuildContext context, int index) {<br>            final item = items[index];<br>            int order = index + 1;<br>            String iconText = order.toString();<br>            return ListTile(<br>              title: Text(item.title),<br>              leading: Container(<br>                padding: const EdgeInsets.all(8),<br>                decoration: BoxDecoration(<br>                  shape: BoxShape.circle,<br>                  color: Theme.of(context).primaryColor,<br>                ),<br>                child: Text(<br>                  iconText,<br>                  style: const TextStyle(color: Colors.white),<br>                ),<br>              ),<br>              onTap: () {<br>                Navigator.restorablePushNamed(<br>                  context,<br>                  SampleItemDetailsView.routeName,<br>                  arguments: item.id,<br>                );<br>              },<br>            );<br>          },<br>        ));<br>  }<br>}</pre><h3>Usage Guidelines</h3><ul><li>Utilize the PageWrapper widget to handle loading, error, and success states in your pages.</li><li>Customize the loading and error UI based on your app’s design guidelines.</li><li>Modify the SampleItemService class to fetch data from your desired source.</li></ul><h3>Contributions</h3><p>Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.</p><h3>Conclusion</h3><p>Simplifying Flutter app development is all about creating reusable components that adhere to best practices. By implementing a common loading skeleton and error handling wrapper, you can ensure a consistent and delightful user experience across your entire app.</p><p>To explore the implementation details, check out the <a href="https://github.com/mikerashoo/flutter-common-page-wrapper">GitHub repository</a>.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=62e5579f7774" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>