How to make widget to occupy all the available space in Gtk.HBox - gtk

I am implementing a custom Gtk# widget which is based on Gtk.EventBox. When I am inserting it into the HBox or VBox it occupies the exact size that is returned by OnSizeRequested method.
How can I make my widget to occupy all the space given to it by the parent box, window or the widget? Just like HBox does.

There is a slight different between the preferred way to do packing in GTK+2 vs GTK+3. With GTK+ you would typically use expand and fill properties of a GtkBox to control how space is allocated. With GTK+3 they are suggesting the user of vertical-expand, horizontal-expand, vertical-fill, horizontal-fill.
A good way to understand how packing works is to play with the fill and expand properties with Glade so you can see the effects in real time. An old tutorial (slightly out of date) shows some screenshots of different packing properties: How_Packing_Effects_the_Layout
As you are developing a widget it is more likely that users of your widget will determine how it should be packed in a larger UI. However, if you're widget is a composite widget (built from other widgets) then you will need to pack the other widgets properly.

PackStart and PackEnd have a fill parameter to specify items that should expand to fill the box. You probably want PackStart(widget, true, true, 0);

Related

How to use SizeTransition with Text inside a Row without overflows

Problem summary
I'm building simple animation in which, simply, a panel expands to the right on a onTap event. The layout can be summarized as follows:
The panel has many tiles. Each tile has its own:
leading icon
title text
trailing icon
Each tile, when expanded, shows the three widgets above
Each tile, when shrinked, just shows the leading icon
The panel is scrollable to hold many icons such as these
At the end of the panel there's a simple icon which, when tapped, triggers the "expand" or "shrink" animation
The animation is built with an AnimatedContainer on the panel and a SizeTransition on the single tiles (via one single controller on the parent class)
The problem is simple: when the tiles shrink, their inner text overflows in the row.
Sample code
I just made this reproducible example on DartPad.
The obvious solution isn't helping me out
The most obvious solution is to simply wrap the Text inside a Flexible Widget: this advised by Flutter's docs and it makes sense, but unluckily it completely breaks my Layout.
Flexible introduces a flex factor that in this context is 100% unwanted.
I want my leading icons to be always at the end of the row, so my Spacer widget should "prevail" there.
I can't just play with flex factors there, as it would unintendedly hide text depending on its value.
Another code smell
Another thing I really don't like is the fact that I have to build a controller on the parent class, but I'm not actually using it on the parent class (I'm just exploiting it so I can give it to the children).
I'm new to animations in Flutter so I'm kinda confused on how I should orchestrate the whole thing here.
Any help will be appreciated / upvoted / accepted. Thank you!
As far as I understood you in a right way you just need set sizes for Row inside of SizeTransition instead of Container sizes
Here is your modified sample https://dartpad.dev/?id=a2408d29a1e8c6ce7a1cef8f21e7491d
I'd try an OverflowBox or a FittedBox (wrapping your text), depending on the result you want to achieve.

Is there a way to calculate a dynamically sized widget?

I'd like to add a custom showMore widget if the skills widget which has a dynamic size(width) exceeds the screen width. On clicking the showMore widget it should show all the skills in a wrap .else show less.
In flutter
Constraints go down and sizes go up
See this documentation.
Flutter uses a single pass algorithm to render your application. This is a technical choice to ensure performance but it comes with limitations.
One of them is that, when you are building the widget tree, you only have access to the constraints of the parent, and not any size of any widget (since they are not rendered yet).
So a short answer to your question is:
No, you cannot do what you are trying to do (displaying something if some widgets are not fitting on the screen) since you don't have access to any sizes in the build method.
An alternative solution would be to use Wrap to wrap your chips or use a ListView on the horizontal axis to make the list of chips horizontally scrollable.
Anyway, if you really want to do this, you can hardcode the sizes of your chip and access the device size with MediaQuery.of(context).size or by using the LayoutBuilder and using contraints.maxWidth as the parent's width. Then you can check whether or not numberOfChips * chipSize <= maxWidth. But I wouldn't recommend it as the design wouldn't be responsive:
All the chips will have the same size, so you'll end up with a big chip for "c" and maybe a long name like "python" won't fit in and you'll end up with overflow issues.
What if the user changes the font size of his device? You will also end up with overflow issues.

Is there a way to know the dimensions of a widget before laying it out on the screen?

I'm trying to implement a breadcrumb widget in my Flutter app and I would like to achieve the following behavior:
Let's say the red outlined area is the space I have for my breadcrumb and that each element of the breadcrumb has a different width. The breadcrumb should be responsive, i.e. it should show elements as long as there is enough space to show them, otherwise, remove some of the elements and add that '...' button in the middle. Clicking on that button shows an overlay that contains the removed elements.
Now, I want to know how much space each breadcrumb element will take before it is laid out on the screen. Only then, I can decide whether there is enough space for it to be shown in the breadcrumb or it should be part of the '...' button.
I tried to use CustomMultiChildLayout which gives you information about how much space a widget takes after laying it out. It also needs to layout each child once, which makes it unuseful in my use case.
P.S: Think about the breadcrumb used in the apple documentation. That's the same behavior I want to achieve.
Short answer, you can't.
You were already using CustomMultiChildLayout, that's good, it shows me that you should have some basic understanding of how flutter rendering pipeline works. Then it should be clear to you of the 3 steps: parent passes down constraints, child reports its size, parent decides where to place the child. So in short, again, you cannot get children's size before layout.
Now move on to the thing you want to achieve. I agree CustomMultiChildLayout cannot achieve what you want to do, but not for the reason you listed. The main problem is CustomMultiChildLayout forces you to layout each child once, and then position them. You must position each child, you cannot skip a child if you don't like it (too big), and you cannot fabricate new stuff that's not your children. If you could achieve these 2 things, then getting children's size after layout isn't going to be a problem.
To achieve those, the easiest way is to use CustomPaint, if everything you need to do, are text-based or just simple shapes. Use TextPainter to layout a text, get its size, then decide whether to paint that text, or paint "..." (skip a child and fabricate your own) instead. You can search on how to do these things easily, but mostly search on TextPainter.
If you want to paint other widgets as your children, and optionally skip some of them while fabricating others, you should write your own RenderObject. That is one level lower than "widgets-level". If you have never done that before, you can research on that topic as well.

overlapping widgets gtk

How can i make widgets overlap one another.
Lower most should be image, rest above can be other widgets like buttons.
Subclass the larger(parent) widget. In a create() method or in the constructor, add a layout( or container) widget to the parent widget, then inset the others into the container. Now threat this new subclass as if it were a single, but specialized, version of its super class.
A Window is an example of a parent widget, while Fixed is an example container. A child could be an EventBox enclosing an Image. The composite of all these is a new window object that has pictures that can be clicked.
For the case of a window's titlebar look with a pixmap background, and buttons, try a Window with an Image and a Fixed container to hold the buttons. The Fixed and the Image should be able to overlap as the Fixed is transparent, and an Image has no Window.
If Buttons are truly what's needed, have a look at Button Boxes and Toolbars in the list of GTK Containers. It may be possible to add an Image background to one of those.
A different approach involves an Alignment Widget(from the same list). It specifies where the smaller widgets are positioned and sized in a proportioned manner.
I assumed, OOP, but if it's not, just organize the creation of the widgets from one function. I've made composite widgets functionally in Haskell(Gtk2Hs), and in Guile Gnome Platform (with and without OOP)

how to zoom in & out in GWT

i created a widget, say MyBox, which has many other widgets. i have to use that widget on different page, however on different page, the size of the widget is different, on some page it's smaller, on other page it's bigger. Just wonder is there any way in GWT to zoom in & out the widget? Thanks
You can set the width and height of a widget, and the normal kinds of "zooming" that happen with HTML will happen to your widget too. If you want inner widgets to resize automatically, check out Layout Panels in GWT 2.0 and later.