|
| 1 | +import 'dart:math'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_constraintlayout/src/constraint_layout.dart'; |
| 5 | + |
| 6 | +import 'custom_app_bar.dart'; |
| 7 | + |
| 8 | +class ArbitraryPositionExample extends StatefulWidget { |
| 9 | + const ArbitraryPositionExample({Key? key}) : super(key: key); |
| 10 | + |
| 11 | + @override |
| 12 | + State createState() => ArbitraryPositionExampleState(); |
| 13 | +} |
| 14 | + |
| 15 | +class ArbitraryPositionExampleState extends State<ArbitraryPositionExample> { |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return Scaffold( |
| 19 | + appBar: const CustomAppBar( |
| 20 | + title: 'Arbitrary Position', |
| 21 | + codePath: 'example/arbitrary_position.dart', |
| 22 | + ), |
| 23 | + body: ConstraintLayout( |
| 24 | + debugPrintLayoutTime: true, |
| 25 | + children: [ |
| 26 | + Container( |
| 27 | + color: Colors.black, |
| 28 | + child: const Text( |
| 29 | + 'Arbitrary position gives you more freedom\nadjust the window size to see the effect', |
| 30 | + style: TextStyle( |
| 31 | + color: Colors.white, |
| 32 | + fontSize: 16, |
| 33 | + ), |
| 34 | + textAlign: TextAlign.center, |
| 35 | + ), |
| 36 | + ).applyConstraint( |
| 37 | + width: matchParent, |
| 38 | + height: 50, |
| 39 | + top: parent.top, |
| 40 | + ), |
| 41 | + Container( |
| 42 | + color: Colors.orange, |
| 43 | + ).applyConstraint( |
| 44 | + size: matchConstraint, |
| 45 | + anchors: [sId(-1)], |
| 46 | + calcSizeCallback: (parent, anchors) { |
| 47 | + return const BoxConstraints.tightFor( |
| 48 | + width: 100, |
| 49 | + height: 100, |
| 50 | + ); |
| 51 | + }, |
| 52 | + calcOffsetCallback: (parent, self, anchors) { |
| 53 | + return Offset( |
| 54 | + min( |
| 55 | + max( |
| 56 | + 300, |
| 57 | + anchors[0].getRight() - |
| 58 | + anchors[0].getMeasuredWidth() / 2), |
| 59 | + 700), |
| 60 | + max(400, parent.size.height - self.getMeasuredHeight())); |
| 61 | + }, |
| 62 | + ) |
| 63 | + ], |
| 64 | + ), |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
0 commit comments