{"id":781,"date":"2023-08-14T16:13:28","date_gmt":"2023-08-14T13:13:28","guid":{"rendered":"https:\/\/barisuslucan.com\/en\/?p=781"},"modified":"2023-08-14T16:13:28","modified_gmt":"2023-08-14T13:13:28","slug":"the-importance-of-modifier-order-in-jetpack-compose","status":"publish","type":"post","link":"https:\/\/barisuslucan.com\/en\/kotlin\/the-importance-of-modifier-order-in-jetpack-compose\/","title":{"rendered":"The Importance of Modifier Order in Jetpack Compose"},"content":{"rendered":"<p>Jetpack Compose&#8217;s &#8216;Modifier&#8217; is a foundational building block. These modifiers are used to after the behavior, size, positioning and appearance of user interface components. However, the order of modifiers can have a significant impact on how a component looks and behaves. In this article, we&#8217;ll explore why the order of modifiers is so crucial and how this ordering works.<br \/><br \/><\/p>\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\" Box(\n        modifier = Modifier\n            .fillMaxSize()\n            .padding(30.dp)\n            .background(color = Color.Blue)\n    ) {\n\n    }\n    \n Box(\n        modifier = Modifier\n            .fillMaxSize()\n            .background(color = Color.Blue)\n            .padding(30.dp)\n    ) {\n\n    }\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">Box<\/span><span style=\"color: #D8DEE9FF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">modifier<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Modifier<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">fillMaxSize<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">padding<\/span><span style=\"color: #D8DEE9FF\">(30<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">dp<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">background<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">color<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Color<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Blue<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    ) <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">Box<\/span><span style=\"color: #D8DEE9FF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">modifier<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Modifier<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">fillMaxSize<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">background<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">color<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Color<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">Blue<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">padding<\/span><span style=\"color: #D8DEE9FF\">(30<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">dp<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    ) <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n<h4><strong><br \/>What&#8217;s the Difference?\u00a0<\/strong><\/h4>\n<p>\u00a0<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-788 size-large\" src=\"https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/08\/Jetpack-Compose-Modifiers-1024x576.jpg\" alt=\"Jetpack Compose Modifiers\" width=\"1024\" height=\"576\" srcset=\"https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/08\/Jetpack-Compose-Modifiers-1024x576.jpg 1024w, https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/08\/Jetpack-Compose-Modifiers-300x169.jpg 300w, https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/08\/Jetpack-Compose-Modifiers-768x432.jpg 768w, https:\/\/barisuslucan.com\/en\/wp-content\/uploads\/2023\/08\/Jetpack-Compose-Modifiers.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>\u00a0<\/p>\n<p>At the first glance, both &#8220;Box&#8221; components might seem similar. However, due to the order of modifiers, there is a significant difference in how these two &#8216;Box&#8217; components appear.<\/p>\n<ol>\n<li><strong>First Box:<\/strong> it starts by filling the available space with &#8216;fillMaxSize()&#8217;. Then, it moves away from its content by 30 dp with &#8216;padding(30.dp)&#8217;. Finally, it adds a blue background with &#8216;background(color = Color.Blue)&#8217;. In this scenario, the blue background will be outside the padding, covering the entire box size.<\/li>\n<li><strong>Second Box:<\/strong> Again, it fills the available space with &#8216;fillMaxSize()&#8217;. But this time, it first adds a blue background with &#8216;background(color = Color.Blue)&#8217; and then moves away from its content by 30 dp with &#8216;padding(30.dp)&#8217;. Here the blue background will only cover the box&#8217;s content, and the padding will be inside the blue background.<\/li>\n<\/ol>\n<h5>\u00a0<\/h5>\n<h5><strong>Another example: Scrolling<\/strong><\/h5>\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"@Composable\nfun HomeScreen() {\n    val scrollState = rememberScrollState()\n    Column(\n        modifier = Modifier\n            .fillMaxSize()\n            .verticalScroll(scrollState)\n            .padding(100.dp)\n\n    ) {\n        for(i in 0 until 100) {\n            Text(&quot;Jetpack Compose&quot;)\n        }\n\n    }\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D08770\">@Composable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9\">fun<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">HomeScreen<\/span><span style=\"color: #D8DEE9FF\">() <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #D8DEE9\">val<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">scrollState<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">rememberScrollState<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">Column<\/span><span style=\"color: #D8DEE9FF\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #D8DEE9\">modifier<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">Modifier<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">fillMaxSize<\/span><span style=\"color: #D8DEE9FF\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">verticalScroll<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">scrollState<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">padding<\/span><span style=\"color: #D8DEE9FF\">(100<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #D8DEE9\">dp<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    ) <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #D8DEE9\">i<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">until<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">100<\/span><span style=\"color: #D8DEE9FF\">) <\/span><span style=\"color: #ECEFF4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">            <\/span><span style=\"color: #88C0D0\">Text<\/span><span style=\"color: #D8DEE9FF\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Jetpack Compose<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n<p><br \/>When &#8216;verticalScroll(scrollState)&#8217; is applied before &#8216;padding(100.dp)&#8217;, it means that the entire &#8216;Column&#8217;, including the padded areas, becomes scrollable. In other words, you can control the scroll even from the padded spaces.<\/p>\n<p>However, if the &#8216;scrollState&#8217; were applied after the &#8216;padding&#8217;, the scroll control would be limited only the content area, excluding the padded spaces. The padded areas would remain static and wouldn&#8217;t respond to scroll gestures.<\/p>\n<h5><strong>Conclusion:<\/strong><\/h5>\n<p>The order of modifiers in Jetpack Compose is not jus about aesthetics; it can also dictate the behavior and interaction of your UI components. Being mindful of this order can help you achieve the desired user experience. Whether you&#8217;re aiming for a specific visual appearance or a particular behavior like scrolling, the sequence of your modifiers plays a pivotal role. Always test the behavior after rearranging modifiers to ensure it aligns with your intended design and functionality.<\/p>","protected":false},"excerpt":{"rendered":"<p>Jetpack Compose&#8217;s &#8216;Modifier&#8217; is a foundational building block. These modifiers are used to after the behavior, size, positioning and appearance of user interface components. However, the order of modifiers can have a significant impact on how a component looks and behaves. In this article, we&#8217;ll explore why the order of modifiers is so crucial and how this ordering works. What&#8217;s the Difference?\u00a0 \u00a0 \u00a0 At the first glance, both &#8220;Box&#8221; components might seem similar. However, due to the order of modifiers, there is a significant difference in how these two &#8216;Box&#8217; components appear. First Box: it starts by filling the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":789,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[29],"tags":[],"class_list":["post-781","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/comments?post=781"}],"version-history":[{"count":4,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/781\/revisions"}],"predecessor-version":[{"id":790,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/posts\/781\/revisions\/790"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/media\/789"}],"wp:attachment":[{"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/media?parent=781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/categories?post=781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barisuslucan.com\/en\/wp-json\/wp\/v2\/tags?post=781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}