<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Melihcan on Medium]]></title>
        <description><![CDATA[Stories by Melihcan on Medium]]></description>
        <link>https://medium.com/@melihcan129?source=rss-d58af8e531d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Melihcan on Medium</title>
            <link>https://medium.com/@melihcan129?source=rss-d58af8e531d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:26:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@melihcan129/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Drawing Custom Shapes in Flutter with CustomPainter]]></title>
            <link>https://medium.com/@melihcan129/drawing-custom-shapes-in-flutter-with-custompainter-04e010f701f4?source=rss-d58af8e531d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/04e010f701f4</guid>
            <dc:creator><![CDATA[Melihcan]]></dc:creator>
            <pubDate>Mon, 24 Feb 2025 12:14:21 GMT</pubDate>
            <atom:updated>2025-02-24T12:14:21.634Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>What is CustomPainter?</strong><br>CustomPainter in Flutter is a class used for drawing custom shapes and graphics. While we typically use widgets like Container, Image, or Icon, CustomPainter allows us to create fully customized visuals.</blockquote><blockquote><strong>Why Use It?</strong></blockquote><ul><li>To create unique UI designs</li><li>To draw custom graphics and shapes</li><li>To generate animated visuals</li></ul><p>Let’s start with a basic example where we draw a circle using CustomPainter.</p><ul><li>You can start by creating a new file named <strong>circle_painter.dart</strong> and add the following content:</li></ul><pre>import &#39;package:flutter/material.dart&#39;;<br><br>class CirclePainter extends CustomPainter {<br>  @override<br>  void paint(Canvas canvas, Size size) {<br>    final Paint paint = Paint()<br>      ..color = Colors.blue<br>      ..style = PaintingStyle.fill; // Fill the shape<br><br>    canvas.drawCircle(<br>      Offset(size.width / 2, size.height / 2), // Center<br>      size.width / 2, // Radius<br>      paint,<br>    );<br>  }<br><br>  @override<br>  bool shouldRepaint(CustomPainter oldDelegate) {<br>    return false;<br>  }<br>}</pre><ul><li>Now, you can use CirclePainter inside your main widget by importing this file.</li></ul><pre>import &#39;package:flutter/material.dart&#39;;<br><br>void main() {<br>  runApp(const MyApp());<br>}<br><br>class MyApp extends StatelessWidget {<br>  const MyApp({super.key});<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return MaterialApp(<br>      debugShowCheckedModeBanner: false,<br>      home: Scaffold(<br>        appBar: AppBar(title: const Text(&quot;CustomPainter Example&quot;)),<br>        body: Center(<br>          child: CustomPaint(<br>            size: const Size(200, 200),<br>            painter: CirclePainter(),<br>          ),<br>        ),<br>      ),<br>    );<br>  }<br>}</pre><blockquote><strong>What is </strong><strong>shouldRepaint method?</strong></blockquote><p>shouldRepaint is a crucial part of CustomPainter, so let&#39;s dive deeper into it.</p><p>The shouldRepaint(CustomPainter oldDelegate) method determines whether Flutter should repaint the widget when the CustomPainter is updated. It helps optimize performance by preventing unnecessary redraws.</p><p>If shouldRepaint returns true, Flutter will call the paint method again and redraw the widget. If it returns false, the widget will not be repainted, saving resources and improving efficiency.</p><blockquote><strong>If the drawing never changes</strong> → return false.</blockquote><ul><li>Example: A static shape (like a simple circle) that does not depend on external variables.</li><li>Since the output is always the same, there’s no need to repaint it.</li></ul><blockquote><strong>If the drawing depends on dynamic values</strong> → compare old and new values.</blockquote><ul><li>Example: A shape that changes size, color, or position over time.</li><li>Compare the old values with the new ones to determine if a repaint is necessary.</li></ul><p>Let’s create a dynamic example where we change the circle’s radius over time using a slider.</p><ul><li>Let’s update our CirclePainter class with the following changes:</li></ul><pre>import &#39;package:flutter/material.dart&#39;;<br><br>class CirclePainter extends CustomPainter {<br>  final double radius;<br>  final Color color; // New: Dynamic color<br><br>  CirclePainter({required this.radius, required this.color});<br><br>  @override<br>  void paint(Canvas canvas, Size size) {<br>    final Paint paint = Paint()<br>      ..color = color // Apply dynamic color<br>      ..style = PaintingStyle.fill;<br><br>    canvas.drawCircle(<br>      Offset(size.width / 2, size.height / 2),<br>      radius,<br>      paint,<br>    );<br>  }<br><br>  @override<br>  bool shouldRepaint(CirclePainter oldDelegate) {<br>    return oldDelegate.radius != radius || oldDelegate.color != color;<br>    // Repaint only if radius or color changes<br>  }<br>}</pre><ul><li>Let’s create a stateful widget named <strong>DynamicCircleScreen</strong>, which will allow us to dynamically change the circle&#39;s <strong>radius</strong> and <strong>color</strong> using sliders.</li></ul><pre>import &#39;package:flutter/material.dart&#39;;<br>import &#39;circle_painter.dart&#39;; // Import the updated CirclePainter<br><br>class DynamicCircleScreen extends StatefulWidget {<br>  const DynamicCircleScreen({super.key});<br><br>  @override<br>  _DynamicCircleScreenState createState() =&gt; _DynamicCircleScreenState();<br>}<br><br>class _DynamicCircleScreenState extends State&lt;DynamicCircleScreen&gt; {<br>  double _radius = 50.0; // Initial radius<br>  Color _color = Colors.blue; // Initial color<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return Scaffold(<br>      appBar: AppBar(title: const Text(&quot;Dynamic Circle Painter&quot;)),<br>      body: Column(<br>        mainAxisAlignment: MainAxisAlignment.center,<br>        children: [<br>          CustomPaint(<br>            size: const Size(200, 200),<br>            painter: CirclePainter(radius: _radius, color: _color),<br>          ),<br>          const SizedBox(height: 20),<br>          Text(&quot;Radius: ${_radius.toStringAsFixed(1)}&quot;),<br>          Slider(<br>            min: 10,<br>            max: 100,<br>            value: _radius,<br>            onChanged: (value) {<br>              setState(() {<br>                _radius = value;<br>              });<br>            },<br>          ),<br>          const SizedBox(height: 20),<br>          Text(&quot;Change Color&quot;),<br>          Wrap(<br>            spacing: 10,<br>            children: [<br>              _colorButton(Colors.blue),<br>              _colorButton(Colors.red),<br>              _colorButton(Colors.green),<br>              _colorButton(Colors.orange),<br>            ],<br>          ),<br>        ],<br>      ),<br>    );<br>  }<br><br>  Widget _colorButton(Color color) {<br>    return GestureDetector(<br>      onTap: () {<br>        setState(() {<br>          _color = color;<br>        });<br>      },<br>      child: Container(<br>        width: 40,<br>        height: 40,<br>        decoration: BoxDecoration(<br>          color: color,<br>          shape: BoxShape.circle,<br>          border: Border.all(color: Colors.black, width: 1),<br>        ),<br>      ),<br>    );<br>  }<br>}</pre><h3>How It Works?</h3><p>✔ <strong>Slider changes the circle’s radius dynamically.</strong><br>✔ <strong>Color buttons allow changing the circle’s color.</strong><br>✔ <strong>Uses </strong><strong>setState() to trigger a rebuild when values change.</strong><br>✔ <strong>Passes dynamic values to </strong><strong>CirclePainter.</strong></p><p>Now, when you move the slider or tap a color, the circle updates <strong>efficiently</strong> thanks to shouldRepaint. 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04e010f701f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flutter Generic Dropdown]]></title>
            <link>https://medium.com/@melihcan129/flutter-generic-dropdown-a0f582103fe0?source=rss-d58af8e531d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0f582103fe0</guid>
            <category><![CDATA[dartlang]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Melihcan]]></dc:creator>
            <pubDate>Mon, 30 Dec 2024 18:45:06 GMT</pubDate>
            <atom:updated>2024-12-30T18:45:06.076Z</atom:updated>
            <content:encoded><![CDATA[<p>I developed a reusable, flexible dropdown component for Flutter projects. With this generic structure, adapting dropdowns for various data types is now much simpler! Check out my post for details and tips.</p><p>Custom Dropdown is below:</p><pre>import &#39;package:flutter/material.dart&#39;;<br><br>class GenericDropdown&lt;T&gt; extends StatelessWidget {<br>  final void Function(T?) onChanged;<br>  final List&lt;T&gt; items;<br>  final String Function(T) displayText;<br>  final T? value;<br>  final TextStyle? style;<br>  final BorderRadius? borderRadius;<br>  const GenericDropdown({<br>    super.key,<br>    required this.onChanged,<br>    required this.items,<br>    required this.displayText,<br>    this.value,<br>    this.style,<br>    this.borderRadius,<br>  });<br><br>  @override<br>  Widget build(BuildContext context) {<br>    return DropdownButton&lt;T&gt;(<br>      value: value,<br>      style: style,<br>      borderRadius: borderRadius,<br>      items: items<br>          .map((e) =&gt; DropdownMenuItem&lt;T&gt;(<br>                value: e,<br>                child: Text(displayText(e)),<br>              ))<br>          .toList(),<br>      onChanged: onChanged,<br>    );<br>  }<br>}</pre><p>One challenge with creating a generic dropdown is that the displayed value might vary depending on the class or type. For instance, let’s say we have a Car class with properties name and model, and an Animal class with properties gender and name. When using a dropdown for the Car class, we might want to display both the car’s name and model, whereas in a dropdown for the Animal class, we might only want to show the animal’s name. Accessing these values within a generic structure, however, can be tricky.</p><p>To solve this, the dropdown widget takes a parameter String Function(T) displayText, which is a function that returns a String and accepts a parameter of type T. This allows us to customize the displayed value in the dropdown for each specific use case. By defining this function, we can control exactly what text appears in the dropdown, regardless of the data type.</p><pre>final String Function(T) displayText;</pre><p>Let’s create an example to test it out.</p><p>First of all let’s create car model to use in dropdown;</p><pre>final class Car {<br>  final String name;<br>  final String model;<br><br>  Car({required this.name, required this.model});<br><br>  static List&lt;Car&gt; getCars() {<br>    return [<br>      Car(name: &#39;Toyota&#39;, model: &#39;Corolla&#39;),<br>      Car(name: &#39;Volkswagen&#39;, model: &#39;Jetta&#39;),<br>    ];<br>  }<br><br>  @override<br>  int get hashCode =&gt; name.hashCode ^ model.hashCode;<br><br>  @override<br>  bool operator ==(Object other) =&gt;<br>      identical(this, other) ||<br>      other is Car &amp;&amp; name == other.name &amp;&amp; model == other.model;<br>}</pre><p>Lastly let’s implement the custom dropdown widget into the page;</p><pre>class MyApp extends StatefulWidget {<br>  const MyApp({super.key});<br><br>  @override<br>  State&lt;MyApp&gt; createState() =&gt; _MyAppState();<br>}<br><br>class _MyAppState extends State&lt;MyApp&gt; {<br>  Car? selectedCar;<br>  @override<br>  Widget build(BuildContext context) {<br>    return MaterialApp(<br>      debugShowCheckedModeBanner: false,<br>      title: &#39;Flutter Demo&#39;,<br>      theme: AppTheme.lightMode,<br>      home: Scaffold(<br>        appBar: AppBar(<br>          title: const Text(&#39;Dropdown Example&#39;),<br>        ),<br>        body: Center(<br>          child: GenericDropdown&lt;Car&gt;(<br>            items: Car.getCars(),<br>            value: selectedCar,<br>            displayText: (v)=&gt; &quot;${v.name} ${v.model}&quot;,<br>            onChanged: (v) {<br>              setState(() {<br>                selectedCar = v;<br>              });<br>            },<br>          ),<br>        ),<br>      ),<br>    );<br>  }<br>}</pre><p>I hope this example helps clarify how to implement a flexible generic dropdown in Flutter. Let me know if you have any questions or need further assistance!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0f582103fe0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>