-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Adding relative position support to Stack widget #24475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
With the What do you think about it @Hixie? |
|
@letsar What's the motivating use case here? Can you describe in more detail what you are trying to solve? (as per https://github.com/flutter/flutter/wiki/Tree-hygiene#overview normally we'd file a bug first for API changes) I can imagine doing something similar to what you describe today using a combination of Align and FractionallySizedBox, but I'm not sure it would solve the same problem you are having. It's hard to evaluate proposals without a concrete use case. |
|
(props for writing tests by the way! thanks!) |
|
(@goderbauer and I spoke about the design of this one... we think it's an issue that the ParentData gets mutated by the |
|
Hi @Hixie, sorry I should have created an issue for this before. I will do better next time. This is my use case: So I thought it could be a good idea to add a way to create a fractional Positioned widget. I played with the Positioned widget and found a way to do it, so I made this PR. |
|
This seems to be doing what you describe with import 'package:flutter/material.dart';
void main() => runApp(MyDemoWidget());
class MyDemoWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
// The child should have a width of 70% of the stack's width.
const double widthFactor = 0.7;
// The child should have a height of 65% of the stack's height.
const double heightFactor = 0.65;
// The child's left edge should be indented at 20% of the stack's width.
const double leftFactor = 0.2;
// The child's top edge should be indented at 5% of the stack's height.
const double topFactor = 0.05;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('DEMO'),
),
body: Center(
child: SizedBox(
height: 100.0,
width: 100.0,
child: Stack(
children: <Widget>[
Container(
color: Colors.green,
),
Align(
alignment: FractionalOffset(
leftFactor / (1.0 - widthFactor),
topFactor / (1.0 - heightFactor),
),
child: FractionallySizedBox(
widthFactor: widthFactor,
heightFactor: heightFactor,
child: Container(
color: Colors.yellow,
)
),
)
],
),
)
),
),
);
}
}The math for the alignment is a little ugly. Maybe we could encapsulate the |
|
Oh great!!! It looks like my PR wasn't needed after all, I really should have created an issue before 🙂 . Yes the math is not really straightforward 😄 , and I think it would be better if we have a widget to encapsulate this like you suggested. The advantage or your solution is that it's also working outside a Stack 👍 . |
|
@letsar Would it be ok to close this PR then and you file a feature request for such a widget at https://github.com/flutter/flutter/issues/new/choose requesting such a Widget? We can then discuss names and how that would work with text directionality over there. |
|
@goderbauer Yes no problem. I will file this feature request. |
In some cases it can be helpful to position a widget inside a
Stackrelatively to the size of theStack.This PR adds a way to do this by setting a new value of the
Positionedwidget (isRelative) to true.