<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rui.Z</title>
    <description>The latest articles on DEV Community by Rui.Z (@zhangrui).</description>
    <link>https://dev.to/zhangrui</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F641191%2F812f287a-1776-4e11-8765-cedadc761998.jpeg</url>
      <title>DEV Community: Rui.Z</title>
      <link>https://dev.to/zhangrui</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhangrui"/>
    <language>en</language>
    <item>
      <title>Use Floating Action Button in Flutter</title>
      <dc:creator>Rui.Z</dc:creator>
      <pubDate>Mon, 07 Jun 2021 19:48:21 +0000</pubDate>
      <link>https://dev.to/zhangrui/use-floating-action-button-in-flutter-2oj4</link>
      <guid>https://dev.to/zhangrui/use-floating-action-button-in-flutter-2oj4</guid>
      <description>&lt;p&gt;FloatingActionButton(FAB) is the widget to create a icon button that hovers over content to promote a quick action in a app screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pKaH__7M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgbob9hhpukrthhlk23x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pKaH__7M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgbob9hhpukrthhlk23x.png" alt="How FAB looks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Design guidance
&lt;/h2&gt;

&lt;p&gt;One screen should have at most one single floating action button. If you want to have more than one quick action, you may consider &lt;a href="https://flutter.dev/docs/cookbook/effects/expandable-fab"&gt;ExpandableFab&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Style
&lt;/h2&gt;

&lt;p&gt;A normal FAB can be constructed via FloatingActionButton(…) and a FAB with stadium shape can be constructed via FloatingActionButton.expanded(…) as the example below. You can add text label to the expanded one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Location
&lt;/h2&gt;

&lt;p&gt;The location of FAB is set on its parent Scaffold widget’s floatingActionButtonLocation property. If appBottomBar is used for a BottomNavigationBar, it can be set to one of the ‘docked’ location such as &lt;code&gt;floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cnl8XquT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9xkbcr9lgnexgjrr2if.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cnl8XquT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9xkbcr9lgnexgjrr2if.png" alt="Docked FAB"&gt;&lt;/a&gt;&lt;br&gt;
Refer &lt;a href="https://api.flutter.dev/flutter/material/FloatingActionButtonLocation-class.html"&gt;this&lt;/a&gt; for more location details.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tips
&lt;/h2&gt;

&lt;p&gt;If the FAB triggers an asynchronous action which may take a few seconds to process(such as login or getting device location), it would be good to have some animation to provide better user experience. I find using a variable ‘_isLoading’ to switch to a CircularProgressIndicator() provide a good feedback to user’s action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;…
floatingActionButton: Visibility(
    visible: _isButtonVisible,
    child: _isLoading
        ? const CircularProgressIndicator()
        : FloatingActionButton(
            onPressed: () {
                setState(() {
                    _isLoading = true;
                });
                // some actions
            },
            child: const Icon(Icons.navigation),
            backgroundColor: Colors.green,
          ),
),
…
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Code example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';

void main() =&amp;gt; runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'FAB Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatelessWidget(),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('FAB AppBar Label'),
      ),
      body: const Center(
        child: Text('Center Text'),
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () {
          // Action of the button
        },
        label: const Text('Submit'),
        icon: const Icon(Icons.thumb_up),
        backgroundColor: Colors.green,
      ),
    );
  }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>flutter</category>
    </item>
    <item>
      <title>Create Bottom Navigation Bar in Flutter </title>
      <dc:creator>Rui.Z</dc:creator>
      <pubDate>Sun, 06 Jun 2021 19:52:54 +0000</pubDate>
      <link>https://dev.to/zhangrui/create-bottom-navigation-bar-in-flutter-jl6</link>
      <guid>https://dev.to/zhangrui/create-bottom-navigation-bar-in-flutter-jl6</guid>
      <description>&lt;p&gt;To implement a navigation bar for user to navigate between views in Flutter, we can use a Material Widget called &lt;a href="https://api.flutter.dev/flutter/material/BottomNavigationBar-class.html" rel="noopener noreferrer"&gt;&lt;strong&gt;BottomNavigationBar&lt;/strong&gt;&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxi3i58e223x5g5m6bbwe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxi3i58e223x5g5m6bbwe.png" alt="BottomNavigationBar"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The BottomNavigationBar is usually used as tha argument of &lt;a href="https://api.flutter.dev/flutter/material/Scaffold/bottomNavigationBar.html" rel="noopener noreferrer"&gt;Scaffold.bottomNavigationBar&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Guidance
&lt;/h2&gt;

&lt;p&gt;Material design guidance suggests that BottomNavigationBar should have no more than 5 items. &lt;/p&gt;

&lt;h2&gt;
  
  
  Style
&lt;/h2&gt;

&lt;p&gt;The bottom navigation bar's type changes how its items are displayed. If not specified, then it's automatically set to &lt;strong&gt;&lt;em&gt;BottomNavigationBarType.fixed&lt;/em&gt;&lt;/strong&gt; when there are less than four items, and &lt;strong&gt;&lt;em&gt;BottomNavigationBarType.shifting&lt;/em&gt;&lt;/strong&gt; otherwise.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzcjitud3ja2nwqjlixs6.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzcjitud3ja2nwqjlixs6.jpeg" alt="BottomNavigationBarType.fixed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fri3epbwbxyzlru00wqiw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fri3epbwbxyzlru00wqiw.jpeg" alt="BottomNavigationBarType.shifting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';

void main() =&amp;gt; runApp(const MyApp());

/// Main application
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  static const String _title = 'App title';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

/// The stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State&amp;lt;MyStatefulWidget&amp;gt; createState() =&amp;gt; _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State&amp;lt;MyStatefulWidget&amp;gt; {
  int _selectedIndex = 0;
  static const List&amp;lt;Widget&amp;gt; _widgetOptions = &amp;lt;Widget&amp;gt;[
    Text('Index 0: Home'),
    Text('Index 1: Business'),
    Text('Index 2: School'),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('App with BottomNavBar'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _selectedIndex,
        unselectedItemColor: Colors.blue,
        selectedItemColor: Colors.black,
        onTap: _onItemTapped,

        items: const &amp;lt;BottomNavigationBarItem&amp;gt;[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'Business',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            label: 'School',
          ),
        ],
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tips
&lt;/h2&gt;

&lt;p&gt;However, switching between views would dispose and rebuild the widgets. It means the status of each view cannot be retained when being switched away. A solution for this is to use &lt;a href="https://api.flutter.dev/flutter/widgets/IndexedStack-class.html" rel="noopener noreferrer"&gt;IndexedStack&lt;/a&gt;. I will write another article on how to use IndexedStack. You may also want to explore it by reading &lt;a href="https://medium.com/flutter/getting-to-the-bottom-of-navigation-in-flutter-b3e440b9386" rel="noopener noreferrer"&gt;this&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>flutter</category>
    </item>
    <item>
      <title>How to keep all constants in Flutter</title>
      <dc:creator>Rui.Z</dc:creator>
      <pubDate>Fri, 04 Jun 2021 20:20:54 +0000</pubDate>
      <link>https://dev.to/zhangrui/how-to-keep-all-constants-in-flutter-9fj</link>
      <guid>https://dev.to/zhangrui/how-to-keep-all-constants-in-flutter-9fj</guid>
      <description>&lt;p&gt;A clean and simple way to keep constants in Flutter is to make an own Dart library in the project for the constants.&lt;/p&gt;

&lt;p&gt;For example, we have a project structure like this.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94ykfa2sv5p2abzy1whw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94ykfa2sv5p2abzy1whw.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;constats.dart&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const String BUTTON_LABEL = 'Submit';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The const keyword is used to represent a compile-time constant. Variables declared with const keyword are implicitly final. &lt;a href="https://dart.dev/guides/language/language-tour#final-and-const" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can then use import statement to access to the constants:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:&amp;lt;project_name&amp;gt;/assets/constants.dart' as constants;
...
ElevatedButton(
  child: const Text(constants.BUTTON_LABEL),
)
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>flutter</category>
      <category>dart</category>
    </item>
  </channel>
</rss>
