Last active
December 14, 2022 17:52
-
-
Save pingbird/fe42f1470dee2e46e5943267f0e7fa2c 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 'dart:math'; | |
import 'package:boxy/boxy.dart'; | |
import 'package:flutter/material.dart'; | |
const darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: const MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
const MyHomePage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatelessWidget { | |
const MyWidget({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MirroredRow( | |
body: Container(color: Colors.red, height: 50), | |
button: Container(color: Colors.purple, width: 150, height: 50), | |
); | |
} | |
} | |
class MirroredRow extends StatelessWidget { | |
const MirroredRow({ | |
Key? key, | |
required this.body, | |
required this.button, | |
}) : super(key: key); | |
final Widget body; | |
final Widget button; | |
@override | |
Widget build(BuildContext context) { | |
return CustomBoxy( | |
delegate: MirroredRowBoxy(), | |
children: [body, button], | |
); | |
} | |
} | |
class MirroredRowBoxy extends BoxyDelegate { | |
@override | |
Size layout() { | |
final body = children[0]; | |
final button = children[1]; | |
final width = constraints.maxWidth; | |
// Lay out button first to get its size | |
final buttonSize = button.layout(BoxConstraints( | |
maxWidth: width, | |
minHeight: constraints.minHeight, | |
maxHeight: constraints.maxHeight, | |
)); | |
// Lay out the body by subtracting the button's size from available width, | |
// also clamp it to 0 because it can go negative | |
final bodyWidth = max(0.0, width - buttonSize.width * 2); | |
final bodySize = body.layout(BoxConstraints( | |
maxWidth: bodyWidth, | |
minWidth: bodyWidth, | |
minHeight: constraints.minHeight, | |
maxHeight: constraints.maxHeight, | |
)); | |
// Position the button to the right side | |
// (or omit this line to put it on the left side) | |
button.position(Offset(width - buttonSize.width, 0)); | |
// Position the body in the center | |
body.position(Offset(buttonSize.width, 0)); | |
return Size( | |
width, | |
max(buttonSize.height, bodySize.height), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment