<?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: CY UKET</title>
    <description>The latest articles on DEV Community by CY UKET (@cyuket).</description>
    <link>https://dev.to/cyuket</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%2F356502%2F42774b75-0996-4f33-b84a-0ad830d2e298.jpeg</url>
      <title>DEV Community: CY UKET</title>
      <link>https://dev.to/cyuket</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cyuket"/>
    <language>en</language>
    <item>
      <title>How to Build an Onboarding Screen using PageView In Flutter</title>
      <dc:creator>CY UKET</dc:creator>
      <pubDate>Sat, 02 May 2020 14:04:49 +0000</pubDate>
      <link>https://dev.to/cyuket/how-to-build-an-onboarding-screen-using-pageview-in-flutter-2hga</link>
      <guid>https://dev.to/cyuket/how-to-build-an-onboarding-screen-using-pageview-in-flutter-2hga</guid>
      <description>&lt;p&gt;Prerequisites&lt;/p&gt;

&lt;p&gt;Tools required to follow the article.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flutter SDK.&lt;/li&gt;
&lt;li&gt;Android Studio or VS code.&lt;/li&gt;
&lt;li&gt;Emulator or an Android device&lt;/li&gt;
&lt;li&gt;Background knowledge of Flutter and Dart&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although there is a packaged already built to do this, there is an advantage getting thing done yourself, one of which customizing the design pattern to suit you. I will take you through achieving this without an external help of a provider.&lt;/p&gt;

&lt;p&gt;Setup&lt;/p&gt;

&lt;p&gt;Create a new project with the command below&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flutter create your_app_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Open the folder on your preferred text editor and open the &lt;code&gt;main.dart&lt;/code&gt; which is in &lt;code&gt;lib&lt;/code&gt; folder and replace the the code with:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';
void main() =&amp;gt; runApp(MyApp());
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() =&amp;gt; _MyHomePageState();
}
class _MyHomePageState extends State&amp;lt;MyHomePage&amp;gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To achieve the goal at hand flutter already have a widget called &lt;code&gt;PageView&lt;/code&gt; Widget, so my job is to show you how this can be used.&lt;/p&gt;

&lt;p&gt;PageView&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;PageView&lt;/code&gt; allows the user to swipe between different screens in your app. All you need to set it up are a &lt;code&gt;PageViewController&lt;/code&gt; which is a required property of the PageView. Each or widget to be swiped is wrapped inside a list of Widget as children.&lt;/p&gt;

&lt;p&gt;Time to See how the magic works&lt;/p&gt;

&lt;p&gt;Initializing PageController &lt;/p&gt;

&lt;p&gt;Inside the stateful widget create &lt;code&gt;controller&lt;/code&gt; variable whose type is of &lt;code&gt;PageViewController&lt;/code&gt; and another &lt;code&gt;int&lt;/code&gt; variable to hold the current index of the page&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  PageController _pageController;
  int currentIndex = 0;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Initialize the state, add the following code after declaring of the page controller’s variable&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@override
  void initState() {
    super.initState();
    _pageController = PageController();
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Also the controller needs to be disposed, to do this add the following code after the last previous one&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Building Screens&lt;/p&gt;

&lt;p&gt;Update the body of your scaffold with the following code&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PageView(
    controller: _pageController,
    children: &amp;lt;Widget&amp;gt;[
      Container(
        child: Text("First Screen")
      ),
      Container(
        child: Text("Second Screen")
      ),
      Container(
        child: Text("Third Screen")
      )

    ],
  ),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Adding Indicators&lt;/p&gt;

&lt;p&gt;It is common in different application with onboarding screens to see indicators to which screen is currently viewed. The &lt;code&gt;PageView&lt;/code&gt; has provision for function which listen to the change in screen and return the index of the current screen. You will create a custom stateless widget for the indicator, and place it in the view. Create the Indicator widget with the code below outside the &lt;code&gt;MyHomePage&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Indicator extends StatelessWidget {
  final int positionIndex, currentIndex;
  const Indicator({this.currentIndex, this.positionIndex});
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 12,
      width: 12,
      decoration: BoxDecoration(
          border: Border.all(color: Colors.blue),
          color: positionIndex == currentIndex
              ? Colors.blue
              : Colors.transparent,
          borderRadius: BorderRadius.circular(100)),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now you have wrap the &lt;code&gt;PageView&lt;/code&gt; widget in &lt;code&gt;Stack&lt;/code&gt; widgets so you can position another thing on the screen and where ever you wish to place it. &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body: Stack(
        children: &amp;lt;Widget&amp;gt;[
          PageView(
            controller: _pageController,
            children: &amp;lt;Widget&amp;gt;[
              Container(child: Text("First Screen")),
              Container(child: Text("Second Screen")),
              Container(child: Text("Third Screen"))
            ],
          ),
        ],
      ),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For the Indicator, it is going to be placed at the bottom of the screen in a row. add the following code as the second widget in the stack&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Positioned(
  bottom: 60,
  left: 150,
  child: Row(
    mainAxisSize: MainAxisSize.max,
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: &amp;lt;Widget&amp;gt;[
      Indicator(
        positionIndex: 0,
        currentIndex: currentIndex,
      ),
      SizedBox(
        width: 10,
      ),
      Indicator(
        positionIndex: 1,
        currentIndex: currentIndex,
      ),
      SizedBox(
        width: 10,
      ),
      Indicator(
        positionIndex: 2,
        currentIndex: currentIndex,
      ),
    ],
  ),
), 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now you will create a method to update the index of the page once it is changed&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onChangedFunction(int index) {
    setState(() {
      currentIndex = index;
    });
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As earlier mentioned the &lt;code&gt;PageView&lt;/code&gt; has a property called &lt;code&gt;onPageChanged&lt;/code&gt; which takes a method and return the index of the current page. go ahead and add the method above to the  &lt;code&gt;PageView&lt;/code&gt; widget&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onPageChanged: onChangedFunction,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next and Previous Button&lt;/p&gt;

&lt;p&gt;Lastly, you can also add a next or previous button to your page. You can do this by acessing the &lt;code&gt;nextPage&lt;/code&gt; and &lt;code&gt;previousPage&lt;/code&gt; which are both methods built into the &lt;code&gt;PageController&lt;/code&gt; and takes two parameter &lt;code&gt;duration&lt;/code&gt; which takes the duration for the page to switch, and &lt;code&gt;curve&lt;/code&gt;  type of transition animation you want to apply while changing. create two static for &lt;code&gt;duration&lt;/code&gt; and the &lt;code&gt;curve&lt;/code&gt; after &lt;code&gt;_pageController&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;static const _kDuration = const Duration(milliseconds: 300);
static const _kCurve = Curves.ease;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Also Create two methods to handle both functions respectively&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; nextFunction() {
    _pageController.nextPage(duration: _kDuration, curve: _kCurve);
  }
 previousFunction() {
    _pageController.previousPage(duration: _kDuration, curve: _kCurve);
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Update your view by adding this as the last widget in the stack&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Positioned(
  bottom: 30,
  left: 130,
  child: Padding(
    padding: const EdgeInsets.symmetric(horizontal: 20.0),
    child: Container(
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: &amp;lt;Widget&amp;gt;[
          InkWell(
              onTap: () =&amp;gt; previousFunction(),
              child: Text("Previous")),
          SizedBox(
            width: 50,
          ),
          InkWell(onTap: () =&amp;gt; nextFunction(), child: Text("Next"))
        ],
      ),
    ),
  ),
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>programming</category>
    </item>
    <item>
      <title>State Management In Flutter</title>
      <dc:creator>CY UKET</dc:creator>
      <pubDate>Sat, 02 May 2020 00:39:09 +0000</pubDate>
      <link>https://dev.to/cyuket/state-management-in-flutter-58he</link>
      <guid>https://dev.to/cyuket/state-management-in-flutter-58he</guid>
      <description>&lt;p&gt;Prerequisites&lt;/p&gt;

&lt;p&gt;Tools required to follow the article.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flutter SDK.&lt;/li&gt;
&lt;li&gt;Android Studio or VS code.&lt;/li&gt;
&lt;li&gt;Emulator or an Android device&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;According to Wikipedia, Flutter is an open-source UI software development kit created by Google. It is used to develop applications for Android, iOS, Windows, Mac, Linux, Google Fuchsia and the web  using Dart language .  &lt;/p&gt;

&lt;p&gt;In Flutter, everything is a widgets which have states, otherwise known as information rendered by the widgets. The state of your application is therefore important to consider, how widgets, content change base on actions and how data is passed on from one widget to another widget in the tree is very essential for the run-time performance of your application. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;State is information that can be read synchronously when the widget is built and might change during the lifetime of the widget.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What We are Building&lt;/p&gt;

&lt;p&gt;At the end of this article, we will show how set state and how to pass data from parent widget to the child widget.&lt;/p&gt;

&lt;p&gt;Widget tree is a structure that represents how our widgets are organized.&lt;br&gt;
Below is a widget tree that describes how the application we will be building.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--42OUUhoq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_5FFA95AC7D57CA650C30D0E12AE5C15226B21984D3FE4E72A7009966A6A5D7A8_1577544827288_wiget_tree.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--42OUUhoq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://paper-attachments.dropbox.com/s_5FFA95AC7D57CA650C30D0E12AE5C15226B21984D3FE4E72A7009966A6A5D7A8_1577544827288_wiget_tree.jpg" alt="figure 1 : Widget Tree of The Application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Setting up Project Tools&lt;/p&gt;

&lt;p&gt;Create a new app with the &lt;code&gt;flutter create app_name&lt;/code&gt;command and open the project on your preferred IDE.&lt;/p&gt;

&lt;p&gt;From our &lt;code&gt;figure 1&lt;/code&gt; above we are going to create three stateless classes each representing our different levels in the tree.&lt;br&gt;
Replace your &lt;code&gt;main.dart&lt;/code&gt; with the following code:&lt;/p&gt;

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

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Level1(),
    );
  }
}

class Level1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      centerTitle: true,
        title: Text('Cy is 10'),
      ),
      body: Level2(),
    );
  }
}

class Level2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Level3(),
    );
  }
}

class Level3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('My Real Age is 10');
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The next line of code we see is&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void main() =&amp;gt; runApp(MyApp());
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Dart programs has an entry point called &lt;code&gt;main&lt;/code&gt;. When we run Flutter or dart file it first runs main function. In this case the main function is calling flutter specific function called &lt;code&gt;runApp&lt;/code&gt; which takes any widget as an argument and created a layout which fills the screen in our case it takes &lt;code&gt;MyApp&lt;/code&gt; as it argument.&lt;br&gt;
&lt;code&gt;MyApp&lt;/code&gt; is a stateless widget which returns &lt;code&gt;MaterialApp&lt;/code&gt; widgetwhich contains three arguments title, theme and home.  Home in our takes   &lt;code&gt;Level1&lt;/code&gt;  as it argument. &lt;code&gt;Level1&lt;/code&gt; returns a Scaffold widget which has an &lt;code&gt;AppBar&lt;/code&gt; widget  that contains a &lt;code&gt;Text&lt;/code&gt; widget as its title. Also, the Scaffold widget has &lt;code&gt;Level2&lt;/code&gt; as its body. Level2&lt;code&gt;returns a&lt;/code&gt;Container&lt;code&gt;widget which has&lt;/code&gt;Level3&lt;code&gt;as it child argument, finally&lt;/code&gt;Level3&lt;code&gt;returns a&lt;/code&gt;Text&lt;code&gt;widget which prints out some strings to the screen. &lt;br&gt;
If you noticed there is a widget that is missing from our code that is the button that will increment my age any time it's pressed. So you can guess which level we will be implementing that. Hope you guessed right, our button will be in our&lt;/code&gt;Level2&lt;code&gt;widget but it will be a&lt;/code&gt;Column&lt;code&gt;so it contains both&lt;/code&gt;Level3&lt;code&gt;And&lt;/code&gt;Button`.&lt;/p&gt;

&lt;p&gt;So let’s update our &lt;code&gt;Level2 class&lt;/code&gt; with the following code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: &amp;lt;Widget&amp;gt;[
            Level3(),
            FlatButton(
              color: Colors.blue,
              onPressed: () {},
              child: Text('Add'),
            )
          ],
        ),
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;State Management Techniques&lt;/p&gt;

&lt;p&gt;There are several ways to manage the state of an application, especially in Flutter. None of these ways is termed “best”. For me, I would say choose a technique according to the complexity of your application. &lt;br&gt;
These are different ways to manage state in a Flutter application which includes&lt;br&gt;
SetState, Lifting State Up, Provider, Inherited Widget, Bloc, MVC, Scoped Model, MobX, Redux&lt;/p&gt;

&lt;p&gt;For the lesson, we are going to look into the first three which is SetState, Lifting State Up and Provider.&lt;/p&gt;

&lt;p&gt;SetState&lt;/p&gt;

&lt;p&gt;Our widgets are either Stateless or Stateful widget.&lt;br&gt;
Stateless Widget: As the name implies, it a widget which cannot change its properties throughout the run-time of the application. You can as well call a static widget. For Properties to Change It has to be rebuilt for changes to reflect on the widget. As you can see from our code all our widgets from &lt;code&gt;MyApp&lt;/code&gt; to &lt;code&gt;level3&lt;/code&gt; are stateless widgets.&lt;br&gt;
Stateful Widget: This can be said to be the opposite of a Stateless Widget. This allows you to change the state of widget even during the run-time of the application. &lt;br&gt;
So for us to be able to increment our Age in our application when the button is clicked, one of our widgets will have to change to Stateful Widget and if you are quick enough you will notice it our &lt;code&gt;Level2&lt;/code&gt; &lt;br&gt;
But first, we need to make the age number in &lt;code&gt;level3&lt;/code&gt; dynamic. so let us update our &lt;code&gt;Level3&lt;/code&gt; first just above the &lt;code&gt;@override&lt;/code&gt; add the following lines code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;final int age;
Level3(this.age);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And replace the &lt;code&gt;Text&lt;/code&gt; widget with the code below:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Text(
  'My Real Age is $age',
  style: TextStyle(
    fontSize: 20,
    fontWeight: FontWeight.bold,
  ),
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Our &lt;code&gt;Level3&lt;/code&gt;  now looks like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level3 extends StatelessWidget {
  final int age;
  Level3(this.age);
  @override
  Widget build(BuildContext context) {
    return Text(
      'My Real Age is $age',
      style: TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let’s move over to &lt;code&gt;Level2&lt;/code&gt; and make first make it a Stateful widget.&lt;br&gt;
Replace the &lt;code&gt;Level2&lt;/code&gt; with the following code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class _Level2State extends State&amp;lt;Level2&amp;gt; {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: &amp;lt;Widget&amp;gt;[
            Level3(age),
            FlatButton(
              color: Colors.blue,
              onPressed: () {},
              child: Text('Add'),
            )
          ],
        ),
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We need to create an integer variable called &lt;code&gt;age&lt;/code&gt; to hold an initial age value of 10, and we do that in the private class of the widget, the &lt;code&gt;_Level2State&lt;/code&gt; &lt;br&gt;
add the following code after the opening brace:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int age = 10;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You have to pass the variable &lt;code&gt;age&lt;/code&gt; into the &lt;code&gt;Level3&lt;/code&gt; widget, update the how it’s been called:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Level3(age),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now Let’s use &lt;code&gt;setState&lt;/code&gt; to add to our age. If you noticed the flat button has an &lt;code&gt;onPressed&lt;/code&gt; which requires a callback function that is triggered only when the button is pressed. So let's add our function to update the age.&lt;br&gt;
update the &lt;code&gt;onPressed&lt;/code&gt; function with the code below:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onPressed: () {
  setState(() {
    age++; 
  });
},
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Your &lt;code&gt;Level2&lt;/code&gt; should look like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class _Level2State extends State&amp;lt;Level2&amp;gt; {
  int age = 10;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: &amp;lt;Widget&amp;gt;[
            Level3(age),
            FlatButton(
              color: Colors.blue,
              onPressed: () {
                setState(() {
                  age++;
                });
              },
              child: Text('Add'),
            )
          ],
        ),
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After this, if we run the application, when you press the button you notice the age increases each time the button is pressed. But we still have a problem and that is we are not able to update the age value on our &lt;code&gt;AppBar&lt;/code&gt; widget. So to this, we have to lift the states.&lt;/p&gt;

&lt;p&gt;Lifting State Up&lt;/p&gt;

&lt;p&gt;This is a process of defining and setting all states at a high level in the tree so that all the children of the widget will be able to access. &lt;br&gt;
Since our &lt;code&gt;AppBar&lt;/code&gt; needs to access the age variable and change when it is pressed, we have to lift the state to the parent widget in our case is our &lt;code&gt;Level1&lt;/code&gt; widget.&lt;br&gt;
So first we need to do the same as we did to our &lt;code&gt;Level2&lt;/code&gt; widget, change it stateful widget and declare our variable age. &lt;br&gt;
Now we can add make the access the age in the &lt;code&gt;AppBar&lt;/code&gt;, update the &lt;code&gt;AppBar&lt;/code&gt; to:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;appBar: AppBar(
  centerTitle: true,
  title: Text('Cy is $age'),
),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;Level1&lt;/code&gt; should look like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level1 extends StatefulWidget {
  @override
  _Level1State createState() =&amp;gt; _Level1State();
}

class _Level1State extends State&amp;lt;Level1&amp;gt; {
  int age = 10;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Cy is $age'),
      ),
      body: Level2(),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We also Have to anticipate the age variable in our &lt;code&gt;Level2&lt;/code&gt; and change it back to stateless widget since we will be handling the state in the parent widget&lt;br&gt;
 Replace the &lt;code&gt;Level2&lt;/code&gt; with the code below:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; class Level2 extends StatelessWidget {
  final int age;
  Level2({this.age});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: &amp;lt;Widget&amp;gt;[
            Level3(age),
            FlatButton(
              color: Colors.blue,
              onPressed: () {
                setState(() {
                  age++;
                });
              },
              child: Text('Add'),
            )
          ],
        ),
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;also, pass &lt;code&gt;age&lt;/code&gt; into the &lt;code&gt;Level2&lt;/code&gt; from &lt;code&gt;Level1&lt;/code&gt;, update the body with:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body: Level2(age: age,),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now we observe that an error is indicated in the line that has the &lt;code&gt;setState&lt;/code&gt; in the &lt;code&gt;Level2&lt;/code&gt; widget, that is telling us that we can’t set states in a stateless widget. So we go ahead and empty that function.&lt;br&gt;
Now that both the &lt;code&gt;AppBar&lt;/code&gt; and other widget bellow the tree can access the &lt;code&gt;age&lt;/code&gt; variable let’s create a function that will update when the button is pressed. &lt;br&gt;
Just after where you declared the variable &lt;code&gt;age&lt;/code&gt; in &lt;code&gt;Level1&lt;/code&gt; let’s create the function:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void addAge() {
  setState(() {
    age++;
  });
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since our button is in &lt;code&gt;Level2&lt;/code&gt; we have to pass it down as props, so we need to anticipate this function in &lt;code&gt;Level2&lt;/code&gt; so update your &lt;code&gt;Level2&lt;/code&gt; constructor with the following:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;final int age;
final Function onPressedFunction;
Level2({this.age, this.onPressedFunction});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;now we can replace our &lt;code&gt;onPressed&lt;/code&gt; in &lt;code&gt;Level2&lt;/code&gt; with the code bellow:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onPressed: onPressedFunction,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;so let’s pass the function down to &lt;code&gt;Level2&lt;/code&gt; in &lt;code&gt;Level1&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body: Level2(
  age: age,
  onPressedFunction: addAge,
),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Our &lt;code&gt;Level1&lt;/code&gt; widget looks like :&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level1 extends StatefulWidget {
  @override
  _Level1State createState() =&amp;gt; _Level1State();
}

class _Level1State extends State&amp;lt;Level1&amp;gt; {
  int age = 10;

  void addAge() {
    setState(() {
      age++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Cy is $age'),
      ),
      body: Level2(
        age: age,
        onPressedFunction: addAge,
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And &lt;code&gt;Level2&lt;/code&gt; looks like :&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level2 extends StatelessWidget {
  final int age;
  final Function onPressedFunction;
  Level2({this.age, this.onPressedFunction});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: &amp;lt;Widget&amp;gt;[
            Level3(age),
            FlatButton(
              color: Colors.blue,
              onPressed: onPressedFunction,
              child: Text('Add'),
            )
          ],
        ),
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And &lt;code&gt;Level3&lt;/code&gt; &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level3 extends StatelessWidget {
  final int age;
  Level3(this.age);

  @override
  Widget build(BuildContext context) {
    return Text(
      'My Real Age is $age',
      style: TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Restarting the application and pressing  the button you can notice that both the text in the &lt;code&gt;AppBar&lt;/code&gt; and the &lt;code&gt;Text&lt;/code&gt; in &lt;code&gt;Level3&lt;/code&gt; changes simultaneously.&lt;br&gt;
So basically we have been able to pass data from the top widget in the tree down to the last child in the tree and also update it with an action button which is in the middle level of the tree. You can as well take a bow cause our aim has been achieved right but I will introduce you to a better way of handling state with a package made available and recommended to us by the flutter team at Google&lt;/p&gt;

&lt;p&gt;Provider&lt;/p&gt;

&lt;p&gt;Provider is packaged is maintained by the flutter team and it is the recommended way of managing the state of an application. So let’s see how to use it in our application. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So if you are wondering why we are introducing Provider to our app, here is a simple reason, we want to access data anywhere in our widget tree without having to pass them from one widget to another.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First of all, we need to install the package to our application, click here to see the full documentation of the package. Let’s add it to our &lt;code&gt;pubspec.yaml&lt;/code&gt;, after &lt;code&gt;cupertino_icons&lt;/code&gt; lets, add the following code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; provider: ^4.0.0 //4.0.0 is the current version as at when this article was written */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;click on &lt;code&gt;package get&lt;/code&gt; to download the packages.&lt;br&gt;
Going back to our &lt;code&gt;main.dart&lt;/code&gt; import provider so we can have access to the classes we will be needing, at the beginning of our file add this&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:provider/provider.dart';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For us to spread our data and modify them we will need a class that extends a flutter class called &lt;code&gt;ChangeNotifier&lt;/code&gt;, and defined our age and function to increment our age&lt;br&gt;
 At the bottom of our &lt;code&gt;main.dart&lt;/code&gt; add the following code:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AgeClass extends ChangeNotifier {
  int age = 10;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let’s start using our provider package in our application. the package is always defined at the top of the widget tree. So let's go over to our &lt;code&gt;MyApp&lt;/code&gt; and update what it is returning with the following: &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return ChangeNotifierProvider&amp;lt;AgeClass&amp;gt;(
  create: (context) =&amp;gt; AgeClass(),
  child: MaterialApp(
    title: 'Flutter Demo',
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: Level1(),
  ),
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;From the code we just added, we have a new widget from the provider package called &lt;code&gt;ChangeNotifierProvider&lt;/code&gt; and the type of data we are expecting in return, in our case we are expecting the &lt;code&gt;AgeClass&lt;/code&gt; which we add defined. Also it &lt;code&gt;ChangeNotifierProvider&lt;/code&gt; requires a &lt;code&gt;create&lt;/code&gt;  function that takes the current context as an argument and returns the actual data, in our case it returning the &lt;code&gt;AgeClass&lt;/code&gt; because that is where our data is stored. &lt;br&gt;
Now our data is can be accessed with a &lt;code&gt;Provider.&lt;/code&gt;&lt;code&gt;of&lt;/code&gt;&lt;code&gt;&amp;lt;*dataType expected*&amp;gt;(context)&lt;/code&gt;&lt;br&gt;
so let’s go ahead and all our data where we need it, first the &lt;code&gt;AppBar&lt;/code&gt; let’s update the &lt;code&gt;title&lt;/code&gt; widget in &lt;code&gt;Level1&lt;/code&gt; with :&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;title: Text('Cy is ${Provider.of&amp;lt;AgeClass&amp;gt;(context).age}'),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;then &lt;code&gt;Text&lt;/code&gt; in &lt;code&gt;Level3&lt;/code&gt; with:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Text(
  'My Real Age is ${Provider.of&amp;lt;AgeClass&amp;gt;(context).age}',
  style: TextStyle(
    fontSize: 20,
    fontWeight: FontWeight.bold,
  ),
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now if you run the app you will realize the function in the add button is not function let’s create that immediately. In the &lt;code&gt;AgeClass&lt;/code&gt;, we will create a function to do this and notify every widget that is listening to data that is been modified and trigger a rebuild of only the widget with the &lt;code&gt;notifyListeners()&lt;/code&gt; method. &lt;br&gt;
so let’s add the following code to our &lt;code&gt;AgeClass&lt;/code&gt; :&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void addAge() {
  age++;
  notifyListeners();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;we have created a function to increment our age let’s go ahead and append it to our &lt;code&gt;FlatButton&lt;/code&gt;'s &lt;code&gt;onPress&lt;/code&gt;. Update the &lt;code&gt;onPressed&lt;/code&gt; with the code below:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onPressed: Provider.of&amp;lt;AgeClass&amp;gt;(context).addAge,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;so you can go ahead and change your &lt;code&gt;Level1&lt;/code&gt; back to a &lt;code&gt;stateless&lt;/code&gt; widget&lt;br&gt;
and also delete the variable &lt;code&gt;age&lt;/code&gt; and the function &lt;code&gt;addAge&lt;/code&gt; inside of it. Your &lt;code&gt;Level1&lt;/code&gt; should look like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level1 extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Cy is ${Provider.of&amp;lt;AgeClass&amp;gt;(context).age}'),
      ),
      body: Level2(),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In &lt;code&gt;Level2&lt;/code&gt;, erase the Function &lt;code&gt;onPressedFunction&lt;/code&gt; and the constructor for it to look as the code bellow:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: &amp;lt;Widget&amp;gt;[
            Level3(),
            FlatButton(
              color: Colors.blue,
              onPressed: Provider.of&amp;lt;AgeClass&amp;gt;(context).addAge,
              child: Text('Add'),
            )
          ],
        ),
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Also, erase the unused variable &lt;code&gt;age&lt;/code&gt; in level three and its constructor. it should look like this:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Level3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text(
      'My Real Age is ${Provider.of&amp;lt;AgeClass&amp;gt;(context).age}',
      style: TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And &lt;code&gt;AgeClass&lt;/code&gt; &lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AgeClass extends ChangeNotifier {
  int age = 10;

  void addAge() {
    age++;
    notifyListeners();
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;so after following this tutorials your &lt;code&gt;main.dart&lt;/code&gt; file should look like what I have bellow:&lt;/p&gt;

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

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider&amp;lt;AgeClass&amp;gt;(
      create: (context) =&amp;gt; AgeClass(),
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Level1(),
      ),
    );
  }
}

class Level1 extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Cy is ${Provider.of&amp;lt;AgeClass&amp;gt;(context).age}'),
      ),
      body: Level2(),
    );
  }
}

class Level2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: &amp;lt;Widget&amp;gt;[
            Level3(),
            FlatButton(
              color: Colors.blue,
              onPressed: Provider.of&amp;lt;AgeClass&amp;gt;(context).addAge,
              child: Text('Add'),
            )
          ],
        ),
      ),
    );
  }
}

class Level3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text(
      'My Real Age is ${Provider.of&amp;lt;AgeClass&amp;gt;(context).age}',
      style: TextStyle(
        fontSize: 20,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}

class AgeClass extends ChangeNotifier {
  int age = 10;

  void addAge() {
    age++;
    notifyListeners();
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Conclusion &lt;/p&gt;

&lt;p&gt;During this article, we’ve learned about the different ways of state management, from using SetState and passing it down the three to increase the scope of the state by Lifting States up to a higher widget and using the provider package to handle states properly.&lt;/p&gt;

&lt;p&gt;Happy Fluttering&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
