Skip to content

Instantly share code, notes, and snippets.

@ditman
Created August 23, 2019 21:47
Show Gist options
  • Save ditman/2d7cc6686a72b859528ae8bc6192f1dc to your computer and use it in GitHub Desktop.
Save ditman/2d7cc6686a72b859528ae8bc6192f1dc to your computer and use it in GitHub Desktop.
This gist is a small Flutter app that reproduces a (re)layout error. Use this as your main.dart in a hello_world app.
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: RelayoutBoundariesCrash(),
);
}
}
class RelayoutBoundariesCrash extends StatefulWidget {
RelayoutBoundariesCrash({Key key}) : super(key: key);
@override
_RelayoutBoundariesCrashState createState() => _RelayoutBoundariesCrashState();
}
class _RelayoutBoundariesCrashState extends State<RelayoutBoundariesCrash> {
bool _visible;
void _toggleVisibility() {
setState(() {
_visible = !_visible;
});
}
_RelayoutBoundariesCrashState() {
_visible = true;
// Toggle visibility automatically, doesn't matter how you toggle this
Timer(Duration(milliseconds: 1000), () {
_toggleVisibility();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox( // Must be a SizedBox (?)
// *both* width AND height need to end up as 0x0
width: !_visible ? 0 : null,
height: !_visible ? 0 : null,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
final double width = !_visible ? 0 : 100;
final double height = !_visible ? 0 : 100;
return Column( // Also fails with Row
children: <Widget>[
SizedBox(
width: width,
height: height,
child: Text("Anything"),
),
SizedBox( // This SizedBox throws the assert
width: width,
height: height,
child: Text("Something else"),
),
],
);
}),
),
// floatingActionButton: FloatingActionButton( // Toggle manually
// onPressed: _toggleVisibility,
// tooltip: 'Crash',
// child: _visible ? Icon(Icons.visibility_off) : Icon(Icons.visibility),
// ),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment