-
-
Save legalcodes/716612f4ae2d979cc5a2868e06c14e58 to your computer and use it in GitHub Desktop.
Resize a widget
This file contains 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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
class MyWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
mainAxisSize: MainAxisSize.max, | |
children: [ | |
BlueBox(), | |
SizedBox( | |
width: 100, | |
child: BlueBox(), | |
), | |
BlueBox(), | |
], | |
); | |
} | |
} | |
class BlueBox extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
width: 50, | |
height: 50, | |
decoration: BoxDecoration( | |
color: Colors.blue, | |
border: Border.all(), | |
), | |
); | |
} | |
} |
This file contains 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
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Directionality( | |
textDirection: TextDirection.ltr, | |
child: Container( | |
color: Color(0xffeeeeee), | |
child: Center( | |
child: Container( | |
child: MyWidget(), | |
color: Color(0xffcccccc), | |
), | |
), | |
), | |
); | |
} | |
} | |
Future<void> main() async { | |
final completer = Completer<void>(); | |
runApp(MyApp()); | |
WidgetsFlutterBinding.ensureInitialized() | |
.addPostFrameCallback((timestamp) async { | |
completer.complete(); | |
}); | |
await completer.future; | |
final controller = LiveWidgetController(WidgetsBinding.instance); | |
final rows = controller.widgetList(find.byType(Row)); | |
if (rows.length == 0) { | |
_result(false, ['Couldn\'t find a Row!']); | |
return; | |
} | |
if (rows.length > 1) { | |
_result(false, ['Found ${rows.length} Rows, rather than just one.']); | |
return; | |
} | |
final row = rows.first as Row; | |
if (row.mainAxisSize != MainAxisSize.max) { | |
_result(false, ['It\'s best to leave the mainAxisSize set to MainAxisSize.max, so there\'s space for the alignments to take effect.']); | |
return; | |
} | |
if (row.children.length != 3) { | |
_result(false, ['The Row should end up with three children.']); | |
return; | |
} | |
if (row.children[0] is! BlueBox) { | |
_result(false, ['The Row\'s first child should be a BlueBox widget.']); | |
return; | |
} | |
if (row.children[1] is! SizedBox) { | |
_result(false, ['The Row\'s second child should be a SizedBox widget.']); | |
return; | |
} | |
if (row.children[2] is! BlueBox) { | |
_result(false, ['The Row\'s third child should be a BlueBox widget.']); | |
return; | |
} | |
final sizedBox = row.children[1] as SizedBox; | |
if (sizedBox.width != 100) { | |
_result(false, ['The SizedBox should have a width of 100.']); | |
return; | |
} | |
if (sizedBox.height != 100) { | |
_result(false, ['The SizedBox widget resizes the BlueBox widget to 100 logical pixels wide. Add a height property inside SizedBox equal to 100 logical pixels.']); | |
return; | |
} | |
_result(true, ['The SizedBox widget resizes the BlueBox widget to 100 logical pixels wide and tall.']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment