{"id":32621,"date":"2024-11-01T09:10:23","date_gmt":"2024-11-01T09:10:23","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32621"},"modified":"2024-11-01T11:54:34","modified_gmt":"2024-11-01T11:54:34","slug":"flutter-course-structuring-the-basic-code-of-main-dart-file-8-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32621\/","title":{"rendered":"Flutter Course: Structuring the Basic Code of main.dart File 8.2"},"content":{"rendered":"<p><body><\/p>\n<p>Flutter is an open-source UI software development kit (SDK) developed by Google, which allows for rapid building of mobile, web, and desktop applications. One of the appeals of Flutter is its ability to build applications for multiple platforms from a single codebase. In this course, we will examine the basic code structure of the <code>main.dart<\/code> file, which is central to Flutter applications.<\/p>\n<h2>1. Role of the main.dart File<\/h2>\n<p>\nThe <code>main.dart<\/code> file is the entry point of a Flutter application. It is the first file executed when the application runs, and within this file, the main structure and UI of the application are defined.<br \/>\n        <br \/>\n<strong>Main Roles:<\/strong><\/p>\n<ul>\n<li>Application setup and initialization<\/li>\n<li>Configuration of the top-level widget<\/li>\n<li>Declaration of the root widget<\/li>\n<\/ul>\n<h3>1.1 Basic Structure<\/h3>\n<p>The basic <code>main.dart<\/code> file starts with the following structure:<\/p>\n<pre>\n        <code>\n        import 'package:flutter\/material.dart';\n\n        void main() {\n            runApp(MyApp());\n        }\n\n        class MyApp extends StatelessWidget {\n            @override\n            Widget build(BuildContext context) {\n                return MaterialApp(\n                    title: 'Flutter Course',\n                    home: Scaffold(\n                        appBar: AppBar(\n                            title: Text('Main Page'),\n                        ),\n                        body: Center(\n                            child: Text('Hello, Flutter!'),\n                        ),\n                    ),\n                );\n            }\n        }\n        <\/code>\n        <\/pre>\n<h2>2. Code Components<\/h2>\n<p>Let&#8217;s analyze the example code above and look at the roles of each part.<\/p>\n<h3>2.1 Importing Necessary Packages<\/h3>\n<p>We import the packages necessary to use Flutter&#8217;s features. The <code>flutter\/material.dart<\/code> package provides Material Design UI components, allowing for easy routing and state management.<\/p>\n<h3>2.2 main() Function<\/h3>\n<p>The <code>main()<\/code> function, which is the entry point of the Flutter application, executes the root widget through the <code>runApp()<\/code> function. Here, we instantiate and run the <code>MyApp<\/code> class.<\/p>\n<h3>2.3 StatelessWidget<\/h3>\n<p>The <code>MyApp<\/code> class inherits from <code>StatelessWidget<\/code>. This means it does not have state and is suitable for cases where the data used to draw the UI does not change.<\/p>\n<h3>2.4 build() Method<\/h3>\n<p>The <code>build()<\/code> method is responsible for creating the widget&#8217;s UI and takes <code>BuildContext<\/code> as a parameter. Within the <code>build()<\/code> method, widgets like <code>MaterialApp<\/code> and <code>Scaffold<\/code> are defined. The <code>Scaffold<\/code> widget provides a basic app layout.<\/p>\n<h2>3. Improving the Example Application<\/h2>\n<p>While you can create a simple application using the basic code, it can be improved by adding more features. Below is an example that adds a button to the basic application, which changes the text when clicked.<\/p>\n<pre>\n        <code>\n        import 'package:flutter\/material.dart';\n\n        void main() {\n            runApp(MyApp());\n        }\n\n        class MyApp extends StatefulWidget {\n            @override\n            _MyAppState createState() => _MyAppState();\n        }\n\n        class _MyAppState extends State<MyApp> {\n            String message = 'Hello, Flutter!';\n\n            void _changeMessage() {\n                setState(() {\n                    message = 'Button has been clicked!';\n                });\n            }\n\n            @override\n            Widget build(BuildContext context) {\n                return MaterialApp(\n                    title: 'Flutter Course',\n                    home: Scaffold(\n                        appBar: AppBar(\n                            title: Text('Main Page'),\n                        ),\n                        body: Center(\n                            child: Column(\n                                mainAxisAlignment: MainAxisAlignment.center,\n                                children: <Widget>[\n                                    Text(message),\n                                    SizedBox(height: 20),\n                                    ElevatedButton(\n                                        onPressed: _changeMessage,\n                                        child: Text('Change Message'),\n                                    ),\n                                ],\n                            ),\n                        ),\n                    ),\n                );\n            }\n        }\n        <\/code>\n        <\/pre>\n<h3>3.1 Adding StatefulWidget<\/h3>\n<p>In the example above, we used <code>StatefulWidget<\/code> instead of <code>StatelessWidget<\/code> since the state can change. <code>StatefulWidget<\/code> has internal state and is used when the UI may change based on that state.<\/p>\n<h3>3.2 setState() Method<\/h3>\n<p>When the button is clicked, we call the <code>_changeMessage()<\/code> method to update the state and use <code>setState()<\/code> to redraw the UI. This allows us to create a dynamically responsive UI for the application.<\/p>\n<h2>4. Widget Structure of Flutter<\/h2>\n<p>The UI in Flutter consists of widgets. Every UI element is represented as a widget, and widgets can be combined to form complex UIs. Widgets can be broadly divided into two categories:<\/p>\n<ul>\n<li><strong>StatelessWidget:<\/strong> A widget that does not change its state and has a static UI<\/li>\n<li><strong>StatefulWidget:<\/strong> A widget that has an internal state and can change its UI based on that state<\/li>\n<\/ul>\n<h2>5. Flutter Project Structure<\/h2>\n<p>A Flutter project is composed of several directories and files. The basic file structure generated is as follows:<\/p>\n<ul>\n<li><code>lib\/<\/code> &#8211; Contains commonly used Dart files<\/li>\n<li><code>pubspec.yaml<\/code> &#8211; Dependency and resource management file<\/li>\n<li><code>android\/<\/code> &#8211; Android-related settings and code<\/li>\n<li><code>ios\/<\/code> &#8211; iOS-related settings and code<\/li>\n<\/ul>\n<h2>6. Conclusion<\/h2>\n<p>In this course, we examined the basic structure and components of the <code>main.dart<\/code> file in Flutter. The application runs through the <code>main.dart<\/code> file, and we can create the UI through a combination of widgets. We also learned how to build dynamic applications utilizing <code>StatelessWidget<\/code> and <code>StatefulWidget<\/code>. This foundational course will be a great help when structuring more complex UIs in the future.<\/p>\n<p>Continue learning for more information and in-depth courses on Flutter! In the next session, we will explore more widgets and advanced features of Flutter.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flutter is an open-source UI software development kit (SDK) developed by Google, which allows for rapid building of mobile, web, and desktop applications. One of the appeals of Flutter is its ability to build applications for multiple platforms from a single codebase. In this course, we will examine the basic code structure of the main.dart &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32621\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Flutter Course: Structuring the Basic Code of main.dart File 8.2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[151],"tags":[],"class_list":["post-32621","post","type-post","status-publish","format-standard","hentry","category-flutter-course"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Flutter Course: Structuring the Basic Code of main.dart File 8.2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32621\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Flutter Course: Structuring the Basic Code of main.dart File 8.2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Flutter is an open-source UI software development kit (SDK) developed by Google, which allows for rapid building of mobile, web, and desktop applications. One of the appeals of Flutter is its ability to build applications for multiple platforms from a single codebase. In this course, we will examine the basic code structure of the main.dart &hellip; \ub354 \ubcf4\uae30 &quot;Flutter Course: Structuring the Basic Code of main.dart File 8.2&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32621\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:10:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:54:34+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32621\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32621\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Flutter Course: Structuring the Basic Code of main.dart File 8.2\",\"datePublished\":\"2024-11-01T09:10:23+00:00\",\"dateModified\":\"2024-11-01T11:54:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32621\/\"},\"wordCount\":565,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Flutter course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32621\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32621\/\",\"name\":\"Flutter Course: Structuring the Basic Code of main.dart File 8.2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:10:23+00:00\",\"dateModified\":\"2024-11-01T11:54:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32621\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32621\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32621\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Flutter Course: Structuring the Basic Code of main.dart File 8.2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Flutter Course: Structuring the Basic Code of main.dart File 8.2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/32621\/","og_locale":"ko_KR","og_type":"article","og_title":"Flutter Course: Structuring the Basic Code of main.dart File 8.2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Flutter is an open-source UI software development kit (SDK) developed by Google, which allows for rapid building of mobile, web, and desktop applications. One of the appeals of Flutter is its ability to build applications for multiple platforms from a single codebase. In this course, we will examine the basic code structure of the main.dart &hellip; \ub354 \ubcf4\uae30 \"Flutter Course: Structuring the Basic Code of main.dart File 8.2\"","og_url":"https:\/\/atmokpo.com\/w\/32621\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:10:23+00:00","article_modified_time":"2024-11-01T11:54:34+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32621\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32621\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Flutter Course: Structuring the Basic Code of main.dart File 8.2","datePublished":"2024-11-01T09:10:23+00:00","dateModified":"2024-11-01T11:54:34+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32621\/"},"wordCount":565,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Flutter course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32621\/","url":"https:\/\/atmokpo.com\/w\/32621\/","name":"Flutter Course: Structuring the Basic Code of main.dart File 8.2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:10:23+00:00","dateModified":"2024-11-01T11:54:34+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32621\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32621\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32621\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Flutter Course: Structuring the Basic Code of main.dart File 8.2"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32621"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32621\/revisions"}],"predecessor-version":[{"id":32622,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32621\/revisions\/32622"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}