Created
April 2, 2025 16:46
-
-
Save slightfoot/746dd67eb91717f318bcbbfa0ce5e13a to your computer and use it in GitHub Desktop.
Intrinsic Height PageView Example - by Simon Lightfoot :: #HumpdayQandA on 2nd April 2025 :: https://www.youtube.com/watch?v=U-XV0kZjI8Q
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2025 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(App()); | |
} | |
class App extends StatelessWidget { | |
const App({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: HomeScreen(), | |
); | |
} | |
} | |
class HomeScreen extends StatelessWidget { | |
const HomeScreen({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Material( | |
child: Placeholder( | |
child: Center( | |
child: IntrinsicHeightPageView( | |
children: [ | |
Page( | |
height: 280.0, | |
color: Colors.red.shade300, | |
), | |
Page( | |
height: 150.0, | |
color: Colors.green.shade300, | |
), | |
Page( | |
height: 320.0, | |
color: Colors.blue.shade300, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class IntrinsicHeightPageView extends StatelessWidget { | |
const IntrinsicHeightPageView({ | |
super.key, | |
this.crossAxisAlignment = CrossAxisAlignment.center, | |
required this.children, | |
}); | |
final CrossAxisAlignment crossAxisAlignment; | |
final List<Widget> children; | |
@override | |
Widget build(BuildContext context) { | |
return LayoutBuilder( | |
builder: (BuildContext context, BoxConstraints constraints) { | |
return ColoredBox( | |
color: Colors.yellow.shade300.withValues(alpha: 0.4), | |
child: SingleChildScrollView( | |
scrollDirection: Axis.horizontal, | |
physics: const PageScrollPhysics(), | |
child: IntrinsicHeight( | |
child: Row( | |
crossAxisAlignment: crossAxisAlignment, | |
children: [ | |
for (final child in children) // | |
Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 24.0), | |
child: SizedBox( | |
width: constraints.maxWidth - 48.0, | |
child: child, | |
), | |
), | |
], | |
), | |
), | |
), | |
); | |
}, | |
); | |
} | |
} | |
class Page extends StatelessWidget { | |
const Page({ | |
super.key, | |
required this.height, | |
required this.color, | |
}); | |
final double height; | |
final Color color; | |
@override | |
Widget build(BuildContext context) { | |
return SizedBox( | |
height: height, | |
child: ColoredBox( | |
color: color, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment