10 More Pattern Programs in Dart — Spiral, Diamond & More

By Mihir Pipermitwala · 6 min read

⚡ TL;DR

10 intermediate pattern programs in Dart — spiral matrix, hollow diamond, letter triangle, Fibonacci triangle, wave, and more. Full code with output.

This is a follow-up to our Dart pattern collection. Below are 10 more pattern programs that show up in interviews and help solidify nested loop fundamentals.


1. Spiral Matrix Pattern

Fill an n x n grid with numbers in spiral order.

Copy
1  2  3  4
12 13 14 5
11 16 15 6
10 9  8  7
Copy
void spiralMatrix(int n) {
  List<List<int>> m = List.generate(n, (_) => List.filled(n, 0));
  int num = 1;
  int top = 0, bottom = n - 1, left = 0, right = n - 1;

  while (top <= bottom && left <= right) {
    for (int i = left; i <= right; i++) m[top][i] = num++;
    top++;
    for (int i = top; i <= bottom; i++) m[i][right] = num++;
    right--;
    for (int i = right; i >= left; i--) m[bottom][i] = num++;
    bottom--;
    for (int i = bottom; i >= top; i--) m[i][left] = num++;
    left++;
  }

  for (var row in m) {
    print(row.map((e) => e.toString().padLeft(2)).join(" "));
  }
}

void main() => spiralMatrix(4);

Output

Copy
 1  2  3  4
12 13 14  5
11 16 15  6
10  9  8  7

2. Hollow Diamond Star

Diamond outline, hollow inside.

Copy
  *
 * *
*   *
 * *
  *
Copy
void hollowDiamond(int n) {
  void row(int i) {
    String stars;
    if (i == 1) {
      stars = "*";
    } else {
      stars = "*" + " " * (2 * i - 3) + "*";
    }
    print(" " * (n - i) + stars);
  }

  for (int i = 1; i <= n; i++) row(i);
  for (int i = n - 1; i >= 1; i--) row(i);
}

void main() => hollowDiamond(3);

Output

Copy
  *
 * *
*   *
 * *
  *

3. Letter Triangle

Each row gets one more letter, starting from ‘A’.

Copy
A
A B
A B C
A B C D
A B C D E
Copy
void letterTriangle(int n) {
  for (int i = 0; i < n; i++) {
    final row = <String>[];
    for (int j = 0; j <= i; j++) {
      row.add(String.fromCharCode(65 + j));
    }
    print(row.join(" "));
  }
}

void main() => letterTriangle(5);

Output

Copy
A
A B
A B C
A B C D
A B C D E

4. Number Stair

Row i contains i copies of i.

Copy
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Copy
void numberStair(int n) {
  for (int i = 1; i <= n; i++) {
    print(List.filled(i, i).join(" "));
  }
}

void main() => numberStair(5);

Output

Copy
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

5. Fibonacci Triangle

Each row continues the Fibonacci sequence.

Copy
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377
Copy
void fibonacciTriangle(int n) {
  int a = 0, b = 1;
  for (int i = 1; i <= n; i++) {
    final row = <String>[];
    for (int j = 0; j < i; j++) {
      row.add("$a");
      int temp = a;
      a = b;
      b = temp + b;
    }
    print(row.join(" "));
  }
}

void main() => fibonacciTriangle(5);

Output

Copy
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377

6. Binary Triangle

Alternating 1s and 0s, starting with 1.

Copy
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Copy
void binaryTriangle(int n) {
  for (int i = 1; i <= n; i++) {
    final row = List.generate(i, (j) => (i + j) % 2);
    print(row.join(" "));
  }
}

void main() => binaryTriangle(5);

Output

Copy
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

7. Wave Pattern

Stars tracing a wave.

Copy
*       *
 *     *
  *   *
   * *
    *
Copy
void wavePattern(int n) {
  for (int i = 0; i < n; i++) {
    final row = StringBuffer();
    for (int j = 0; j < 2 * n - 1; j++) {
      if (i == j || i + j == 2 * n - 2) {
        row.write("*");
      } else {
        row.write(" ");
      }
    }
    print(row.toString().trimRight());
  }
}

void main() => wavePattern(5);

Output

Copy
*       *
 *     *
  *   *
   * *
    *

Note: The wave peaks where the two diagonals converge; increase n for a deeper trough.


8. Double Hill

Two mirrored hills forming an “M” shape.

Copy
*             *
**           **
***         ***
****       ****
*****     *****
******   ******
******* *******
Copy
void doubleHill(int n) {
  for (int i = 1; i <= n; i++) {
    final sides = "*" * i;
    final gap = " " * (2 * (n - i) + 1);
    print(sides + gap + sides);
  }
}

void main() => doubleHill(7);

Output

Copy
*             *
**           **
***         ***
****       ****
*****     *****
******   ******
******* *******

9. Hollow Pyramid

A pyramid with only the outline.

Copy
    *
   * *
  *   *
 *     *
*********
Copy
void hollowPyramid(int n) {
  for (int i = 1; i <= n; i++) {
    String row = " " * (n - i);
    if (i == n) {
      row += "*" * (2 * n - 1);
    } else if (i == 1) {
      row += "*";
    } else {
      row += "*" + " " * (2 * i - 3) + "*";
    }
    print(row);
  }
}

void main() => hollowPyramid(5);

Output

Copy
    *
   * *
  *   *
 *     *
*********

10. X-Shape

Diagonals of stars forming an “X”.

Copy
*   *
 * *
  *
 * *
*   *
Copy
void xShape(int n) {
  for (int i = 0; i < n; i++) {
    final row = StringBuffer();
    for (int j = 0; j < n; j++) {
      if (i == j || i + j == n - 1) {
        row.write("*");
      } else {
        row.write(" ");
      }
    }
    print(row.toString());
  }
}

void main() => xShape(5);

Output

Copy
*   *
 * *
  *
 * *
*   *

Conclusion

These Dart patterns complement the earlier creative set. The spiral matrix and hollow shapes are common in interviews, while the letter and Fibonacci triangles reinforce iteration and state management. Combined, they give you a solid pattern toolkit for any coding round.

Happy coding!

Mihir Pipermitwala

Written by Mihir Pipermitwala

Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.

More about Mihir →
Scroll down to load comments...
Related Blogs
Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlock Flutter's potential with clean code. Learn best practices for writing maintainable Dart code, with examples and essential principles.

CLEAN CODECODE BEST PRACTICESCODE MAINTAINABILITYCODE ORGANIZATIONCODE READABILITY

June 02, 2023

Top 9 Local Databases for Flutter: Full Comparison

Top 9 Local Databases for Flutter: Full Comparison

Compare the best local databases for Flutter (Isar, Hive, Sqflite, ObjectBox, Realm, Drift, Floor, CBL, Sembast). Find pros, cons, and performance comparisons.

CODINGFLUTTERFLUTTER LOCAL DATABASESLEARN TO CODENOSQL

April 06, 2023

Related Tutorials
Essential Git Skills: How to Delete Remote Branches

Essential Git Skills: How to Delete Remote Branches

Keep your repository clean and organized. This guide walks you through how to delete both local and remote branches in Git, step by step.

BITBUCKETDELETE LOCAL BRANCHDELETE REMOTE BRANCHGITGIT BRANCH CLEANUP AUTOMATION

June 12, 2024

Show an Offline Message for No Internet in Flutter

Show an Offline Message for No Internet in Flutter

You have to use the 'Connectivity Flutter Package' to achieve this feature on your App. This package helps to know whether your device is online or offline.

CONNECTIVITY PLUSDEPENDENCIESFLUTTERFLUTTER DEVELOPMENTFLUTTER PACKAGES

April 09, 2024

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Implement TabBar and TabBarView in Flutter step by step — customize tab indicators, enable scrollable tabs, and change tabs programmatically.

CODINGDEFAULT TAB CONTROLLERFLUTTERLEARN TO CODETAB CONTROLLER

July 26, 2023

File Manipulation in Flutter: Best Practices & Examples

File Manipulation in Flutter: Best Practices & Examples

Master Flutter file manipulation — permission management, directory handling, and practical read-write scenarios to elevate your app's file handling.

CODINGFLUTTERFLUTTER DEVELOPMENTFLUTTER FILE OPERATIONFLUTTER PATH PROVIDER

June 30, 2023

Related Recommended Services
Visual Studio Code for the Web

Visual Studio Code for the Web

Build with Visual Studio Code, anywhere, anytime, in your browser.

IDEVISUAL STUDIOVISUAL STUDIO CODEWEB
Renovate | Automated Dependency Updates

Renovate | Automated Dependency Updates

Renovate Bot keeps source code dependencies up-to-date using automated Pull Requests.

AUTOMATED DEPENDENCY UPDATESBUNDLERCOMPOSERGITHUBGO MODULES
Best XML Formatter and XML Beautifier

Best XML Formatter and XML Beautifier

Online XML Formatter will format xml data, helps to validate, and works as XML Converter. Save and Share XML.

XMLXML BEAUTIFIERXML CONVERTERXML FORMATXML FORMATTER
Kubecost | Kubernetes cost monitoring and management

Kubecost | Kubernetes cost monitoring and management

Kubecost started in early 2019 as an open-source tool to give developers visibility into Kubernetes spend. We maintain a deep commitment to building and supporting dedicated solutions for the open source community.

CLOUDKUBECOSTKUBERNETESOPEN SOURCESELF HOSTED
Related Recommended Stories
How GitHub reduced testing time for iOS apps with new runner features

How GitHub reduced testing time for iOS apps with new runner features

Learn how GitHub used macOS and Apple Silicon runners for GitHub Actions to build, test, and deploy our iOS app faster.

IOSGITHUBTESTINGRUNNER
5 ways to transform your workflow using GitHub Copilot and MCP

5 ways to transform your workflow using GitHub Copilot and MCP

Learn how to streamline your development workflow with five different MCP use cases.

AGENT MODECODING AGENTCOPILOTFIGMAGITHUB
One weird trick for powerful Git aliases

One weird trick for powerful Git aliases

Advanced Git Aliases

ALIASALIAS TEMPLATEATLASSIANBITBUCKETGIT
Awesome Python

Awesome Python

An opinionated list of awesome Python frameworks, libraries, software and resources

AWESOMEAWESOME PYTHONCOLLECTIONSGITHUBPYTHON
Related Recommended Tools
Find out what websites are built with - Wappalyzer

Find out what websites are built with - Wappalyzer

Find out the technology stack of any website. Create lists of websites and contacts by the technologies they use.

ADD ONSANALYTICSAPP STOREAPPLEBOOKING
Sourcetree | Free Git GUI for Mac and Windows

Sourcetree | Free Git GUI for Mac and Windows

A Git GUI that offers a visual representation of your repositories. Sourcetree is a free Git client for Windows and Mac.

GITGITHUBGITLABATLASSIANBITBUCKET
Related Recommended Videos