【问题标题】:How would I code this Tab Bar in Flutter?我将如何在 Flutter 中编写这个标签栏?
【发布时间】:2020-12-05 21:38:58
【问题描述】:

我有一些使用 SwiftUI 的经验,并且我在前段时间编写了这段代码。我目前正在学习如何使用 Flutter 构建 UI。我将如何进行编码?它是一个可以通过滑动打开和关闭的浮动标签栏。

这基本上是一个圆角矩形 HStack,里面有 5 个按钮,如果它被滑动或长按,可以动画成一个按钮。几天前我刚刚开始使用 Flutter,但我仍在努力将我的 SwiftUI 逻辑转换为 Flutter。有什么想法吗?


struct TabBar: View {
    
@Binding var selected : Int
@State var expand = true
    
    var drag: some Gesture {
        DragGesture()
            .onChanged {_ in self.expand = false}
        }

var body: some View {
        
    HStack {
        Spacer(minLength:0)
        
        HStack{
            
            if !self.expand{
                
                Button(action: {
                    self.expand.toggle()
                }) {
                    Image("appleLogo")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 15, height: 15, alignment: .center)
                        .foregroundColor(.black)
                        .scaleEffect(2)
                        .padding()
                }
                
            }
                
            else{
                Button(action: {
                    self.selected = 0
                            }) {
                                ZStack {
                                    
                                    Circle()
                                    .frame(width: 40, height: 40)
                                    .opacity(0)
                                    
                                    Image(systemName: "house")
                                        .foregroundColor(self.selected == 0 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                            Spacer(minLength:15)
                            
                            Button(action: {
                                self.selected = 1
                            }) {
                                ZStack {
                                    
                                    Circle()
                                    .frame(width: 40, height: 40)
                                    .opacity(0)
                                    
                                    Image(systemName: "bubble.left")
                                        .foregroundColor(self.selected == 1 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                            Spacer(minLength:15)

                            Button(action: {
                                self.selected = 2
                            }) {
                                ZStack {
                                    
                                    Circle()
                                    .frame(width: 40, height: 40)
                                    .opacity(0)
                                    
                                    Image(systemName: "plus.app")
                                        .foregroundColor(self.selected == 2 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                            Spacer(minLength:15)
                            
                            Button(action: {
                                self.selected = 3
                            }) {
                                ZStack {
                                    
                                    Circle()
                                    .frame(width: 40, height: 40)
                                    .opacity(0)
                                    
                                    Image(systemName: "cart")
                                        .foregroundColor(self.selected == 3 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                            Spacer(minLength:15)

                            Button(action: {
                                self.selected = 4
                            }) {
                                ZStack {
                                    
                                    Circle()
                                        .frame(width: 40, height: 40)
                                        .opacity(0)
                                    
                                    Image(systemName: "person")
                                        .foregroundColor(self.selected == 4 ? .black : Color("darkGray"))
                                        .padding(.horizontal)
                                        .scaleEffect(1.5)
                                }
                            }
                            
                        }
                    }
        .padding(.vertical,self.expand ? 8 : 6)
        .padding(.horizontal,self.expand ? 20 : 6)
        .background(VisualEffectView())
        .background(Color("TabBarGray").opacity(0.3))
        .opacity(self.expand ? 1.0 : 0.3)
        .clipShape(Capsule())
        .padding(30)
        .onLongPressGesture {
            self.expand.toggle()}
        .gesture(drag)
        .animation(.interactiveSpring(response:0.6, dampingFraction:0.6, blendDuration:0.6))
        .frame(maxWidth: UIScreen.main.bounds.width,
               maxHeight: UIScreen.main.bounds.height, alignment: .center)
                }
            }
        }

打开

关闭

【问题讨论】:

    标签: swift flutter swiftui flutter-layout flutter-animation


    【解决方案1】:

    我也是 Flutter 的新手,但一般来说我会 创建一个有状态的小部件来存储按钮栏的状态。然后将按钮栏创建为无状态小部件(使用 Container(child:Row(children...)

    在有状态小部件中,您可以使用任何启用滑动的方法来包装无状态小部件的调用(到目前为止还没有使用过)。在那里你会有类似 setState(() {expanded = !expanded}) 的东西,并且可能你用类似的东西调用无状态小部件

    Container(
      child: (expanded ? ShowBar() : Container()),
      )
    

    在哪里显示无状态小部件类 ShowBar 或空容器

    【讨论】:

    • 我最终制作了一个 Stateful Widget 和一个 AnimatedContainer,它可以在扩展栏和关闭栏之间切换。我也会测试你的解决方案,看看它会发生什么。谢谢!
    猜你喜欢
    • 2011-03-12
    • 1970-01-01
    • 2020-01-20
    • 2020-09-07
    • 2023-03-08
    • 2015-10-19
    • 2011-11-17
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多