<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[yushosei]]></title><description><![CDATA[yushosei]]></description><link>https://yushosei.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 01:25:03 GMT</lastBuildDate><atom:link href="https://yushosei.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Flutter Is  Garbage]]></title><description><![CDATA[Why I prefer Jetpack Compose for Android: readability, modifiers, local state, layout constraints, and native integration compared with Flutter.]]></description><link>https://yushosei.hashnode.dev/flutter-is-garbage</link><guid isPermaLink="true">https://yushosei.hashnode.dev/flutter-is-garbage</guid><category><![CDATA[Flutter]]></category><category><![CDATA[Jetpack Compose]]></category><dc:creator><![CDATA[yushosei]]></dc:creator><pubDate>Fri, 27 Nov 2026 06:23:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/uploads/covers/63f7323ea4069fe3b3e954f2/cea937aa-dfb7-4cb3-855d-4ff66628ea6c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When I read UI code, I want to understand a few simple things: what the screen shows, which state it responds to, and what the user can do.</p>
<p>Having to work through layers of <code>child</code> properties, constructors, and layout widgets before I can see that intent gets tiring. This is where I feel the biggest difference between Flutter and Jetpack Compose. Both offer declarative UI, but reading and changing the code can feel very different.</p>
<p>This post explains why I prefer Compose's approach for Android UI development. My focus is the code I have to work with every day and the effort involved in changing it, rather than platform counts or benchmark scores.</p>
<p><img src="https://cdn.hashnode.com/uploads/covers/63f7323ea4069fe3b3e954f2/f53fc420-d741-47d7-b043-75e629643e4b.png" alt="Flutter code panel and counter transformed into a gold-and-rainbow jackpot spectacle, with a lion, white tiger, goddess, robots, and Flutter mascots." /></p>
<p><em>Artwork: AI-stylized adaptation of the <a href="https://flutter.dev/assets/productive.986b33c335308b28c1e239c93a23614c.webp">official Flutter “Productive” illustration</a>, from <a href="https://flutter.dev/">flutter.dev</a>, under <a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>.</em></p>
<h2>Everything is a widget. Is that easier to read?</h2>
<p>In Flutter, visible elements, spacing, alignment, and size constraints are all composed using widgets. That is the core model described in the <a href="https://docs.flutter.dev/ui/layout">official layout documentation</a>. Building complex interfaces from small pieces is an understandable idea.</p>
<p>The friction appears as the code grows. Add spacing, alignment, sizing, and click handling around a piece of text, and structural relationships become mixed with visual decoration in the same nested tree. While reading it, I have to keep distinguishing the layers that describe the screen's composition from the layers that apply a property or behavior.</p>
<p>Extracting smaller widgets can help. Giving reusable pieces names and separating responsibilities is good design. But creating more tiny, single-use widgets just to make nesting bearable has a cost too. Now I have more names and locations to follow.</p>
<p>I care about how much code I have to navigate before I understand what a screen is trying to do. Line count alone tells me very little about that.</p>
<h2>Modifiers fit the way I describe a UI</h2>
<p>One thing I like about Compose is how it lets me express an element and the transformations applied to it. <a href="https://developer.android.com/develop/ui/compose/modifiers">Modifier</a> provides a common way to combine sizing, spacing, backgrounds, input handling, and other behavior.</p>
<p>Consider a piece of text with outer spacing, a blue background, and inner padding. These short snippets compare how the intent is expressed; imports and surrounding theme setup are omitted.</p>
<p>Flutter:</p>
<pre><code class="language-dart">Container(
  margin: const EdgeInsets.all(16),
  padding: const EdgeInsets.symmetric(
    horizontal: 12,
    vertical: 8,
  ),
  decoration: const BoxDecoration(color: Colors.blue),
  child: const Text('Hello'),
)
</code></pre>
<p>Jetpack Compose:</p>
<pre><code class="language-kotlin">Text(
    text = "Hello",
    modifier = Modifier
        .padding(16.dp)
        .background(Color.Blue)
        .padding(horizontal = 12.dp, vertical = 8.dp)
)
</code></pre>
<p>Flutter's <code>Container</code> can group these properties in one place. A fair comparison should acknowledge that convenience instead of manufacturing a deeply nested Flutter example to make Compose look shorter.</p>
<p>What I prefer is being able to use the same <code>Modifier</code> syntax across different UI elements. Styling text, sizing a row, and handling input follow a similar reading pattern. I can start with the element and then follow the behavior applied to it.</p>
<p>Modifier order matters, though. Swapping <code>clickable</code> and <code>padding</code> can change the clickable area. That is something developers need to learn. I find it easier to reason about this ordering when it is expressed directly in the chain.</p>
<h2>Small pieces of state need less setup</h2>
<p>I feel a similar difference when working with local UI state. In Compose, <code>remember</code> and <code>mutableStateOf</code> let me declare state close to the UI and event handlers that use it. A small interaction is easy to follow within a function.</p>
<p>Flutter's <code>StatefulWidget</code>, <code>State</code>, and <code>setState</code> form a clear model too. The <a href="https://docs.flutter.dev/data-and-backend/state-mgmt/ephemeral-vs-app">Flutter documentation</a> explains that these built-in tools are sufficient for local state. A separate state management package is not a requirement.</p>
<p>Still, the standard structure of moving between a widget class and its State class feels like extra ceremony for a small piece of state. Compose's function-based approach gives me a lighter starting point.</p>
<p>Shared state still requires design in Compose. I have to think about <a href="https://developer.android.com/develop/ui/compose/state#state-hoisting">state hoisting</a>, ownership, event flow, and lifecycle. <code>remember</code> alone does not preserve state through activity recreation, either. I prefer the way Compose lets me start with a small interaction and add structure as the requirements grow.</p>
<h2>Layout complexity does not disappear</h2>
<p>Understanding Flutter layout means understanding the constraints a parent passes down and how children choose their sizes. The <a href="https://docs.flutter.dev/ui/layout/constraints">official constraints guide</a> explains these rules in detail. Giving a widget a width does not guarantee that it will end up with that width.</p>
<p>That alone would be an unfair criticism of Flutter. Compose also lays out elements under constraints from their parents. <code>Modifier.size</code> does not let an element ignore every incoming constraint, as the <a href="https://developer.android.com/develop/ui/compose/modifiers#padding-and-size">Compose documentation</a> explains.</p>
<p>The difference I care about is how I trace those rules through the code. In Flutter, I follow how wrapper widgets affect the constraints. In Compose, I inspect the layout and the order of its modifiers. Both require understanding, but the latter is easier for me to work through.</p>
<p>This affects everyday changes: adjusting spacing, changing a touch target, or figuring out why something occupies more space than expected. The effort of reading that structure adds up.</p>
<h2>For Android development, I prefer staying close to Android</h2>
<p>For an Android-only app, I also like working with Kotlin code and the Android SDK in the same project environment. It reduces the boundaries I need to consider when adding platform features.</p>
<p>In Flutter, a plugin can make a supported feature straightforward to use. When I need a custom native integration, tools such as <a href="https://docs.flutter.dev/platform-integration/platform-channels">platform channels and Pigeon</a> are available. With platform channels, I also have to maintain the contract between the Dart call and its platform implementation, including data transfer and error handling.</p>
<p>Being able to build a feature and having little overhead in maintaining that feature are separate considerations. When Android is the only target, I want a clear reason to introduce that additional boundary.</p>
<p>The scope of this comparison matters. Flutter offers UI and code sharing across platforms. Ignoring that value while comparing it with Jetpack Compose for Android would tilt the decision from the start. This post focuses on writing Android UI; a broader choice involving Compose Multiplatform needs its own evaluation.</p>
<h2>I want UI code that is easy to change</h2>
<p>When Flutter's code sharing reduces a team's work, that is valuable. A product that benefits from shared UI and has compatible platform requirements can have a strong reason to choose it.</p>
<p>My priorities are code that reveals its intent quickly, fewer layers to navigate for a small change, and fewer boundaries to manage when connecting platform features. For Android development, those priorities lead me toward Jetpack Compose.</p>
<p>That is why Flutter's widget-heavy style frustrates me. A consistent conceptual model does not automatically make the code pleasant to read and edit every day. The label “declarative UI” leaves plenty of room for differences in the actual development experience.</p>
<p>Getting a screen to render is the starting point. I also want to be able to read it comfortably and change it next month.</p>
]]></content:encoded></item><item><title><![CDATA[Jetpack Compose - style Extensions for Flutter]]></title><description><![CDATA[Explore how to simplify your Flutter UI development by leveraging Dart's extension methods to achieve a Jetpack Compose-style syntax.]]></description><link>https://yushosei.hashnode.dev/jetpack-compose-style-extensions-for-flutter</link><guid isPermaLink="true">https://yushosei.hashnode.dev/jetpack-compose-style-extensions-for-flutter</guid><category><![CDATA[Flutter]]></category><category><![CDATA[Jetpack Compose]]></category><dc:creator><![CDATA[yushosei]]></dc:creator><pubDate>Sun, 11 Jun 2023 08:57:24 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1686473960095/57acf586-438a-405e-a878-3a0688b468f4.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello Developers! Welcome back to our tech blog. As a Jetpack Compose developer, I've always admired its clean, lean, and intuitive approach to crafting UI components. However, recently, I decided to venture into the Flutter development landscape and found myself battling with the intricacies of its widget architecture.</p>
<p>While Flutter provides powerful tools and flexibility with its extensive widget system, its syntax, for someone accustomed to Jetpack Compose, can be a bit jarring. The biggest hurdle I encountered was dealing with deeply nested widgets wrapped within one another, often leading to a daunting pile of closing braces.</p>
<p>This verbosity in code, especially when creating complex UI, can impact readability and maintenance, resulting in an awkward development experience. On the other hand, Jetpack Compose, with its declarative nature and modifier-based system, offers an elegant way to layer attributes onto composable functions, avoiding the pitfalls of nested structures.</p>
<p>Jetpack Compose's clean and unambiguous syntax makes reading and writing UI code a more streamlined process. For instance, to add padding to a Composable or align it within its parent, you would chain these actions using a modifier, leading to a linear and more readable code.</p>
<p>In Flutter, to achieve similar functionality, we typically wrap our widget within a parent widget responsible for that attribute, like Padding() or Align(). While this approach works and offers a high degree of customization, it leads to deeply nested structures and sometimes a non-intuitive reading order of the code.</p>
<p>But don't worry! If, like me, you're yearning for the simplicity of Jetpack Compose in your Flutter development, I've got some good news. Thanks to Dart's support for extension methods, we can introduce a Jetpack Compose-style syntax into our Flutter projects, and today, we're going to explore just that.</p>
<p>Let's look at an example. Consider the following code:</p>
<pre><code class="lang-dart">Text(<span class="hljs-string">'Hello, World!'</span>)
  .padding(<span class="hljs-keyword">const</span> EdgeInsets.all(<span class="hljs-number">16</span>))
  .background(Colors.blue)
  .align(Alignment.bottomLeft)
  .expanded(<span class="hljs-number">2</span>)
  .aspectRatio(<span class="hljs-number">1.5</span>)
  .clickable(() {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">'Clicked!'</span>);
  });
</code></pre>
<p>This looks more streamlined and intuitive, right? It's almost as if we are writing Jetpack Compose code! Now, how does this compare to the traditional Flutter style?</p>
<pre><code class="lang-dart">GestureDetector(
  onTap: () {
    <span class="hljs-built_in">print</span>(<span class="hljs-string">'Clicked!'</span>);
  },
  child: AspectRatio(
    aspectRatio: <span class="hljs-number">1.5</span>,
    child: Expanded(
      flex: <span class="hljs-number">2</span>,
      child: Align(
        alignment: Alignment.bottomLeft,
        child: DecoratedBox(
          decoration: BoxDecoration(color: Colors.blue),
          child: Padding(
            padding: <span class="hljs-keyword">const</span> EdgeInsets.all(<span class="hljs-number">16</span>),
            child: Text(<span class="hljs-string">'Hello, World!'</span>),
          ),
        ),
      ),
    ),
  ),
)
</code></pre>
<p>It's the same result but more nested and verbose. This extension-based method allows you to chain widget transformations, reducing unnecessary widget nesting and improving readability.</p>
<p>So, How to Compose-like coding style in Flutter? It's none other than Dart's extension methods. Below, you can find the definition for these some Jetpack Compose style extensions:</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:flutter/material.dart'</span>;

<span class="hljs-keyword">extension</span> WidgetModifier <span class="hljs-keyword">on</span> Widget {
  Widget padding([EdgeInsetsGeometry value = <span class="hljs-keyword">const</span> EdgeInsets.all(<span class="hljs-number">8.0</span>)]) {
    <span class="hljs-keyword">return</span> Padding(
      padding: value,
      child: <span class="hljs-keyword">this</span>,
    );
  }

  Widget background(Color color) {
    <span class="hljs-keyword">return</span> DecoratedBox(
      decoration: BoxDecoration(color: color),
      child: <span class="hljs-keyword">this</span>,
    );
  }

  Widget align([Alignment alignment = Alignment.center]) {
    <span class="hljs-keyword">return</span> Align(
      alignment: alignment,
      child: <span class="hljs-keyword">this</span>,
    );
  }

  Widget expanded([<span class="hljs-built_in">int</span> flex = <span class="hljs-number">1</span>]) {
    <span class="hljs-keyword">return</span> Expanded(
      flex: flex,
      child: <span class="hljs-keyword">this</span>,
    );
  }

  Widget aspectRatio([<span class="hljs-built_in">double</span> aspectRatio = <span class="hljs-number">1.0</span>]) {
    <span class="hljs-keyword">return</span> AspectRatio(
      aspectRatio: aspectRatio,
      child: <span class="hljs-keyword">this</span>,
    );
  }

  Widget clickable(VoidCallback onPressed) {
    <span class="hljs-keyword">return</span> GestureDetector(
      onTap: onPressed,
      child: <span class="hljs-keyword">this</span>,
    );
  }
}
</code></pre>
<p>These extensions give our widgets additional methods like <code>padding</code>, <code>background</code>, <code>align</code>, <code>expanded</code>, <code>aspectRatio</code>, and <code>clickable</code>, which we can chain together to achieve the desired widget configuration.</p>
<p>As we wrap up this post, it's essential to remember that this is just one method of approaching Flutter development. Depending on your personal or team's coding style and project requirements, you may prefer the traditional style, the Jetpack Compose style extension method, or even a blend of both. The key is to write clean, maintainable code that efficiently solves the task at hand.</p>
<p>The power of Dart and Flutter is in its flexibility, and this is just one example of how we can mold it to suit our needs better.</p>
<p>Thank you for reading! Don't forget to share this post if you found it helpful. Until next time, Happy Coding!</p>
]]></content:encoded></item><item><title><![CDATA[Jetpack Compose - Easy Sign in Button]]></title><description><![CDATA[Learn how to easily create a third-party signin button using Android Jetpack Compose with this tutorial and interactive demo. Get the source code!]]></description><link>https://yushosei.hashnode.dev/jetpack-compose-easy-sign-in-button</link><guid isPermaLink="true">https://yushosei.hashnode.dev/jetpack-compose-easy-sign-in-button</guid><category><![CDATA[Jetpack Compose]]></category><category><![CDATA[Android]]></category><dc:creator><![CDATA[yushosei]]></dc:creator><pubDate>Thu, 29 Dec 2022 13:02:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1677641623729/376ecb9d-1844-474d-84fb-fdfc38284ec1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Creating a third-party signin button can be a daunting task, but with <a target="_blank" href="https://developer.android.com/jetpack/compose">Jetpack Compose</a>, it's easier than ever to create beautiful and functional UI components for Android app.</p>
<p>In this tutorial, we'll be using Jetpack Compose to create a Sign in button that can be easily customized to fit your app's branding and style.</p>
<p>To start, let's take a look at the final product we'll be creating. The signin button will be a rounded rectangle with a custom icon and text label, as shown in the <strong>interactive demo application</strong> below :</p>
<iframe style="width:100%;height:400px;background-color:#0f172a" src="https://app.yushosei.com?page=login">
</iframe>

<p>Wonderful! Now, let's get into the code. First, we'll need to create a <strong>Composable</strong> function that will define the layout and appearance of the button. We'll start by creating a <code>MaterialComponent</code> and customizing it with the appropriate colors and shapes:</p>
<pre><code class="lang-kotlin"><span class="hljs-meta">@Composable</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">SignInButton</span><span class="hljs-params">(
    text: <span class="hljs-type">String</span>,
    loadingText: <span class="hljs-type">String</span> = <span class="hljs-string">"Signing in..."</span>,
    icon: <span class="hljs-type">Painter</span>,
    isLoading: <span class="hljs-type">Boolean</span> = <span class="hljs-literal">false</span>,
    borderColor: <span class="hljs-type">Color</span> = Color.LightGray,
    backgroundColor: <span class="hljs-type">Color</span> = Color.White,
    progressIndicatorColor: <span class="hljs-type">Color</span> = MaterialTheme.colors.primary,
    textStyle: <span class="hljs-type">TextStyle</span> = MaterialTheme.typography.button,
    onClick: () -&gt; <span class="hljs-type">Unit</span>
)</span></span> {
    Surface(
        modifier = Modifier.clickable(
            enabled = !isLoading,
            onClick = onClick
        ).clip(RoundedCornerShape(<span class="hljs-number">4</span>.dp)),
        shape = RoundedCornerShape(<span class="hljs-number">4</span>.dp),
        border = <span class="hljs-keyword">if</span> (borderColor != Color.Transparent)
            BorderStroke(width = <span class="hljs-number">1</span>.dp, color = borderColor)
        <span class="hljs-keyword">else</span>
            <span class="hljs-literal">null</span>,
        color = backgroundColor
    ) {
        Row(
            modifier = Modifier.wrapContentSize()
                .padding(<span class="hljs-number">16</span>.dp)
                .animateContentSize(
                    animationSpec = tween(
                        durationMillis = <span class="hljs-number">300</span>,
                        easing = LinearOutSlowInEasing
                    )
                ),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center,
        ) {
            Icon(
                painter = icon,
                contentDescription = <span class="hljs-string">"SignInButton"</span>,
                tint = Color.Unspecified,
                modifier = Modifier.size(<span class="hljs-number">20</span>.dp)
            )

            Spacer(modifier = Modifier.width(<span class="hljs-number">8</span>.dp))

            Text(
                text = <span class="hljs-keyword">if</span> (isLoading) loadingText <span class="hljs-keyword">else</span> text,
                style = textStyle
            )
            <span class="hljs-keyword">if</span> (isLoading) {
                Spacer(modifier = Modifier.width(<span class="hljs-number">16</span>.dp))
                CircularProgressIndicator(
                    modifier = Modifier
                        .height(<span class="hljs-number">16</span>.dp)
                        .width(<span class="hljs-number">16</span>.dp),
                    strokeWidth = <span class="hljs-number">3</span>.dp,
                    color = progressIndicatorColor
                )
            }
        }
    }
}
</code></pre>
<p>That's It! The <code>SignInButton</code> function code provided above contains the implementation of the Sign in button we've been working on. The function accepts several parameters, such as button text, icon, and styling options, to create a rounded rectangle button with a custom icon and label. The button has a loading state, indicated by a <code>CircularProgressIndicator</code> component, and it's implemented using the <code>Surface</code> composable. The <code>Row</code> and <code>Spacer</code> composables are used to create the layout of the button contents.</p>
<p>This code implements the <strong>demo application</strong> using this SignInButton function :</p>
<pre><code class="lang-kotlin"><span class="hljs-meta">@Composable</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">LoginDemo</span><span class="hljs-params">()</span></span> {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(
            <span class="hljs-number">16</span>.dp,
            Alignment.CenterVertically
        ),
    ) {
        <span class="hljs-keyword">var</span> isLoading <span class="hljs-keyword">by</span> remember { mutableStateOf(<span class="hljs-number">0</span>) }
        <span class="hljs-keyword">val</span> buttonStyle = MaterialTheme.typography.button.copy(
            fontWeight = FontWeight.Bold
        )

        Text(
            text = <span class="hljs-string">"Click it!"</span>,
            style = MaterialTheme.typography.h4.copy(
                fontWeight = FontWeight.Bold,
                color = Color.White
            )
        )

        Spacer(modifier = Modifier.height(<span class="hljs-number">16</span>.dp))

        SignInButton(
            text = <span class="hljs-string">"Sign in with Google"</span>,
            isLoading = isLoading == <span class="hljs-number">1</span>,
            icon = rememberVectorPainter(Icons.Google),
            textStyle = buttonStyle.copy(
                color = Color.Black
            ),
            onClick = {
                isLoading = <span class="hljs-number">1</span>
            })

        SignInButton(
            text = <span class="hljs-string">"Sign in with Twitter"</span>,
            isLoading = isLoading == <span class="hljs-number">2</span>,
            icon = rememberVectorPainter(Icons.Twitter),
            textStyle = buttonStyle.copy(
                color = Color.White
            ),
            onClick = {
                isLoading = <span class="hljs-number">2</span>
            },
            progressIndicatorColor = Color.White,
            backgroundColor = Color(<span class="hljs-number">0xFF2696ea</span>),
            borderColor = Color.Transparent
        )
    }
}
</code></pre>
<p>With just a few lines of code, we've created a beautiful and functional signin button that can be easily integrated into any Android app. Feel free to experiment with different colors, fonts, and shapes to create a button that <strong>matches your app's branding and style</strong>.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Congratulations, you've successfully created a third-party Sign in button using Jetpack Compose!</p>
<p>Remember, Jetpack Compose is a powerful tool that can help you streamline your app development process and create beautiful, functional UIs in <strong>less time than ever before</strong>. If you enjoyed this tutorial, be sure to check out my other articles on Jetpack Compose and Android development.</p>
<p>Thanks for reading, and <strong>happy coding!</strong></p>
]]></content:encoded></item><item><title><![CDATA[Jetpack Compose - Easy Button Animation]]></title><description><![CDATA[Learn how to create stunning animations using Android Jetpack Compose with this step-by-step tutorial. Interact with the demo app and access the source…]]></description><link>https://yushosei.hashnode.dev/jetpack-compose-easy-button-animation</link><guid isPermaLink="true">https://yushosei.hashnode.dev/jetpack-compose-easy-button-animation</guid><category><![CDATA[Jetpack Compose]]></category><category><![CDATA[Android]]></category><dc:creator><![CDATA[yushosei]]></dc:creator><pubDate>Tue, 06 Dec 2022 13:57:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1678016538262/ff706e5d-944d-4e44-9d2c-336c9ffc473f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello, Android developers! In this article, I'll be showing you how to create a cool animation using <a target="_blank" href="https://developer.android.com/jetpack/compose">Android Jetpack Compose</a>. Animations are a great way to enhance the user experience of your app. With Jetpack Compose, creating animations is easier than ever before. So let's get started!</p>
<p>To start, let me show you <strong>interactive demo applications</strong> to see what we'll be building. These demos will give you a clear idea of what the end product will look like and how it will function.</p>
<iframe style="width:100%;height:400px;background-color:#0f172a" src="https://app.yushosei.com?page=buttons">
</iframe>

<p>As you can see, the button animates smoothly when pressed, giving the user feedback and enhancing the overall experience.</p>
<p>Now that you've seen what we'll be building, let's take a look at the source code. I'll walk you through the steps to create the animation, explaining the code along the way.</p>
<pre><code class="lang-kotlin">
<span class="hljs-function"><span class="hljs-keyword">fun</span> Modifier.<span class="hljs-title">pressClickEffect</span><span class="hljs-params">()</span></span> = composed {
    <span class="hljs-keyword">var</span> isPressed <span class="hljs-keyword">by</span> remember { mutableStateOf(<span class="hljs-literal">false</span>) }
    <span class="hljs-keyword">val</span> ty <span class="hljs-keyword">by</span> animateFloatAsState(<span class="hljs-keyword">if</span> (isPressed) <span class="hljs-number">20f</span> <span class="hljs-keyword">else</span> <span class="hljs-number">0f</span>)

    <span class="hljs-keyword">this</span>
        .graphicsLayer {
            translationY = ty
        }
        .pointerInput(isPressed) {
            awaitPointerEventScope {
                isPressed = <span class="hljs-keyword">if</span> (isPressed) {
                    waitForUpOrCancellation()
                    <span class="hljs-literal">false</span>
                } <span class="hljs-keyword">else</span> {
                    awaitFirstDown(<span class="hljs-literal">false</span>)
                    <span class="hljs-literal">true</span>
                }
            }
        }
}

<span class="hljs-function"><span class="hljs-keyword">fun</span> Modifier.<span class="hljs-title">bounceClickEffect</span><span class="hljs-params">()</span></span> = composed {
    <span class="hljs-keyword">var</span> isPressed <span class="hljs-keyword">by</span> remember { mutableStateOf(<span class="hljs-literal">false</span>) }
    <span class="hljs-keyword">val</span> scale <span class="hljs-keyword">by</span> animateFloatAsState(<span class="hljs-keyword">if</span> (isPressed) <span class="hljs-number">0.70f</span> <span class="hljs-keyword">else</span> <span class="hljs-number">1f</span>)

    <span class="hljs-keyword">this</span>
        .graphicsLayer {
            scaleX = scale
            scaleY = scale
        }
        .pointerInput(isPressed) {
            awaitPointerEventScope {
                isPressed = <span class="hljs-keyword">if</span> (isPressed) {
                    waitForUpOrCancellation()
                    <span class="hljs-literal">false</span>
                } <span class="hljs-keyword">else</span> {
                    awaitFirstDown(<span class="hljs-literal">false</span>)
                    <span class="hljs-literal">true</span>
                }
            }
        }
}

<span class="hljs-function"><span class="hljs-keyword">fun</span> Modifier.<span class="hljs-title">shakeClickEffect</span><span class="hljs-params">()</span></span> = composed {
    <span class="hljs-keyword">var</span> isPressed <span class="hljs-keyword">by</span> remember { mutableStateOf(<span class="hljs-literal">false</span>) }

    <span class="hljs-keyword">val</span> tx <span class="hljs-keyword">by</span> animateFloatAsState(
        targetValue = <span class="hljs-keyword">if</span> (isPressed) -<span class="hljs-number">50f</span> <span class="hljs-keyword">else</span> <span class="hljs-number">0f</span>,
        animationSpec = repeatable(
            iterations = <span class="hljs-number">2</span>,
            animation = tween(durationMillis = <span class="hljs-number">50</span>, easing = LinearEasing),
            repeatMode = RepeatMode.Reverse
        )
    )
    <span class="hljs-keyword">this</span>
        .graphicsLayer {
            translationX = tx
        }
        .pointerInput(isPressed) {
            awaitPointerEventScope {
                isPressed = <span class="hljs-keyword">if</span> (isPressed) {
                    waitForUpOrCancellation()
                    <span class="hljs-literal">false</span>
                } <span class="hljs-keyword">else</span> {
                    awaitFirstDown(<span class="hljs-literal">false</span>)
                    <span class="hljs-literal">true</span>
                }
            }
        }
}
</code></pre>
<p>That's it! Let's take the <code>pressClickEffect()</code> function as an example.</p>
<p>When the <code>pressClickEffect</code> modifier is applied to a composable, it defines a mutable state variable called <code>isPressed</code> using the <code>remember</code> function. This variable is used to keep track of whether the composable is currently being pressed.</p>
<p>The <code>pressClickEffect</code> modifier also defines an animated float value called <code>ty</code> using the <code>animateFloatAsState</code> function. This value represents the vertical translation of the composable and is animated based on the value of <code>isPressed</code>.</p>
<p>When the user presses on the composable, the <code>pointerInput</code> function is used to detect the touch event and set the <code>isPressed</code> variable to <code>true</code>. This triggers the animation of the <code>ty</code> value and causes the composable to move down by 20dp.</p>
<p>When the user releases on the composable, the <code>pointerInput</code> function is used to detect the release event and set the <code>isPressed</code> variable back to <code>false</code>. This triggers the animation of the <code>ty</code> value again, causing the composable to move back up to its original position.</p>
<p>This code implements the <strong>demo application</strong> using this function :</p>
<pre><code class="lang-kotlin"><span class="hljs-meta">@Composable</span>
<span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">ButtonDemo</span><span class="hljs-params">()</span></span> {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.spacedBy(
            <span class="hljs-number">16</span>.dp,
            Alignment.CenterVertically
        ),
    ) {
        <span class="hljs-keyword">val</span> text = remember { mutableStateOf(<span class="hljs-string">"Click It!"</span>) }

        Text(
            text = text.value
        )

        Button(
            onClick = { text.value = <span class="hljs-string">"Press Clicked!"</span> },
            modifier = Modifier.pressClickEffect(),
        ) {
            Text(
                text = <span class="hljs-string">"Press Button"</span>
            )
        }

        Button(
            onClick = { text.value = <span class="hljs-string">"Bounce Clicked!"</span> },
            modifier = Modifier.bounceClickEffect(),
        ) {
            Text(
                text = <span class="hljs-string">"Bounce Button"</span>
            )
        }

        Button(
            onClick = { text.value = <span class="hljs-string">"Shake Clicked!"</span> },
            modifier = Modifier.shakeClickEffect(),
        ) {
            Text(
                text = <span class="hljs-string">"Shake Button"</span>
            )
        }
    }
}
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In conclusion, creating animations with Android Jetpack Compose is easier than ever before. With just a few lines of code, you can create stunning animations that will make your app stand out from the crowd. I hope this article has been helpful and has inspired you to add some cool animations to your own apps. Happy coding!</p>
]]></content:encoded></item></channel></rss>