hydra 2.1.0
hydra: ^2.1.0 copied to clipboard
Responsive and adaptive Flutter layouts with breakpoints. Swap widgets, values, grids and visibility per screen size, animated, with zero dependencies.
Hydra #
Responsive layouts are like the Hydra of Greek mythology - cut off one screen size and two more appear. Phone, tablet, foldable, desktop, ultrawide... every time you think you've handled them all, a new head grows.
Hydra tames the beast. Describe what changes at up to four breakpoints and let it pick the right variant - with zero additional dependencies beyond Flutter itself.
This is the example app: one widget tree, resized from 390 to 1320 px and back. Navigation, header and grid are plain Hydra widgets - including the transitions.
Highlights #
- Responsive widgets - swap entire widgets per breakpoint
- Animated transitions - widgets fade, scale or slide in and out instead of jumping
- Responsive values - adapt padding, font sizes, colors or any type
T - Building blocks - visibility, grid and builder widgets for the common cases
- Configure once - one
HydraScopeinstead of abehaviouron every widget - Container queries -
HydraLayoutScopeadapts to a panel or dialog instead of the screen - Smart fallbacks - you don't need a variant for every breakpoint
- Cheap rebuilds - below a
HydraScope, widgets rebuild per breakpoint, not per pixel - Zero dependencies - only Flutter SDK, nothing else
- 100% test coverage - every line, every head
Installation #
flutter pub add hydra
import 'package:hydra/hydra.dart';
At a glance #
| You want to... | Use |
|---|---|
| show a different widget per breakpoint | HydraWidget |
| pick a value per breakpoint | HydraValue<T> / context.hydra<T>() |
| build one tree from the breakpoint | HydraBuilder / Breakpoint.current |
| show something only on some screens | HydraVisibility |
| change the number of grid columns | HydraGrid / HydraSliverGrid |
| set breakpoints once for the whole app | HydraScope |
| adapt to a panel or dialog, not the screen | HydraLayoutScope |
Responsive Widgets #
Use HydraWidget to display different widgets based on the current breakpoint:
const HydraWidget(
mini: PhoneLayout(),
medium: TabletLayout(),
large: DesktopLayout(),
)
Hydra selects the best match for the current screen width. If no exact match exists, it falls back to the nearest available alternative - small shows TabletLayout here.
Animated transitions #
By default the widgets are swapped instantly. Pass a transition to animate the change:
const HydraWidget(
transition: HydraTransition.fade(),
mini: PhoneLayout(),
large: DesktopLayout(),
)
| Transition | Effect |
|---|---|
HydraTransition.fade() |
Cross-fades the old and the new widget |
HydraTransition.scale() |
Fades and scales the new widget in |
HydraTransition.size() |
Collapses the old widget and expands the new one along an axis |
HydraTransition(builder: ...) |
Your own AnimatedSwitcher transition builder |
All of them take a duration (300 ms by default), a curve and an alignment for variants of different size. The same transitions work on HydraVisibility.
Responsive Values #
Use the BuildContext extension when you need a value instead of a whole widget:
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(context.hydra<double>(mini: 8, large: 32)),
child: Text(
'Hello',
style: TextStyle(
fontSize: context.hydra<double>(mini: 14, medium: 16, large: 20),
),
),
);
}
Or keep the value around as a HydraValue<T> and resolve it later:
const padding = HydraValue<double>(mini: 8, small: 16, large: 32);
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(padding.resolve(context)),
child: content,
);
}
Works with any type - double, EdgeInsets, TextStyle, Color, or your own types.
The Current Breakpoint #
HydraBuilder hands the active breakpoint to a builder, for a single widget tree that adapts in place:
HydraBuilder(
builder: (context, breakpoint) {
return Padding(
padding: EdgeInsets.all(breakpoint == Breakpoint.mini ? 8 : 24),
child: Text('Current: ${breakpoint.name}'),
);
},
)
Outside of a builder, ask for it directly:
final breakpoint = Breakpoint.current(context);
if (breakpoint >= Breakpoint.medium) {
showSideNavigation();
}
Breakpoints are ordered from mini to large and compare with <, <=, > and >=.
Visibility #
HydraVisibility shows its child only within a range of breakpoints. Both bounds are inclusive and optional; a hidden child is not built.
// Side navigation on tablets and desktops ...
HydraVisibility(
visibleFrom: Breakpoint.medium,
child: const SideNavigation(),
)
// ... and a bottom bar everywhere else.
HydraVisibility(
visibleUntil: Breakpoint.small,
child: const BottomBar(),
)
Pass a replacement to show something else instead of nothing.
With a transition the child animates in and out, and the layout around it makes room gradually:
HydraVisibility(
visibleFrom: Breakpoint.medium,
transition: const HydraTransition.size(alignment: Alignment.centerLeft),
child: const SideNavigation(),
)
Grid #
HydraGrid is a GridView.count whose column count depends on the breakpoint:
HydraGrid(
mini: 1,
small: 2,
medium: 3,
large: 4,
mainAxisSpacing: 16,
crossAxisSpacing: 16,
children: cards,
)
padding, childAspectRatio, shrinkWrap and physics are passed through to the grid.
Inside a CustomScrollView, use HydraSliverGrid with the same column counts:
CustomScrollView(
slivers: [
const SliverAppBar(title: Text('Bestiary')),
HydraSliverGrid(mini: 2, large: 4, children: cards),
],
)
Both have a .builder constructor that builds the children on demand.
Breakpoints #
Default breakpoints: 600 (small), 900 (medium), 1200 (large). Everything below 600 is considered mini.
// Custom breakpoints
HydraWidget(
behaviour: const HydraBehaviour(
breakpointSmall: 480,
breakpointMedium: 768,
breakpointLarge: 1024,
),
small: mobileLayout(),
medium: tabletLayout(),
large: desktopLayout(),
)
Every Hydra widget and value accepts a behaviour.
Presets #
| Constructor | small / medium / large | Source |
|---|---|---|
HydraBehaviour() |
600 / 900 / 1200 | Hydra default |
HydraBehaviour.material() |
600 / 840 / 1200 | Material Design 3 |
HydraBehaviour.bootstrap() |
576 / 768 / 992 | Bootstrap 5 sm / md / lg |
HydraBehaviour.tailwind() |
640 / 768 / 1024 | Tailwind CSS sm / md / lg |
HydraBehaviour.cupertino() |
428 / 768 / 1024 | Apple device widths: iPhone Pro Max (428), iPad portrait, iPad landscape |
Options #
| Constructor | Description |
|---|---|
HydraBehaviour() |
Default: orientation-aware, prefers larger fallback |
HydraBehaviour.preferSmaller() |
Falls back to smaller alternative instead of larger |
HydraBehaviour.noOrientation() |
Uses shortest side regardless of orientation |
By default, Hydra uses MediaQuery.sizeOf(context).width which changes when the device rotates. Set isOrientationAware: false (or use HydraBehaviour.noOrientation()) to use the shortest side instead - the layout stays consistent regardless of rotation. The presets accept both flags as well.
Configure Once With HydraScope #
Instead of passing the same behaviour everywhere, put a HydraScope above your app:
HydraScope(
behaviour: const HydraBehaviour.material(),
child: MaterialApp(...),
)
Everything below it - HydraWidget, HydraValue, context.hydra, HydraBuilder, HydraVisibility, HydraGrid and Breakpoint.current - uses the scoped behaviour. A behaviour passed directly still wins.
Adapt to the Available Space #
Breakpoints follow the screen by default. A list in a 360 px side panel would get the large layout on a desktop. HydraLayoutScope resolves the breakpoint from the space its parent gives it instead, like a container query in CSS:
Row(
children: [
const SizedBox(width: 360, child: HydraLayoutScope(child: Inbox())),
Expanded(child: HydraLayoutScope(child: MessageView())),
],
)
Everything below it - HydraWidget, values, HydraGrid, HydraVisibility, HydraBuilder and Breakpoint.current - adapts to that space and rebuilds only when its breakpoint changes. Without a behaviour it uses the one of the enclosing HydraScope.
It needs a bounded width, so put it inside an Expanded or a sized box rather than directly into a Row or a horizontal list.
Performance #
Hydra reads the screen size with MediaQuery.sizeOf, so it ignores unrelated changes such as the keyboard or text scaling. Two things keep rebuilds down while a window is resized:
- Use a
HydraScope. It resolves the breakpoint once, from the screen size at its place in the tree, and everything below it rebuilds only when the breakpoint changes instead of on every pixel. AMediaQuerywith a different size below the scope, like a preview frame or a split view, needs aHydraScopeof its own. HydraBuilderrebuilds per breakpoint. Its builder only runs again when the breakpoint changes, with or without a scope.
For long lists, HydraGrid.builder builds its children on demand:
HydraGrid.builder(
mini: 2,
large: 4,
itemCount: products.length,
itemBuilder: (context, index) => ProductCard(products[index]),
)
HydraVisibility removes a hidden child from the tree. Set maintainState: true to keep it alive instead - its state survives, and it is not rebuilt from scratch when it comes back.
Testing #
hydra_test runs a widget test once per breakpoint:
final breakpoints = HydraVariant();
testWidgets('adapts the navigation', variant: breakpoints, (tester) async {
await tester.pumpWidget(const MyApp());
// breakpoints.currentValue is the breakpoint of this run.
});
How It Works #
- Breakpoint detection - determines the current breakpoint from screen width
- Exact match - looks for a widget or value registered at that breakpoint
- Nearest fallback - if no exact match, picks the closest available alternative (prefers larger by default, configurable via
isSmallerScreenPreferred)
Example #
The example app is the one from the animation above: a side navigation that slides in, a bottom bar that slides out, a search field that fades in and cards that glide to their new columns. Run it and resize the window:
cd example
flutter run -d chrome
License #
MIT
