Skip to content

[Shapes] - Implementing property method alternatives in Flutter #27

@suragch

Description

@suragch

I'm implementing the a Rectangle widget (#26).

Here is what a Rectangle view in SwiftUI can do:

Screenshot 2024-06-04 at 11 26 58

The swift code for that looks like so:

import SwiftUI

struct RectanglePage: View {
    var body: some View {
        ScrollView {
            VStack(spacing: 20) {
                // Basic rectangle
                Rectangle()
                    .fill(Color.blue)
                    .frame(width: 200, height: 100)
                
                // Rectangle with stroke
                Rectangle()
                    .stroke(Color.red, lineWidth: 4)
                    .frame(width: 200, height: 100)
                
                // Rectangle with rounded corners
                Rectangle()
                    .fill(Color.green)
                    .cornerRadius(20)
                    .frame(width: 200, height: 100)
                
                // Rectangle with gradient fill
                Rectangle()
                    .fill(LinearGradient(gradient: Gradient(colors: [.yellow, .orange]), startPoint: .leading, endPoint: .trailing))
                    .frame(width: 200, height: 100)
                
                // Rectangle with shadow
                Rectangle()
                    .fill(Color.purple)
                    .shadow(color: .gray, radius: 10, x: 0, y: 10)
                    .frame(width: 200, height: 100)
            }
        }
    }
}

struct RectanglePage_Previews: PreviewProvider {
    static var previews: some View {
        RectanglePage()
    }
}

In that code you can see a number of different modifier methods:

  • fill
  • frame
  • stroke
  • cornerRadius
  • shadow

One of the guiding principles is that swift_ui doesn't use modifier methods.

How to avoid modifier methods gives a hypothetical alternative for Flutter:

Frame(
  width: 200,
  height: 100,
  child: Fill(
    Colors.purple,
    child: Ellipse(),
  ),
);

Is this how we want to define the API, by creating widgets for all of the following?

  • Fill
  • Frame
  • Stroke
  • CornerRadius
  • Shadow

Not widget properties?

@matthew-carroll

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions