You can use the Material Grid List to make a column layout responsive together with Angular Flex Layout , but you'll need some code.
TL; DR; Go to the link reference, in English, more detailed than the example below.
The Angular Flex Layout uses breakpoints to detect the size of the screen:
-
xs - max-width: 599px;
-
gt-xs - min-width: 600px;
-
sm - min-width: 600px; max-width: 959px;
-
gt-sm - min-width: 960px;
-
md - min-width: 960px; max-width: 1279px;
-
gt-md - min-width: 1280;
-
lg - min-width: 1280px; max-width: 1919px;
-
gt-lg - min-width: 1920px;
-
xl - min-width: 1920px; max-width: 5000px;
You can use them to define a layout, fxLayout
, in rows ( row
) or columns ( column
), or even visibility of objects with fxShow
and fxHide
, as below: / p>
<div fxLayout="row" fxLayout.xs="column" fxLayoutAlign="center center" fxLayoutAlign.xs="center stretch" class="row-example">
<div>1</div>
<div>2</div>
<div>3</div>
<div fxShow="true" fxHide.xs="true"></div>
</div>
In the angular part of your component, to detect breakpoints , you need a service called ObservableMedia
, which is installed with the Angular Flex Layout package, below the steps followed by an example:
$ npm install @angular/flex-layout --save
You need to import the module into your app.module
:
...
import { FlexLayoutModule } from "@angular/flex-layout";
@NgModule({
imports: [
BrowserModule,
FlexLayoutModule
],
declarations: [ App ],
bootstrap: [ App ]
})
Then on the components that want to use the responsive layout, you need to inject the ObservableMedia
service. In the example below, using mat-grid-list
, we can use ObservableMedia
to define how many columns are displayed per line:
HTML
<mat-grid-list [cols]="cols | async" gutterSize="16px">
<mat-grid-tile>1</mat-grid-tile>
<mat-grid-tile>2</mat-grid-tile>
<mat-grid-tile>3</mat-grid-tile>
<mat-grid-tile>5</mat-grid-tile>
</mat-grid-list>
TS
public cols: Observable<number>;
constructor(private observableMedia: ObservableMedia) {}
ngOnInit() {
const grid = new Map([
["xs", 1],
["sm", 2],
["md", 2],
["lg", 3],
["xl", 3]
]);
let start: number;
grid.forEach((cols, mqAlias) => {
if (this.observableMedia.isActive(mqAlias)) {
start = cols;
}
});
this.cols = this.observableMedia.asObservable()
.map(change => grid.get(change.mqAlias))
.startWith(start);
}
In the above code in% w /% w /%, the Media Query Alias , maAlias, is mapped to the breakpoints of screen sizes, and associates the same number of columns.
In map
, initialization of the component, we detect which breakpoint is active to define the initial number of columns.
Finally, we initialize a grid
to monitor screen size changes activated by the ngOnInit
service, every time we change, we adjust the size of the columns.
Source: link