Skip to content

Instantly share code, notes, and snippets.

@monkeyswarm
Created August 1, 2021 23:55
Show Gist options
  • Save monkeyswarm/40ddb7f0f5736cdc6ee3204c6b9c684d to your computer and use it in GitHub Desktop.
Save monkeyswarm/40ddb7f0f5736cdc6ee3204c6b9c684d to your computer and use it in GitHub Desktop.
Track containers 3: list view with children changing content size
// List view, first and third changee height of their content.
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key? key, required this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ScrollController _scrollController = ScrollController();
double _scale(double value, double inLow, double inHigh, double outLow,
double outHigh) {
return (((value - inLow) / (inHigh - inLow)) * (outHigh - outLow) + outLow)
.clamp(min(outLow, outHigh), max(outLow, outHigh));
}
Widget _buildContainer(Color color, Color borderColor, double height) =>
Container(
height: height,
width:1000,
decoration: BoxDecoration(
color: color, border: Border.all(color: borderColor, width: 8)),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RaisedButton(onPressed: () {}, child: Text('foo')),
RaisedButton(onPressed: () {}, child: Text('foo')),
RaisedButton(onPressed: () {}, child: Text('foo')),
]));
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(children: [
Container(
color: Color(0x88888888),
height: 600,
child: ListView(controller: _scrollController, children: [
Container(
height: 600,
child: Align(
alignment: Alignment.bottomCenter,
child: AnimatedBuilder(
animation: _scrollController,
builder: (_, __) => _buildContainer(
Color(0x88FF0000),
Color(0xFFFF0000),
_scale(_scrollController.offset, 0, 450, 600,
150))))),
_buildContainer(Color(0x8800FF00), Color(0xFF00FF00), 300),
Container(
height: 600,
child: Align(
alignment: Alignment.topCenter,
child: AnimatedBuilder(
animation: _scrollController,
builder: (_, __) => _buildContainer(
Color(0x880000FF),
Color(0xFF0000FF),
_scale(_scrollController.offset, 450, 900, 150,
600))))),
])),
]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment