0

enter image description here

As you can see from the image I have a widget with a series of elements, I have to put at the end of the series of elements then the word Next update which must find some at the end of the widget.

The problem is:

  • if there are few elements, Next update is too high.
  • if there are many elements the Next update message is not shown.

Can you give me a hand?

P.s.

If you think it should be written differently let me know, thank you in advance.

struct GitCommitWidgetEntryView : View {
    var entry: ProviderCommit.Entry
    @Environment(\.colorScheme) var colorScheme
    let firstColor: UInt = getDate() ? 0x4688B4 : 0x030721
    
    var body: some View {
        GeometryReader { geometry in
            if(entry.loading){
                if(!entry.error){
                    VStack() {
                        if(entry.user != ""){
                            HStack {
                                Spacer()
                                Text("\(entry.user) (\(entry.commits.count))")
                                    .font(.caption)
                                    .foregroundColor(Color.black)
                                    .shadow(
                                        color: Color.black,
                                        radius: 1.0,
                                        x: CGFloat(1),
                                        y: CGFloat(1)
                                    )
                                Spacer()
                            }
                            .background(Color(hex: 0xff9800))
                        }
                        if(entry.commits.count > 0){
                            ForEach(entry.commits, id:\.id){
                                item in
                                Text(item.commit.message)
                                    .shadow(
                                        color: Color.black,
                                        radius: 1.0,
                                        x: CGFloat(1),
                                        y: CGFloat(1)
                                    )
                                Text("\(item.author.login) \(convertDate(date: item.commit.author.date))")
                                    .shadow(
                                        color: Color.black,
                                        radius: 1.0,
                                        x: CGFloat(1),
                                        y: CGFloat(1)
                                    )
                                Divider()
                            }
                            .frame(alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                        }else{
                            Text("There are no repositories.")
                                .font(.caption)
                                .foregroundColor(Color.black)
                                .padding(2)
                                .background(Color.white.opacity(0.4))
                                .cornerRadius(5)
                                .padding(.bottom, 3)
                                .padding(.horizontal, /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
                                .multilineTextAlignment(.center)
                                .frame(maxWidth: .infinity, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                        }
                        HStack {
                            Spacer()
                            Text("Next update")
                                .font(.caption)
                                .foregroundColor(Color.black)
                                .multilineTextAlignment(.center)
                                .shadow(
                                    color: Color.black,
                                    radius: 1.0,
                                    x: CGFloat(1),
                                    y: CGFloat(1)
                                )
                            Spacer()
                        }
                        .background(Color(hex: 0xff9800))
                    }
                    .background(LinearGradient(
                                    gradient: Gradient(colors: [
                                        Color(hex: firstColor),
                                        Color(hex: 0xffffff)
                                    ]),
                                    startPoint: .top,
                                    endPoint: .bottom)
                    )
                }else{
                    VStack() {
                        Text("No user exist.")
                            .font(.caption)
                            .foregroundColor(Color.black)
                            .padding(2)
                            .background(Color.white.opacity(0.4))
                            .cornerRadius(5)
                            .padding(.bottom, 3)
                            .padding(.horizontal, /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
                            .multilineTextAlignment(.center)
                    }
                    .frame(maxWidth: .infinity, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    .background(Color.red)
                }
            }else{
                VStack() {
                    Text("No user selected.")
                        .font(.caption)
                        .foregroundColor(Color.black)
                        .padding(2)
                        .background(Color.white.opacity(0.4))
                        .cornerRadius(5)
                        .padding(.bottom, 3)
                        .padding(.horizontal, /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
                        .multilineTextAlignment(.center)
                }
                .frame(maxWidth: .infinity, maxHeight: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                .background(Color.green)
            }
        }
    }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Paul
  • 3,644
  • 9
  • 47
  • 113

1 Answers1

0

The approach you need is to separate "labels to be always on screen" and "growing list", schematically it should be like

VStack {
  Text("Header label")
  Spacer()
  Text("Footer label")
}
.background(
   VStack {
     Text("Header label").opacity(0)     // for content offset
                                    // or constant height spacer
     ForEach ... // list here
   }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • I did as you indicated, but I don't understand what I'm doing wrong. As you can see from the image at the top, some text is cut. https://user-images.githubusercontent.com/20476002/125618565-48bfc0fb-9a79-49b1-80ff-77d411553c0b.gif Code: https://pastebin.com/Gaxmc3MF – Paul Jul 14 '21 at 12:04