我发现这个更好:
https ://vaadin.com/directory/component/app-layout-add-on
它与 Vaadin 11(也是 V10 和 V8)兼容,更重要的是,它基于与 Vaadin Router API 的兼容。
<dependency>
<groupId>com.github.appreciated</groupId>
<artifactId>app-layout-addon</artifactId>
<version>2.0.0</version>
</dependency>
这是 Vaadin 11(和 10)的代码示例:
https ://github.com/appreciated/vaadin-app-layout/tree/master/app-layout-examples/app-layout-plain-example
从在您的主布局上扩展 AppLayoutRouterLayout 开始(我还从主布局中删除了默认路由,但由您决定)并继续上面的简单示例。
以下是基于上述示例的示例代码;
import com.github.appreciated.app.layout.behaviour.Behaviour;
import com.github.appreciated.app.layout.builder.AppLayoutBuilder;
import com.github.appreciated.app.layout.component.appbar.AppBarBuilder;
import com.github.appreciated.app.layout.component.appmenu.MenuHeaderComponent;
import com.github.appreciated.app.layout.component.appmenu.left.LeftClickableComponent;
import com.github.appreciated.app.layout.component.appmenu.left.LeftNavigationComponent;
import com.github.appreciated.app.layout.component.appmenu.left.builder.LeftAppMenuBuilder;
import com.github.appreciated.app.layout.design.AppLayoutDesign;
import com.github.appreciated.app.layout.notification.DefaultNotificationHolder;
import com.github.appreciated.app.layout.notification.component.AppBarNotificationButton;
import com.github.appreciated.app.layout.router.AppLayoutRouterLayout;
import com.vaadin.flow.component.icon.VaadinIcon;
import static com.github.appreciated.app.layout.entity.Section.HEADER;
public class MainLayout
extends AppLayoutRouterLayout {//https://vaadin.com/directory/component/app-layout-add-on
private static DefaultNotificationHolder notifications = new DefaultNotificationHolder(newStatus -> {
});
@Override
public com.github.appreciated.app.layout.behaviour.AppLayout getAppLayout() {
return AppLayoutBuilder
.get(Behaviour.LEFT_HYBRID_SMALL)// There some other behaviours too
.withTitle("Header")
.withAppBar(AppBarBuilder
.get()
.add(new AppBarNotificationButton(VaadinIcon.BELL, notifications))
.build())
.withDesign(AppLayoutDesign.MATERIAL)
.withAppMenu(LeftAppMenuBuilder
.get()
.addToSection(new MenuHeaderComponent("Menu-Header",
"Version 2.0.1",
null
), HEADER)
.addToSection(new LeftClickableComponent("Set Behaviour HEADER",
VaadinIcon.COG.create(),
clickEvent -> openModeSelector()
), HEADER)
.add(new LeftNavigationComponent("Home", VaadinIcon.HOME.create(), View1.class))
.build())
.build();
}
private void openModeSelector() {
// the code to open a dialog
}
}
其中 View1 类只是一个简单的布局;
@Route(value = "", layout = MainLayout.class)
public class View1 extends VerticalLayout {
public View1() {
Paragraph label = new Paragraph("Hi!");
label.setWidth("100%");
add(label);
}
}
此外,请确保将视图作为默认视图 (@Route(value = "",...)