Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
"widgets/flexible",
"widgets/fitted_box",
"widgets/fractionally_sized_box",
"widgets/intrinsic_height",
"widgets/intrinsic_width",
"widgets/limited_box",
"widgets/padding",
"widgets/positioned",
Expand Down
151 changes: 151 additions & 0 deletions docs/widgets/intrinsic_height.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
---
title: "IntrinsicHeight"
description: "Documentation for IntrinsicHeight"
---

import { PLAYGROUND_BASE_URL } from "/snippets/playground_base.mdx";

export const intrinsicHeightPreviewJson = {
"type": "intrinsicHeight",
"child": {
"type": "row",
"crossAxisAlignment": "stretch",
"children": [
{
"type": "container",
"width": 60,
"color": "#FF5733",
"child": {
"type": "text",
"data": "Short"
}
},
{
"type": "container",
"width": 60,
"color": "#33A1FF",
"child": {
"type": "text",
"data": "A much longer piece of text"
}
}
]
}
};
export const intrinsicHeightPreviewSrc = `${PLAYGROUND_BASE_URL}/embed`;

The Stac IntrinsicHeight allows you to build a Flutter intrinsic height widget using JSON.
To know more about the intrinsic height widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/widgets/IntrinsicHeight-class.html).

`IntrinsicHeight` sizes its child to the child's maximum intrinsic height. It is most useful
inside a `row` that uses `crossAxisAlignment: stretch`, when the row itself sits somewhere
with an unbounded height — for example inside a horizontally scrolling
`singleChildScrollView` that is itself inside a vertically scrolling list. In that situation
Comment thread
divyanshub024 marked this conversation as resolved.
`stretch` has no finite height to stretch its children into and layout fails; wrapping the
`row` in `intrinsicHeight` computes a real height first, so every child of the row ends up
the same height as the tallest one.

<Warning>
`intrinsicHeight` is relatively expensive because it performs a speculative extra layout
pass before the final layout. Prefer giving the child a fixed height where you can, and
reach for `intrinsicHeight` only when the height genuinely needs to be computed from the
content.
</Warning>

## Properties

| Property | Type | Description |
|----------|--------------|----------------------------------------------------------|
| child | `StacWidget` | The widget to size to its own maximum intrinsic height. |

## Example

<Tabs sync={false}>
<Tab title="Dart">

```dart
StacIntrinsicHeight(
child: StacRow(
crossAxisAlignment: StacCrossAxisAlignment.stretch,
children: [
StacContainer(
width: 60,
color: StacColors.deepOrange,
child: StacText(data: 'Short'),
),
StacContainer(
width: 60,
color: StacColors.blue,
child: StacText(data: 'A much longer piece of text'),
),
],
),
)
```

</Tab>
<Tab title="JSON">

```json
{
"type": "intrinsicHeight",
"child": {
"type": "row",
"crossAxisAlignment": "stretch",
"children": [
{
"type": "container",
"width": 60,
"color": "#FF5733",
"child": {
"type": "text",
"data": "Short"
}
},
{
"type": "container",
"width": 60,
"color": "#33A1FF",
"child": {
"type": "text",
"data": "A much longer piece of text"
}
}
]
}
}
```

</Tab>
<Tab title="Preview">
<Frame>
<iframe
id="stac"
src={intrinsicHeightPreviewSrc}
title="Stac Playground"
className="w-full rounded-xl border-0"
style={{ height: "640px" }}
loading="lazy"
onLoad={(event) => {
const iframe = event.currentTarget;
const targetOrigin = PLAYGROUND_BASE_URL;
const message = {
type: "stac-preview-json",
payload: intrinsicHeightPreviewJson
};

let attempts = 0;
const maxAttempts = 12;
const interval = setInterval(() => {
iframe.contentWindow?.postMessage(message, targetOrigin);
attempts += 1;

if (attempts >= maxAttempts) {
clearInterval(interval);
}
}, 250);
}}
/>
</Frame>
</Tab>
</Tabs>
150 changes: 150 additions & 0 deletions docs/widgets/intrinsic_width.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
---
title: "IntrinsicWidth"
description: "Documentation for IntrinsicWidth"
---

import { PLAYGROUND_BASE_URL } from "/snippets/playground_base.mdx";

export const intrinsicWidthPreviewJson = {
"type": "intrinsicWidth",
"child": {
"type": "column",
"crossAxisAlignment": "stretch",
"children": [
{
"type": "container",
"height": 40,
"color": "#FF5733",
"child": {
"type": "text",
"data": "Short"
}
},
{
"type": "container",
"height": 40,
"color": "#33A1FF",
"child": {
"type": "text",
"data": "A much longer piece of text"
}
}
]
}
};
export const intrinsicWidthPreviewSrc = `${PLAYGROUND_BASE_URL}/embed`;

The Stac IntrinsicWidth allows you to build a Flutter intrinsic width widget using JSON.
To know more about the intrinsic width widget in Flutter, refer to the [official documentation](https://api.flutter.dev/flutter/widgets/IntrinsicWidth-class.html).

`IntrinsicWidth` sizes its child to the child's maximum intrinsic width. It is the
horizontal counterpart of [IntrinsicHeight](/widgets/intrinsic_height) and solves the same
problem along the other axis — for example a `column` using `crossAxisAlignment: stretch`
that sits somewhere with an unbounded width.

<Warning>
`intrinsicWidth` is relatively expensive because it performs a speculative extra layout
pass before the final layout. Prefer giving the child a fixed width where you can, and
reach for `intrinsicWidth` only when the width genuinely needs to be computed from the
content.
</Warning>

## Properties

| Property | Type | Description |
|------------|--------------|-----------------------------------------------------------------------|
| stepWidth | `double` | If set, forces the child's width to be a multiple of this value. |
| stepHeight | `double` | If set, forces the child's height to be a multiple of this value. |
| child | `StacWidget` | The widget to size to its own maximum intrinsic width. |

## Example

<Tabs sync={false}>
<Tab title="Dart">

```dart
StacIntrinsicWidth(
child: StacColumn(
crossAxisAlignment: StacCrossAxisAlignment.stretch,
children: [
StacContainer(
height: 40,
color: StacColors.deepOrange,
child: StacText(data: 'Short'),
),
StacContainer(
height: 40,
color: StacColors.blue,
child: StacText(data: 'A much longer piece of text'),
),
],
),
)
```

</Tab>
<Tab title="JSON">

```json
{
"type": "intrinsicWidth",
"child": {
"type": "column",
"crossAxisAlignment": "stretch",
"children": [
{
"type": "container",
"height": 40,
"color": "#FF5733",
"child": {
"type": "text",
"data": "Short"
}
},
{
"type": "container",
"height": 40,
"color": "#33A1FF",
"child": {
"type": "text",
"data": "A much longer piece of text"
}
}
]
}
}
```

</Tab>
<Tab title="Preview">
<Frame>
<iframe
id="stac"
src={intrinsicWidthPreviewSrc}
title="Stac Playground"
className="w-full rounded-xl border-0"
style={{ height: "640px" }}
loading="lazy"
onLoad={(event) => {
const iframe = event.currentTarget;
const targetOrigin = PLAYGROUND_BASE_URL;
const message = {
type: "stac-preview-json",
payload: intrinsicWidthPreviewJson
};

let attempts = 0;
const maxAttempts = 12;
const interval = setInterval(() => {
iframe.contentWindow?.postMessage(message, targetOrigin);
attempts += 1;

if (attempts >= maxAttempts) {
clearInterval(interval);
}
}, 250);
}}
/>
</Frame>
</Tab>
</Tabs>
2 changes: 2 additions & 0 deletions packages/stac/lib/src/framework/stac_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class StacService {
const StacAspectRatioParser(),
const StacFittedBoxParser(),
const StacLimitedBoxParser(),
const StacIntrinsicHeightParser(),
const StacIntrinsicWidthParser(),
const StacDynamicViewParser(),
const StacDropdownMenuParser(),
const StacClipRRectParser(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';
import 'package:stac/src/parsers/core/stac_widget_parser.dart';
import 'package:stac_framework/stac_framework.dart';
import 'package:stac_core/widgets/intrinsic_height/stac_intrinsic_height.dart';

class StacIntrinsicHeightParser extends StacParser<StacIntrinsicHeight> {
const StacIntrinsicHeightParser();

@override
String get type => StacIntrinsicHeight().type;

@override
StacIntrinsicHeight getModel(Map<String, dynamic> json) =>
StacIntrinsicHeight.fromJson(json);

@override
Widget parse(BuildContext context, StacIntrinsicHeight model) {
return IntrinsicHeight(child: model.child.parse(context));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';
import 'package:stac/src/parsers/core/stac_widget_parser.dart';
import 'package:stac_framework/stac_framework.dart';
import 'package:stac_core/widgets/intrinsic_width/stac_intrinsic_width.dart';

class StacIntrinsicWidthParser extends StacParser<StacIntrinsicWidth> {
const StacIntrinsicWidthParser();

@override
String get type => StacIntrinsicWidth().type;

@override
StacIntrinsicWidth getModel(Map<String, dynamic> json) =>
StacIntrinsicWidth.fromJson(json);

@override
Widget parse(BuildContext context, StacIntrinsicWidth model) {
return IntrinsicWidth(
stepWidth: model.stepWidth,
stepHeight: model.stepHeight,
child: model.child.parse(context),
);
}
}
2 changes: 2 additions & 0 deletions packages/stac/lib/src/parsers/widgets/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export 'package:stac/src/parsers/widgets/stac_hero/stac_hero_parser.dart';
export 'package:stac/src/parsers/widgets/stac_icon/stac_icon_parser.dart';
export 'package:stac/src/parsers/widgets/stac_icon_button/stac_icon_button_parser.dart';
export 'package:stac/src/parsers/widgets/stac_image/stac_image_parser.dart';
export 'package:stac/src/parsers/widgets/stac_intrinsic_height/stac_intrinsic_height_parser.dart';
export 'package:stac/src/parsers/widgets/stac_intrinsic_width/stac_intrinsic_width_parser.dart';
export 'package:stac/src/parsers/widgets/stac_limited_box/stac_limited_box_parser.dart';
export 'package:stac/src/parsers/widgets/stac_linear_progress_indicator/stac_linear_progress_indicator_parser.dart';
export 'package:stac/src/parsers/widgets/stac_list_tile/stac_list_tile_parser.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ enum WidgetType {
/// Ink well widget
inkWell,

/// Intrinsic height widget
intrinsicHeight,

/// Intrinsic width widget
intrinsicWidth,

/// Limited box widget
limitedBox,

Expand Down
Loading
Loading