Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give Form Section a Background Color/Gradient

This is my code:

Form {
  Section {} // regular section no background

  Section { // <--- want this section to have a background color
     Text("Hello")
  }
  .background(Gradient(colors: [.teal, .cyan, .green]).opacity(0.6))

} // Do not want background on form only on an individual section

This only puts a background on the Text(), which makes sense, but how do I get it to apply the background just the section

like image 775
Yuda Avatar asked Dec 07 '25 16:12

Yuda


1 Answers

You need to use .listRowBackground and apply a gradient as the background style. See below:

Form {
      Section {} // regular section no background

      Section { // <--- want this section to have a background color
         Text("Hello")
      }
      .listRowBackground(LinearGradient(gradient: Gradient(colors: [.teal, .cyan, .green]), startPoint: .leading, endPoint: .trailing))
        
} // Do not want background on form only on an individual section

enter image description here

like image 193
nickreps Avatar answered Dec 09 '25 16:12

nickreps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!