Last active
August 22, 2020 03:55
-
-
Save guilherme-v/b2711aa6cb96d74441432df46f7447ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
GlobalKey _keyMyApp = GlobalKey(); | |
GlobalKey _keyMaterialApp = GlobalKey(); | |
GlobalKey _keyHomePage = GlobalKey(); | |
GlobalKey _keyScaffold = GlobalKey(); | |
GlobalKey _keyAppbar = GlobalKey(); | |
GlobalKey _keyCenter = GlobalKey(); | |
GlobalKey _keyFAB = GlobalKey(); | |
GlobalKey _keyText = GlobalKey(); | |
void printConstraint(String name, BoxConstraints c) { | |
print( | |
'CONSTRAINT of $name: min(w=${c.minWidth.toInt()},h=${c.minHeight.toInt()}) max(w=${c.maxWidth.toInt()},h=${c.maxHeight.toInt()})', | |
); | |
} | |
void printSizes() { | |
printSize('MyApp', _keyMyApp); | |
printSize('MaterialApp', _keyMaterialApp); | |
printSize('HomePage', _keyHomePage); | |
printSize('Scaffold', _keyScaffold); | |
printSize('Appbar', _keyAppbar); | |
printSize('Center', _keyCenter); | |
printSize('Text', _keyText); | |
printSize('FAB', _keyFAB); | |
} | |
void printSize(String name, GlobalKey key) { | |
final RenderBox renderBox = key.currentContext.findRenderObject(); | |
final size = renderBox.size; | |
print("SIZE of $name: w=${size.width.toInt()},h=${size.height.toInt()}"); | |
} | |
void printPositions() { | |
printPosition('MyApp', _keyMyApp); | |
printPosition('MaterialApp', _keyMaterialApp); | |
printPosition('HomePage', _keyHomePage); | |
printPosition('Scaffold', _keyScaffold); | |
printPosition('Appbar', _keyAppbar); | |
printPosition('Center', _keyCenter); | |
printPosition('Text', _keyText); | |
printPosition('FAB', _keyFAB); | |
} | |
void printPosition(String name, GlobalKey key) { | |
final RenderBox renderBox = key.currentContext.findRenderObject(); | |
final position = renderBox.localToGlobal(Offset.zero); | |
print("POSITION of $name: $position "); | |
} | |
void main() { | |
runApp(LayoutBuilder( | |
builder: (context, constraints) { | |
printConstraint('MyApp', constraints); | |
return MyApp(); | |
}, | |
)); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return LayoutBuilder( | |
key: _keyMyApp, | |
builder: (context, constraints) { | |
printConstraint('MaterialApp', constraints); | |
return MaterialApp( | |
key: _keyMaterialApp, | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: LayoutBuilder( | |
builder: (context, constraints) { | |
printConstraint('HomePage', constraints); | |
return HomePage( | |
key: _keyHomePage, | |
title: 'Flutter Demo Home Page', | |
); | |
}, | |
), | |
); | |
}, | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
HomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
void initState() { | |
WidgetsBinding.instance.addPostFrameCallback(_afterLayout); | |
super.initState(); | |
} | |
void _afterLayout(_) { | |
printSizes(); | |
printPositions(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return LayoutBuilder(builder: (context, constraints) { | |
printConstraint('Scaffold', constraints); | |
return Scaffold( | |
backgroundColor: Colors.purple, | |
key: _keyScaffold, | |
appBar: AppBar( | |
key: _keyAppbar, | |
title: Text(widget.title), | |
), | |
body: LayoutBuilder( | |
builder: (context, constraints) { | |
printConstraint('Center', constraints); | |
return Center( | |
key: _keyCenter, | |
child: LayoutBuilder(builder: (context, constraints) { | |
printConstraint('Text', constraints); | |
return Text( | |
'You have pushed the button this many times:', | |
key: _keyText, | |
style: TextStyle(color: Colors.white), | |
); | |
}), | |
); | |
}, | |
), | |
floatingActionButton: LayoutBuilder( | |
builder: (context, constraints) { | |
printConstraint('FAB', constraints); | |
return FloatingActionButton( | |
key: _keyFAB, | |
onPressed: printSizes, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
); | |
}, | |
), | |
); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment