{"id":81,"date":"2023-02-20T02:37:35","date_gmt":"2023-02-20T02:37:35","guid":{"rendered":"http:\/\/blog.aryapurwadana.com\/?p=81"},"modified":"2023-02-20T02:37:37","modified_gmt":"2023-02-20T02:37:37","slug":"flutter-introduction","status":"publish","type":"post","link":"https:\/\/blog.aryapurwadana.com\/index.php\/2023\/02\/20\/flutter-introduction\/","title":{"rendered":"Flutter &#8211; Introduction"},"content":{"rendered":"\n<p>Flutter is a growing mobile framework developed by Google, designed to help in the development of mobile and web applications with only one code base and the ability to export them as native apps for Android and iOS.<\/p>\n\n\n\n<p>In this article, we will dive into the basics of Flutter. What is it? How does it work? What is Dart? To answer these questions, we\u2019ll create a small application that will help us understand the foundations of Flutter.<\/p>\n\n\n\n<h2 id=\"what-is-flutter\" class=\"wp-block-heading\">What is Flutter?<\/h2>\n\n\n\n<p>Flutter is a tool that allows developers to build native cross-platform apps with just one programming language and also one codebase. It will not create an app that runs in the browser or something that gets wrapped by native apps. Instead, it creates a native app for both iOS and Android that can be published to the stores later.<\/p>\n\n\n\n<p>The good news is that you can create this app just by using one programming language, rather than using separate ones to build an iOS app or an Android app. That way, you will have just one code base to worry about. Flutter is an SDK (Software Development Kit) that allows you to compile your codebase into native machine code that runs on the platforms mentioned above.<br><br>Besides its compilation tools, Flutter acts as a framework by providing a collection of UI building blocks (widgets) like tabs, dropdowns, buttons, etc., some utility functions; and some packages that will get compiled by using the SDK.<\/p>\n\n\n\n<p>Flutter can be compared to React Native. Our CTO, <a href=\"https:\/\/www.fullstacklabs.co\/team\/mike-piccolo\" target=\"_blank\" rel=\"noreferrer noopener\">Mike Piccolo<\/a>, discusses the differences between Flutter and React Native in <a href=\"https:\/\/www.fullstacklabs.co\/blog\/react-native-vs-flutter\" target=\"_blank\" rel=\"noreferrer noopener\">React Native vs Flutter: A CTO\u2019s Perspective<\/a>.<\/p>\n\n\n\n<h2 id=\"what-is-dart\" class=\"wp-block-heading\">What is Dart?<\/h2>\n\n\n\n<p>Dart is the programming language that Flutter uses. It is focused on building front-end user interfaces and front-end apps. By using it, you will be able to create either web apps or mobile apps.<\/p>\n\n\n\n<p>It is developed by Google and is a class-based, object-oriented, and strongly typed programming language. Dart\u2019s syntax is very similar to languages like Java or JavaScript, so if you have some experience with those, it will be easy for you to begin using it.<\/p>\n\n\n\n<h2 id=\"union-means-strength\" class=\"wp-block-heading\">Union means strength<\/h2>\n\n\n\n<p>So Flutter and Dart are actually two independent things that work together. Flutter is a framework for Dart, and Dart uses the capabilities of Flutter to build the app. In the end, Flutter working as an SDK will help to build the app.<\/p>\n\n\n\n<p>Even though you\u2019ll only have one code base, if at any specific moment you need to differentiate for the platforms you are building for, Flutter will give you the tools to find the platform you are running on and act accordingly either for iOS or Android.<\/p>\n\n\n\n<h2 id=\"widgets\" class=\"wp-block-heading\">Widgets<\/h2>\n\n\n\n<p>Flutter is built using widgets. If you have spent time in React\u2019s world, you could say that a widget is a component. Essentially, it is a snippet of code with some instructions that will have to be beside other widgets and interact with them.<\/p>\n\n\n\n<p>Let\u2019s take a look:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets-global.website-files.com\/5d9bc5d562ffc22c37470958\/60d1fec4a45bcf565257e832_2OBA3jbO6vxWtaRilOXc_g-41kdshnfqdFV3j1QQR-h1djdU16Eok1PjVtiuxtXLPDrL_NVrW5ljt5RpUo0dSjpYYWs8OPtXs86n5GLF0SXcIyhOTdvDTDRiTa3Tbsflabg5moyC.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>On a simple view like this, you will have a bunch of widgets working together:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Login button<\/li>\n\n\n\n<li>Carrousel of features (with two children widgets)<\/li>\n\n\n\n<li>Register button<\/li>\n\n\n\n<li>The main widget wrapping the entire app<\/li>\n\n\n\n<li>Some layout or stylistic widgets<\/li>\n<\/ul>\n\n\n\n<p>So you will be creating a widget\u2019s tree, with one main root. You might have different views and inside these, you could also have several sections that are composed of a set of widgets.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets-global.website-files.com\/5d9bc5d562ffc22c37470958\/60d1fec498dbacdffac95871_PnPOCsaq9CBMQx5Zqihf6Z21_aAcBuaXmj0v5nzbSuuloyRB8eUoVqgnaTSwmhZXLZtP4nAi4cazzbBFSjdIZDayEkdoMbWJFrypBfyeZfda9awhk_9i7KQvZHZZ8YpW3tOK__16.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Another aspect about widgets is the fact that there is not only one type, but different&nbsp; types of widgets that can be divided into the following categories:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Structural widgets (Card, ElevatedButton, Text)<\/li>\n\n\n\n<li>Stylistic widgets (ColorScheme)<\/li>\n\n\n\n<li>Layout widgets (Column, ListView)<\/li>\n<\/ul>\n\n\n\n<h2 id=\"lets-get-our-hands-dirty\" class=\"wp-block-heading\">Let\u2019s get our hands dirty<\/h2>\n\n\n\n<p>First of all, install Flutter and follow all the steps described to set up your env in the following link: <a href=\"https:\/\/flutter.dev\/docs\/get-started\/install\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/flutter.dev\/docs\/get-started\/install<\/a>.<\/p>\n\n\n\n<h3 id=\"creating-the-first-app\" class=\"wp-block-heading\">Creating the first app<\/h3>\n\n\n\n<p>Once you\u2019re done with that, it&#8217;s time to create your first Flutter app by using the command line:<\/p>\n\n\n\n<p><strong><em>flutter create your_app_name<\/em><\/strong><\/p>\n\n\n\n<p>The app\u2019s name must include underscores to separate each word.<\/p>\n\n\n\n<p><strong><em>cd your_app_name<\/em><\/strong><\/p>\n\n\n\n<p>Go inside your folder\u2019s app.<\/p>\n\n\n\n<p><strong><em>flutter run<\/em><\/strong><\/p>\n\n\n\n<p>Then execute this command and you should see the default Flutter app running on your device or your emulator.<\/p>\n\n\n\n<p>Caveat: In this app, you will find that by default, Flutter is using the material design system, but you will not be attached to this. Flutter also provides iOS-styled widgets (Cupertino Widgets) and both of them are highly customizable, so you can even have your own styled app.<\/p>\n\n\n\n<h3 id=\"project-structure\" class=\"wp-block-heading\">Project Structure<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets-global.website-files.com\/5d9bc5d562ffc22c37470958\/60d1ff56ac36e14c10a58415__mflUxrxl5qHBF0p4UF7XEcBgq0jb7u_XcngIvv6qyLt-by2SQlddfpDXmuvUeoKc5F-bnuUU5wNPuGEqP3ED4TMTuXkMildAEGV-3EFTWEylG4sUpCUlCMhJ8sqU43WE-GGOi8k.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>The majority of the files and folders you will find are intended for configuration. Let&#8217;s take a look at the most important ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Android (folder): <\/em>Contains a complete Android project, and it will be the one in which your Flutter app will be \u201cmerged\u201d when compiling to native code. Inside this folder, you will find important files like <em>build.gradle <\/em>and<em> AndroidManifex.xml.<\/em><\/li>\n\n\n\n<li><em>build (folder): <\/em>Maintains the output of your Flutter app and will be managed by Flutter\u2019s SDK.<\/li>\n\n\n\n<li>\u200d<em>ios (folder):<\/em> Contains a complete iOS project and will be the one in which your Flutter app will be \u201cmerged\u201d&nbsp; when compiling to native code.<\/li>\n\n\n\n<li>\u200d<em>Lib (folder): <\/em>The folder where you will probably be working the most. It contains all the .dart files to create the Flutter application and contains the main.dart file, which is by default the entry point of the app.<\/li>\n\n\n\n<li>\u200d<em>test (folder):<\/em> Holds all the automated tests that will check the functionality of the code.<em>\u200d<\/em><\/li>\n\n\n\n<li><em>pubspec.yaml (file):<\/em> Contains some metadata about the flutter application, but most importantly it will be the palace to manage the project\u2019s dependencies, so this is the place to configure the external packages that will be used by your application.<\/li>\n<\/ul>\n\n\n\n<h3 id=\"first-lines-of-code\" class=\"wp-block-heading\">First lines of code<\/h3>\n\n\n\n<p>As mentioned earlier, Flutter is made of widgets, which we need to draw something on the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nvoid main() {\n    runApp(MyApp);\n}\n\nclass MyApp extends StatelessWidget {\n    Widget build(BuildContext context)  {\n        return MaterialApp(home: Text('Hello!'))\n    }\n}\n\n<\/code><\/pre>\n\n\n\n<p>In the code above, we have one main class, <strong><em>MyApp<\/em><\/strong>, that is extending a Flutter class (widget) called <strong><em>StatelessWidget<\/em><\/strong>.<\/p>\n\n\n\n<p>This class needs to define a build method that must return a widget that at the end will be the element rendered in the screen. For that purpose, we use the <strong><em>MaterialApp<\/em><\/strong> class and inside its home property, we make use of another widget, <strong><em>Text<\/em><\/strong>, to render a string.<\/p>\n\n\n\n<p>Finally, all this code will run inside the <strong><em>main<\/em><\/strong> function (the first function to be called when running the application), which in turn will also run another function <strong><em>runApp<\/em><\/strong> to mount its given widget as the <strong>root widget<\/strong> and attach it to the screen.&nbsp;<\/p>\n\n\n\n<p>This is a tiny sample, but as you can see we used many widgets above and it can be cumbersome initially, but that\u2019s actually the way Flutter works \u2014 the user interface is composed by mixing widgets together.<\/p>\n\n\n\n<h2 id=\"base-flutter-widgets\" class=\"wp-block-heading\">Base flutter widgets<\/h2>\n\n\n\n<h3 id=\"statelesswidget\" class=\"wp-block-heading\">StatelessWidget<\/h3>\n\n\n\n<p>A StatelessWidget does not have any mutable state that it needs to track. Instead, it is composed of other children widgets, and it has a build method for that.<\/p>\n\n\n\n<p>All the new widgets extending the <strong><em>StatelessWidget<\/em><\/strong> would not track data over time or trigger re-builds by themselves.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nclass UserTitle extends StatefulWidget {\n final String userName;\n\n UserTitle(this.userName);\n\n @override\n Widget build(BuildContext context) {\n   return DecorateBox(\n     decoration: BoxDecoration(color: Colors.amber),\n     child: Text(userName)\n   );\n }\n}\n\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have a new widget <strong><em>UserTitle<\/em><\/strong> that only makes use of its final property userName. That property never changes over time and it is finally rendered inside the <strong><em>Text<\/em><\/strong> widget.<\/p>\n\n\n\n<p><em>This component only will be rebuilt when its input data changes.<\/em><\/p>\n\n\n\n<h3 id=\"statefulwidget\" class=\"wp-block-heading\">StatefulWidget<\/h3>\n\n\n\n<p>A StatelessWidget provides immutable configuration info and a state object that can change over time and fire rebuilds.<\/p>\n\n\n\n<p>For this kind of widget we would need two classes:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A widget class: This one will be in charge of maintaining the immutable properties (if they exist) and creating the state object.<\/li>\n\n\n\n<li>A state class: This class will maintain the properties that would change and it will be also responsible to build the children widgets.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\nclass UserTitle extends StatefulWidget {\n final String userName;\n\n UserTitle({this.userName});\n\n @override\n _ItemFlutterAppsCounter createState() =&gt;  _ItemFlutterAppsCounter();\n}\n\nclass _ItemFlutterAppsCounter extends State&lt;UserTitle&gt; {\n int appsCount = 1;\n\n @override\n Widget build(BuildContext context) {\n   return GestureDetector(\n     onTap: () {\n       setState(() {\n         appCount++;\n       })\n     },\n     child: Text('The user ${widget.userName} has ${appsCount} flutter apps');\n   );\n }\n}\n\n<\/code><\/pre>\n\n\n\n<p>By using the method <strong><em>setState<\/em><\/strong>, we can make a change to the state object \u2014 in this case, to increase the <strong><em>appsCount<\/em><\/strong> property. This change will finally trigger a rebuild in the UI (more specifically on its children) to reflect the new value.<\/p>\n\n\n\n<p><em>This component will be rebuilt when its input or internal data changes.<\/em><\/p>\n\n\n\n<h4 id=\"inherited-widgets\" class=\"wp-block-heading\">Inherited Widgets<\/h4>\n\n\n\n<p>Passing data from one widget to another can be cumbersome. Inherited widgets help children widgets to access data from a parent one directly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nclass ColorInfo extends InheritedWidget {\n final List&lt;Color&gt; colors;\n\n ColorInfo({ this.colors, required Widget child}) : super(child: child);\n\n static ColorInfo of(BuildContext context) =&gt;  context.inheritedFromWidgetOfExactType(ColorInfo);\n\n bool updateShouldNotify(ColorInfo oldWidget) =&gt; oldWidget.colors != colors;\n}\n\n<\/code><\/pre>\n\n\n\n<p>In the code above, the class <strong><em>ColorInfo<\/em><\/strong> extends the <strong><em>InheritedWidget<\/em><\/strong> and it only has a required property <strong><em>child<\/em><\/strong>, then we can add any property (or properties) that we actually want to be inherited, in this case, it is <strong><em>colors<\/em><\/strong>.<\/p>\n\n\n\n<p><strong><em>ColorInfo<\/em><\/strong> also has two methods:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The <strong><em>of<\/em><\/strong> method: it is in charge to call the InheritedWidget of the exact type, and it will allow children Widgets to access its properties more easily.<\/li>\n\n\n\n<li>The second method is <strong><em>updateShouldNotify<\/em><\/strong> that communicates if a redraw should be executed into the children Widgets when the data changes.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\nClass WidgetThatUsesTheColorInfo extends statelessWidget {\n Widget build(BuildContext context) {\n   return Text(\n     'Test text',\n     style: TextStyle(color: ColorInfo.of(context).colors);\n   );\n }\n}\n\n<\/code><\/pre>\n\n\n\n<p>Then we will have a children widget using the inherited widget, for our example, it is <strong><em>WidgetThatUsesTheColorInfo<\/em><\/strong>, on its <strong>build<\/strong> method, more exactly when using the <strong><em>Text<\/em><\/strong> widget the style property uses the <strong>ColorInfo<\/strong> widget, and then it calls its <strong><em>of <\/em><\/strong>method to access the inherited property <strong><em>colors<\/em><\/strong>.<\/p>\n\n\n\n<h2 id=\"lets-create-something-more-styled\" class=\"wp-block-heading\">Let\u2019s create something more styled<\/h2>\n\n\n\n<p>We want to create a little poll, in which we are going to present the user with two questions separately and each time he\/she will have two possible answers.<\/p>\n\n\n\n<p>Let\u2019s start with the main widget, a stateful one:<\/p>\n\n\n\n<p><strong><em>Main.dart<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ importing our main package\n\nimport 'package:flutter\/material.dart';\n\n\/\/ the app's entry point, that mounts our root widget\n\nvoid main() =&gt; runApp(MyApp());\n\n\/\/ This would be our main widget\n\nclass MyApp extends StatefulWidget {\n\n \/\/ Here we create MyApp state\n @override\n _MyAppState createState() {\n   return _MyAppState();\n }\n}\n\n\/\/ This is the class that holds MyApp's state\n\nclass _MyAppState extends State&lt;MyApp&gt; {\n var _questionIndex = 0;\n\n\n\/\/ Creating some variables that will be used\n var _questions = &#91;\n   'What color do you like the most?',\n   'What\\'s your favorite weather?',\n ];\n\n var _answers = &#91;\n   &#91;'Red', 'Green', 'Blue'],\n   &#91;'Sunny Day', 'Cloudy Day', 'Snowing Day'],\n]\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ This is the method that will update the state\n void _answerQuestion(String answer) {\n   setState(() {\n     if (_questionIndex &lt; 2) {\n       _questionIndex = _questionIndex + 1;\n     }\n   });\n }\n\n\n\/\/ This is the build method it will present some questions if `_questionIndex` is minor than 3 and a text if that condition is not true\n\n @override\n Widget build(BuildContext context) {\n   if (_questionIndex &lt; 2) {\n     return MaterialApp(\n       home: Scaffold(\n         appBar: AppBar(title: Text(\"Personality Test\")),\n         body: Column(\n           children: &#91;\n             Text(_questions&#91;_questionIndex]),\n             ElevatedButton(\n                 onPressed: () =&gt; _answerQuestion(_answers&#91;_questionIndex]&#91;0]),\n                 child: Text(_answers&#91;_questionIndex]&#91;0])),\n             ElevatedButton(\n                 onPressed: () =&gt; _answerQuestion(_answers&#91;_questionIndex]&#91;1]),\n                 child: Text(_answers&#91;_questionIndex]&#91;1])),\n           ],\n         ),\n       ),\n     );\n   } else {\n     return MaterialApp(\n       home: Scaffold(\n         appBar: AppBar(title: Text(\"Liar Test\")),\n         body: Column(\n           children: &#91;\n             Text('We don\u2019t know anything about your personality, but the app looks good!'),\n           ],\n         ),\n       ),\n     );\n   }\n }\n}\n\n<\/code><\/pre>\n\n\n\n<p>This is our awesome app now:<\/p>\n\n\n\n<p>We have defined a StatefulWidget <strong><em>MyApp<\/em><\/strong> and below it, we have its state. With this last one, we implement some rebuilds inside the app each time a button is pressed (by executing the&nbsp; <strong><em>_answerQuestion<\/em><\/strong> function ) to present a new question and some new possible answers.<\/p>\n\n\n\n<p>We do this until our questions counter is less than 2. At that point, we change the widget rendered by our root component and now we present a simple widget with a message inside it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets-global.website-files.com\/5d9bc5d562ffc22c37470958\/60d204d60a4f131dea754986_uvzIo9Uq0QlhDRDueco3qPFlHH8MdheND1Kj53xMoXOoMvpYxrRD0YBbzrHZPRCeR39x_d4D8HKJIZWz-h6SCDqccCm38DXjlc7eHcNvoiGX4HGeeendWNuhg3jgJdTxhBIFg016.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\"><em>(I have removed some of the buttons inside the code above, just for space purposes)<\/em><\/figcaption><\/figure>\n\n\n\n<h3 id=\"scaffold\" class=\"wp-block-heading\">Scaffold:<\/h3>\n\n\n\n<p><em>This is a widget that implements the basic material design visual layout structure.<\/em><\/p>\n\n\n\n<p><em>The most common scenario is to use its <\/em><strong><em>AppBar<\/em><\/strong><em> property to add a (yes, you got it)<\/em><\/p>\n\n\n\n<p><em>App bar for the app, followed by its <\/em><strong><em>Body<\/em><\/strong><em> property where the app content could be rendered.<\/em><\/p>\n\n\n\n<h3 id=\"column-layout\" class=\"wp-block-heading\">Column (layout):<\/h3>\n\n\n\n<p><em>This is a widget that will render its children in a vertical array. It receives a <\/em><strong><em>children<\/em><\/strong><em> property where you can list all the internal widgets that a column is going to have.<\/em><\/p>\n\n\n\n<p><em>If the content is larger than the column\u2019s height, it will not add a scroll and conversely will cut the content.<\/em><\/p>\n\n\n\n<p>Let\u2019s add some styling for the app\u2019s question. We\u2019ll start by taking it to an isolated file:<\/p>\n\n\n\n<p>First, let\u2019s go and update the main file:<\/p>\n\n\n\n<p><strong><em>main.dart<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport 'package:flutter_app\/question.dart';\n\n<\/code><\/pre>\n\n\n\n<p>Replace:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nText(_questions&#91;_questionIndex]),\n\n<\/code><\/pre>\n\n\n\n<p>With:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nQuestion(_questions&#91;_questionIndex]),\n\n<\/code><\/pre>\n\n\n\n<p>Now it\u2019s time for the custom file creation:<\/p>\n\n\n\n<p><strong><em>Question.dart<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport 'package:flutter\/material.dart';\n\nclass Question extends StatelessWidget {\n final String questionText;\n\n Question(this.questionText);\n\n @override\n Widget build(BuildContext context) {\n   return Container(\n     child: Text(\n       questionText,\n       style: TextStyle(\n         fontSize: 40,\n         color: Color.fromRGBO(255, 255, 255, 1),\n         fontWeight: FontWeight.bold,\n       ),\n       textAlign: TextAlign.left,\n     ),\n     margin: EdgeInsets.all(40),\n     width: double.infinity,\n   );\n }\n}\n\n<\/code><\/pre>\n\n\n\n<p>Now we are using a custom widget inside our app. Let\u2019s take a look at some of its children widgets.<\/p>\n\n\n\n<h3 id=\"container-layout-widget\" class=\"wp-block-heading\">Container: (layout widget)<\/h3>\n\n\n\n<p><em>This widget helps to decorate, positioning, and composing children widgets. With this one, you can add padding, margin, borders, or even background color. In the previous code, we wrapped a <\/em><strong><em>Text<\/em><\/strong><em> widget inside the container, and we set the container\u2019s width to expand to the available space and apply some margin (top, right, bottom, left).<\/em><\/p>\n\n\n\n<p><em>This widget only accepts children that must be placed in a property with the same name.<\/em><\/p>\n\n\n\n<p><em>Another type of element with just one children property includes:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Padding<\/em><\/li>\n\n\n\n<li><em>Center<\/em><\/li>\n\n\n\n<li><em>ContainedBox<\/em><\/li>\n<\/ul>\n\n\n\n<h3 id=\"text-ui-widget\" class=\"wp-block-heading\">Text: (UI Widget)<\/h3>\n\n\n\n<p>It is a widget to render any string on the screen. It accepts one position value that must be a <strong><em>string<\/em><\/strong>, but then it has some named properties to configure the current text.<\/p>\n\n\n\n<h3 id=\"textstyle-style-widget\" class=\"wp-block-heading\"><em>TextStyle: (Style Widget)<\/em><\/h3>\n\n\n\n<p>One of the named properties of the <strong><em>Text<\/em><\/strong> widget. It will allow applying some styles to the text. In the example above, we used <strong><em>fontSize<\/em><\/strong>, <strong><em>color<\/em><\/strong>, and <strong><em>fontWeight<\/em><\/strong>. Each of these properties has a specific type so its value must respect that.<\/p>\n\n\n\n<h2 id=\"using-the-custom-widget-in-our-root-widget\" class=\"wp-block-heading\">Using the custom Widget in our root widget<\/h2>\n\n\n\n<p>Let\u2019s take a look at a new, more styled version of our root Widget. It will still be a <strong><em>StatefulWidget <\/em><\/strong>but with some add-ons:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport 'package:flutter\/material.dart';\n\/\/ Importing of the new custom widget: Question\nimport 'package:flutter_app\/question.dart';\n\/\/ Importing of dart\u2019s math package\nimport 'dart:math';\n\nvoid main() =&gt; runApp(MyApp());\n\nclass MyApp extends StatefulWidget {\n @override\n _MyAppState createState() {\n   return _MyAppState();\n }\n}\n\nclass _MyAppState extends State&lt;MyApp&gt; {\n var _questionIndex = 0;\n \/\/ New color variable\n var _color = Colors.deepOrange&#91;400];\n\n var _questions = &#91;\n   'What color do you like the most?',\n   'What\\'s your favorite weather?',\n ];\n\n var _answers = &#91;\n   &#91;'Red', 'Green', 'Blue'],\n   &#91;'Sunny Day', 'Cloudy Day', 'Snowing Day'],\n ];\n\n \nvoid _answerQuestion(String answer) {\n   setState(() {\n     final random = Random();\n\n     if (_questionIndex &lt; 2) {\n       _questionIndex = _questionIndex + 1;\n     }\n\n     \/\/ Generate a random color, and setting it as the value for _color\n     _color = Color.fromRGBO(\n       random.nextInt(256),\n       random.nextInt(256),\n       random.nextInt(256),\n       .8,\n     );\n   });\n }\n\n @override\n Widget build(BuildContext context) {\n   if (_questionIndex &lt; 3) {\n     return MaterialApp(\n       home: Scaffold(\n         appBar: AppBar(title: Text(\"Personality Test\")),\n         \/\/ New AnimatedContainer widget\n         body: AnimatedContainer(\n           child: Column(\n             children: &#91;\n               Question(_questions&#91;_questionIndex]),\n               ElevatedButton(\n                   onPressed: () =&gt;\n                       _answerQuestion(_answers&#91;_questionIndex]&#91;0]),\n                   child: Text(_answers&#91;_questionIndex]&#91;0])),\n               ElevatedButton(\n                   onPressed: () =&gt;\n                       _answerQuestion(_answers&#91;_questionIndex]&#91;1]),\n                   child: Text(_answers&#91;_questionIndex]&#91;1])),\n             ],\n           ),\n           \/\/ AnimatedContainer named properties\n           duration: Duration(seconds: 1),\n           height: double.infinity,\n           width: double.infinity,\n           \/\/ color property takes it\u2019s value from the dynamic _color var\n           color: _color,\n         ),\n       ),\n     );\n\n\n  } else {\n     return MaterialApp(\n       home: Scaffold(\n         appBar: AppBar(title: Text(\"Liar Test\")),\n         \/\/ New AnimatedContainer widget\n         body: AnimatedContainer(\n           child: Center(\n             child: Text(\n               'We don\\\u2019t know anything about you personality, but the app looks good!',\n               style: TextStyle(\n                 fontSize: 40,\n                 color: Color.fromRGBO(255, 255, 255, 1),\n                 fontWeight: FontWeight.bold,\n               ),\n               textAlign: TextAlign.center,\n             ),\n           ),\n           duration: Duration(seconds: 1),\n           height: double.infinity,\n           width: double.infinity,\n           \/\/ color property takes it\u2019s value from the dynamic _color var\n           color: _color,\n         ),\n       ),\n     );\n   }\n }}\n\n<\/code><\/pre>\n\n\n\n<h3 id=\"animatedcontainer-layout-widget\" class=\"wp-block-heading\">AnimatedContainer: (layout widget)<\/h3>\n\n\n\n<p>This widget is pretty similar to the one that we used on the previous version of this small app, but the special thing about this one is that its properties can be animated.<\/p>\n\n\n\n<p>You can see how we are using the <strong><em>_color<\/em><\/strong> variable to change the container color, and that change is going to have an effect anytime the user presses a button and changes the question.<\/p>\n\n\n\n<p>This is one approach but with any gesture or button to enumerate some, you can dynamically change the <strong><em>AnimatedContainer<\/em><\/strong>properties and create some cool animations for your app.<\/p>\n\n\n\n<p>So this is our app now. Its design isn\u2019t state-of-the-art, but at least we have some color:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets-global.website-files.com\/5d9bc5d562ffc22c37470958\/60d205eadc0531c7968f6a07_-tTAweuVz0CB1VVDIkweU1KhsSRkKCnDhYVCFdmSWr-nANcjJPir-71ynUqh-ECGzLe_lSex-4xJu1RCvSOKShZoe_Qkdd-gPpf49NaISSGmOW21ZHFUti5Gph1W-Zcr222rA-I1.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Finally, let&#8217;s modify those buttons a little bit.<\/p>\n\n\n\n<p>Since we now want to implement some custom buttons, it&#8217;s a good idea to create a custom widget that holds its shape and style. With that, we have its configuration in just one place, and we can re-use it anywhere.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport 'package:flutter\/material.dart';\n\nclass CircularButton extends StatelessWidget {\n final Function onPress;\n final String text;\n final String value;\n\n CircularButton(this.onPress, this.text, this.value);\n\n @override\n Widget build(BuildContext context) {\n   return Container(\n     child: RawMaterialButton(\n       onPressed: () =&gt; onPress(value),\n       child: Text(text),\n       fillColor: Colors.cyan&#91;100],\n       splashColor: Colors.white,\n       padding: EdgeInsets.all(70),\n       shape: CircleBorder(),\n       textStyle: TextStyle(\n         color: Colors.blueGrey&#91;900],\n         fontSize: 20,\n       ),\n     ),\n     margin: EdgeInsets.all(10),\n   );\n }\n}\n\n<\/code><\/pre>\n\n\n\n<p>The above code creates the StatelessWidget <strong><em>CircularButton<\/em><\/strong> that receives three positional arguments (onPress, text, value), which will be used to configure the dynamic widget\u2019s properties.<\/p>\n\n\n\n<h3 id=\"rawmaterialbutton-ui-widget\" class=\"wp-block-heading\">RawMaterialButton: (Ui widget)<\/h3>\n\n\n\n<p>This creates a button based on the material Button widgets. You can configure several properties to create your own custom button. With the code above, we created a circular button by setting its shape as <strong><em>CircleBorder<\/em><\/strong> and adding a big value for the <strong><em>padding<\/em><\/strong> property.<\/p>\n\n\n\n<p>Now we can import this button into our main file and replace the <strong><em>ElevatedButton<\/em><\/strong> with our custom button widget.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n@override\n Widget build(BuildContext context) {\n   if (_questionIndex &lt; 3) {\n     return MaterialApp(\n       home: Scaffold(\n         appBar: AppBar(title: Text(\"Personality Test\")),\n         body: AnimatedContainer(\n           child: Stack(\n             children: &#91;\n               Positioned(\n                 child: Question(_questions&#91;_questionIndex]),\n                 left: 40,\n                 top: 34.0,\n               ),\n               Positioned(\n                 child: CircularButton(\n                   _answerQuestion,\n                   _answers&#91;_questionIndex]&#91;0],\n                   _answers&#91;_questionIndex]&#91;0],\n                 ),\n                 left: 24.0,\n                 top: 325.0,\n               ),\n               Positioned(\n                 child: CircularButton(\n                   _answerQuestion,\n                   _answers&#91;_questionIndex]&#91;1],\n                   _answers&#91;_questionIndex]&#91;1],\n                 ),\n                 left: 150.0,\n                 top: 470.0,\n               ),\n               Positioned(\n                 child: CircularButton(\n                   _answerQuestion,\n                   _answers&#91;_questionIndex]&#91;2],\n                   _answers&#91;_questionIndex]&#91;2],\n                 ),\n                 left: 150.0,\n\n  \n               top: 180.0,\n               ),\n             ],\n           ),\n           duration: Duration(seconds: 1),\n           height: double.infinity,\n           width: double.infinity,\n           color: _color,\n         ),\n       ),\n     );\n   }\n\n<\/code><\/pre>\n\n\n\n<p>Besides using the new button component, we have changed the parent layout widget. Now we are making use of <strong><em>Stack<\/em><\/strong>, and like its direct children, we are using the <strong><em>Positioned<\/em><\/strong> widget. The combination of these two will let us position some elements in an \u201cabsolute\u201d way.<\/p>\n\n\n\n<h3 id=\"stack-layout-widget\" class=\"wp-block-heading\">Stack: (layout widget)<\/h3>\n\n\n\n<p>Unlike column, row, or other container-based widgets, Stack will allow the creation of a view with overlapping widgets. It will accept a list of children widgets to define its size, but you can easily override that behavior and configure it to expand and take the full space of its parent container.<\/p>\n\n\n\n<p>By default, all widgets will be positioned at the Stack\u2019s topStart corner.<\/p>\n\n\n\n<h3 id=\"positioned-layout-widget\" class=\"wp-block-heading\">Positioned: (layout widget)<\/h3>\n\n\n\n<p>A widget that controls where a child of a Stack is positioned.<\/p>\n\n\n\n<p>With the use of its position properties (top, right, bottom, left), we can position its children relative to the Stack Widget.<\/p>\n\n\n\n<p>With the above changes now our app looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets-global.website-files.com\/5d9bc5d562ffc22c37470958\/60d206873a8b400c665ee948_fMr3uaIMOFQPaaDy3XIb4vxBMYqBvQPWmteq5dBBXQm7qR2Mtb3d1T5O-wnI3UKq52zSk6gMxxW3Xzko4KYsjU5vFxUGn3mwFv67lWfo93RofPe546gEsK-7lUlIaJ4fhjj5tdOW.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>As you can see, the composition is all inside Flutter. We are constantly nesting widgets. Flutter comes with a long, long, long list of widgets that you can use and mix to create your custom ones. In this article, we tackle only some of the most basic ones.<\/p>\n\n\n\n<p>This is only one small piece of Flutter. The path is long and there are plenty of topics to learn about Flutter:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Theming<\/li>\n\n\n\n<li>State Management (Providers)<\/li>\n\n\n\n<li>Adaptive Widgets (depending on OS widgets)<\/li>\n\n\n\n<li>Routing<\/li>\n\n\n\n<li>Forms and user input<\/li>\n\n\n\n<li>Authentication and HTTP calls<\/li>\n\n\n\n<li>And many more<\/li>\n<\/ul>\n\n\n\n<p>Flutter is a growing technology nowadays and is winning an important place in the development world, so go ahead and keep learning about it!<\/p>\n\n\n\n<p>If you\u2019re interested in working with Flutter and helping companies develop their Flutter projects, visit <a href=\"https:\/\/www.fullstacklabs.co\/careers\" target=\"_blank\" rel=\"noreferrer noopener\">our careers page<\/a>. Also, if you\u2019re a leader looking to develop your Flutter project but need some expert resources, check out our <a href=\"https:\/\/www.fullstacklabs.co\/flutter-developers\" target=\"_blank\" rel=\"noreferrer noopener\">Flutter Developers<\/a> page to learn more about our available talent.<\/p>\n","protected":false},"excerpt":{"rendered":"Flutter is a growing mobile framework developed by Google, designed to help in the development of mobile and&hellip;\n","protected":false},"author":1,"featured_media":82,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0,"footnotes":""},"categories":[14,4],"tags":[],"class_list":{"0":"post-81","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-flutter","8":"category-programming","9":"cs-entry","10":"cs-video-wrap"},"_links":{"self":[{"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":1,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/posts\/81\/revisions\/83"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/media\/82"}],"wp:attachment":[{"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.aryapurwadana.com\/index.php\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}