Skip to content

Instantly share code, notes, and snippets.

@datafoya
Last active November 19, 2019 16:38
Show Gist options
  • Save datafoya/c3ac34ed8952724a0ecb0af1445c2af8 to your computer and use it in GitHub Desktop.
Save datafoya/c3ac34ed8952724a0ecb0af1445c2af8 to your computer and use it in GitHub Desktop.
Tweak the layout
import 'package:flutter_web/material.dart';
import 'package:flutter_web_test/flutter_web_test.dart';
import 'package:flutter_web_ui/ui.dart' as ui;
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.account_circle, size: 50)),
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Flutter McFlutter',
style: Theme.of(context).textTheme.headline),
Text('Experienced App Developer'),
],
),
],
);
}
}
import 'package:flutter_web/material.dart';
import 'package:flutter_web_test/flutter_web_test.dart';
import 'package:flutter_web_ui/ui.dart' as ui;
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.account_circle, size: 50),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Flutter McFlutter',
style: Theme.of(context).textTheme.headline,
),
Text(
'Experienced App Developer',
)
],
),
],
),
SizedBox(height: 8),
Row(),
SizedBox(height: 16),
Row(),
],
);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
scaffoldBackgroundColor: Color(0xffeeeeee),
textTheme: TextTheme(
body1: TextStyle(
fontSize: 16,
),
),
),
home: Scaffold(
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Container(
decoration: BoxDecoration(
color: Color(0xffffffff),
border: Border.all(),
boxShadow: [
BoxShadow(
blurRadius: 10,
color: Color(0x80000000),
),
],
),
padding: const EdgeInsets.all(8.0),
child: MyWidget(),
),
),
),
),
);
}
}
Future<void> main() async {
await ui.webOnlyInitializePlatform();
runApp(MyApp());
final controller = LiveWidgetController(WidgetsBinding.instance);
// Check MyWidget starts with one Column
final myWidgetElement = controller.element(find.byType(MyWidget));
final myWidgetChildElements = <Element>[];
myWidgetElement.visitChildElements((e) => myWidgetChildElements.add(e));
if (myWidgetChildElements.length != 1 ||
myWidgetChildElements[0].widget is! Column) {
_result(false, ['The root widget in MyWidget\'s build method should be a Column.']);
return;
}
// Check outermost Column has 5 correct children.
final outerColumnElement = myWidgetChildElements[0];
final outerColumnChildWidgets =
(outerColumnElement.widget as Column).children;
final outerColumnChildElements = <Element>[];
outerColumnElement.visitChildElements((e) => outerColumnChildElements.add(e));
if (outerColumnChildWidgets.length != 5 ||
outerColumnChildWidgets[0] is! Row ||
outerColumnChildWidgets[1] is! SizedBox ||
outerColumnChildWidgets[2] is! Row ||
outerColumnChildWidgets[3] is! SizedBox ||
outerColumnChildWidgets[4] is! Row) {
_result(false, ['The children of the outermost Column should be [Row, SizedBox, Row, SizedBox, Row] in that order.']);
return;
}
// Check outermost Column's properties
if ((outerColumnElement.widget as Column).mainAxisSize != MainAxisSize.min) {
_result(false, ['The outermost Column should use MainAxisSize.min for its mainAxisSize.']);
return;
}
if ((outerColumnElement.widget as Column).crossAxisAlignment !=
CrossAxisAlignment.stretch) {
_result(false, ['The outermost Column should use CrossAxisAlignment.stretch for its crossAxisAlignment.']);
return;
}
// Check first Row has two children: Padding and Column
final firstRowElement = outerColumnChildElements
.firstWhere((e) => e.widget == outerColumnChildWidgets[0]);
final firstRowChildElements = <Element>[];
firstRowElement.visitChildElements((e) => firstRowChildElements.add(e));
if (firstRowChildElements.length != 2 ||
firstRowChildElements[0].widget is! Padding ||
firstRowChildElements[1].widget is! Column) {
_result(false, ['The first Row should have two children: first a Padding, and then a Column.']);
return;
}
// Check Padding has correct padding
final paddingElement = firstRowChildElements[0];
if ((paddingElement.widget as Padding).padding != const EdgeInsets.all(8)) {
_result(false, ['The Padding widget in the first Row should have a padding of 8.']);
return;
}
// Check Padding has an Icon as its child
final paddingChildren = <Element>[];
paddingElement.visitChildElements((e) => paddingChildren.add(e));
if (paddingChildren.length != 1 || paddingChildren[0].widget is! Icon) {
_result(false, ['The Padding widget in the first Row should have an Icon as its child.']);
return;
}
// Check icon has a size of 50
if ((paddingChildren[0].widget as Icon).size != 50) {
_result(false, ['The Icon in the top-left corner should have a size of 50.']);
return;
}
// Check inner Column has correct properties
final innerColumnElement = firstRowChildElements[1];
final innerColumnWidget = innerColumnElement.widget as Column;
if (innerColumnWidget.crossAxisAlignment != CrossAxisAlignment.start) {
_result(false, ['The Column for the name and title should use CrossAxisAlignment.start as its crosAxisAlignment.']);
return;
}
if (innerColumnWidget.mainAxisSize != MainAxisSize.min) {
_result(false, ['The Column for the name and title should use MainAxisSize.min as its mainAxisSize.']);
return;
}
// Check inner Column has two Text children
if (innerColumnWidget.children.any((w) => w is! Text)) {
_result(false, ['The Column for the name and title should have two children, both Text widgets.']);
return;
}
// Check first Text has headline style
final nameText = innerColumnWidget.children[0] as Text;
if (nameText?.style?.fontSize != 24) {
_result(false, ['The Text widget for the name should use the "headline" textStyle.']);
return;
}
// Check first SizedBox has correct properties
final firstSizedBoxElement = outerColumnChildElements
.firstWhere((e) => e.widget == outerColumnChildWidgets[1]);
if ((firstSizedBoxElement.widget as SizedBox).height != 8) {
_result(false, ['The SizedBox before the first empty Row should have a height of 8.']);
return;
}
// Check second SizedBox has correct properties
final secondSizedBoxElement = outerColumnChildElements
.firstWhere((e) => e.widget == outerColumnChildWidgets[3]);
if ((secondSizedBoxElement.widget as SizedBox).height != 16) {
_result(false, ['The SizedBox between the first and second empty Rows should have a height of 16.']);
return;
}
_result(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment