Skip to content

Instantly share code, notes, and snippets.

@machinescream
Created July 5, 2020 22:00
Show Gist options
  • Save machinescream/d23ccba7faf6570d8049589b003a004c to your computer and use it in GitHub Desktop.
Save machinescream/d23ccba7faf6570d8049589b003a004c to your computer and use it in GitHub Desktop.
intrstc Altrntv
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var currentHeight = 0.0;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Material(
color: Colors.red,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 50,
height: currentHeight,
color: Colors.yellow,
),
MeasureSize(
onChange: (size){
setState(() {
currentHeight = size.height;
});
},
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('123', style: TextStyle(fontSize: 20)),
Text('123', style: TextStyle(fontSize: 20)),
],
),
),
],
),
),
),
);
}
}
typedef void OnWidgetSizeChange(Size size);
class MeasureSize extends StatefulWidget {
final Widget child;
final OnWidgetSizeChange onChange;
const MeasureSize({
Key key,
@required this.onChange,
@required this.child,
}) : super(key: key);
@override
_MeasureSizeState createState() => _MeasureSizeState();
}
class _MeasureSizeState extends State<MeasureSize> {
@override
Widget build(BuildContext context) {
SchedulerBinding.instance.addPostFrameCallback(postFrameCallback);
return Container(
key: widgetKey,
child: widget.child,
);
}
var widgetKey = GlobalKey();
var oldSize;
void postFrameCallback(_) {
var context = widgetKey.currentContext;
if (context == null) return;
var newSize = context.size;
if (oldSize == newSize) return;
oldSize = newSize;
widget.onChange(newSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment