going through some flutter code and discover this flutter code
const _validatedFormzStatuses = <FormzStatus>{
FormzStatus.valid,
FormzStatus.submissionInProgress,
FormzStatus.submissionSuccess,
FormzStatus.submissionFailure,
FormzStatus.submissionCanceled,
};
can anyone share how this constant work?
the curly bracket part, and the generic type.
also any keyword i can use to find answer in the web? am struggling to determine the term of this constant format, thanks.
This const refers that's once u initialled any value to that variable then u can't change the value of that constants variables in future.
For Eg. A man have only 2 Hands and u can not changed man hands number.
so that's why we are using const in flutter or any programming language.
consider we have these three lines of code:
1.const EdgeInsets.all(25.0)
2.const EdgeInsets.all(25.0)
3.const EdgeInsets.all(25.0)
At first line EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget and then it creates a constant object with same value for rendering if it is found in another place.
Hey there is already an object with this value, so just render it.
Hey there is already an object with this value, so just render it.
Now, let us consider these scenario:
1.EdgeInsets.all(25.0)
2.EdgeInsets.all(25.0)
3.EdgeInsets.all(25.0)
At first line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
At the second line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
At third line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
So, by using const we can reduce the time for recreating the same object every time and using it, instead, we create an object once and then reuse it at every time we need.
Related
I am creating a Flutter app using the Flip Card Package. I would like to be able to flip a card and see a randomly selected image. Example: Flash cards, one side is a static image, I flip it to see a picture of a dog. I then tap again and it flips to back to the static image. I tap again, and I see a picture of a cat, etc...
I was able to get it all set up. The card flips back and forth but it is not randomly selecting a new card unless I restart the build after each flip. So, I am missing a piece somewhere that tells the app to pick again. Your help would be super appreciated!
Flip Card description https://pub.dev/packages/flip_card
snippet of code
Just as a tip as it seems you are new to SO - please post the code as text so that we can paste is in an IDE and try to run it. As an image its much harder to replicate your issue. Just a tip for going forward. Otherwise, welcome to SO!
So for your issue, I am not certain exactly why since I cannot see the entire context within that snippet BUT i suspect its because the card doesn't know it needs to rebuild since its just being replaced by another image but it doesn't know its a new image.
You can create a Widget class thats stateless and pass in the path. The Key change will ensure that the widget gets rebuilt:
The Widget class:
class FlipCardDetails extends StatelessWidget {
final String imagePath;
const FlipCardDetails({Key key, #required this.imagePath}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: Stack(
children: <Widget>[
Image.asset(
this.imagePath,
key: ValueKey<String>(imagePath),
)
],
)
);
}
}
Then in your main code where it says:
back: Container(child: _cardDetails()),
You can replace this with:
back: FlipCardDetails(imagePath: <Provide the image path>);
You can create a key based on the image path itself like so:
key: ValueKey<String>(imagePath)
This all would only work if there is a function you can run when the person clicks the card that would flip the card. That way you can run your randomizer and provide the image path to the widget.
Instead if you can't and the widget needs to calculate its own random path, then make it stateful, and on initState change the image path. That should work as well.
Without more context I can't really be sure if this is right or which solution works better. Let me know and I can to advise more.
I think that the problem is that your calling the 'Random' function only once when initializing your card. Try adding a function in which you call the Random function to change your '_random' and your 'element' and call it every time the card is flipped.You can do that by using the 'onFlipDone' function field of the FlipCard.
In gwt, I am creating a dynamic text area and now I want to read the value of the text area.
Can some help in me reading the value of dynamic created text area.
It all depends on which classes you use. The convenient way to read from com.google.gwt.user.client.ui.Label is to call getText(). But of cause the most generic way to get the interior of your "text area" is to call getElement().getInnerHTML() of your com.google.gwt.user.client.ui.UIObject object. For example:
FlowPanel myTextArea = new FlowPanel(); // FlowPanel extends Widget extends UIObject
myTextArea.add(someWidget);
GWT.log("Here is what I have inside myTextArea: " +
myTextArea.getElement().getInnerHTML());
A note of criticism. Normally, when you ask a question related to some custom UI, it should come along with at least few lines of code.
I have a flow panel with many photo-widgets inside (gallery with random number of rows and columns, depends on screen size) for which I want to implement drag and drop behavior to change their order. I am using gwt-dnd library. Its FlowPanelDropController allows you to define your own positioner (delimiter) which shows the candidate location for dropping the dragged widget.
I want this positioner to be the empty space with defined width, and the challenging thing is to implement sliding animation effect for the when positioner is added and removed.
If you are a desktop Picasa app user you know what I mean: the target row slides both sides (little to the left, little to the right) extending the space between the items where you are going to drop a photo.
The whole thing is complex enough, but any help related to how to apply the animation for positioner attach/detach is appreciated. Maybe I need to use a different approach (e.g., use GWT native dnd instead of gwt-dnd lib and no "positioners" at all) if you have any ideas how this could be helpful.
Thanks.
Well, I ended up overriding AbstractPositioningDropController (parent of FlowPanelDropController) and adding some extra features.
1) newPositioner() method now builds the Label, which is vertical space with some small width, height and decoration. This widget's element has constant id (say, "POSITIONER"), which helps to distinguish between multiple positioners if you plan to have several of them while navigating with a drag object over multiple drop targets. Also some transition CSS effects were applied to the Label, which will be responsible for handling animated extension of Label's width.
2) in onEnter() I do the following
...
removePositioner(getPositionerElement());
Widget positioner = newPositioner();
dropTarget.insert(positioner, targetIndex);
animatePositionerExtension();
where getPositionerElement() returns DOM.getElementById(POSITIONER)
At the same time removePositioner(..) resets the id of this element to something abstract and ideally should provide some animation before calling .removeFromParent(). But I didn't have enough time to properly debug this so ended up just removing the old positioner with no animation.
Method animatePositionerExtension() contains the code that changes the width of the positioner widget, so that CSS transition will catch that and provides animation.
All access to positioner widget in the class should be provided through updated methods.
3) onLeave() contains line removePositioner(getPositionerElement());
4) In the end of onMove() I added a couple of lines:
galleryWidget.extendHoveredRow(targetIndex - 1);
animatePositionerExtension();
where extendHoveredRow(hoveredWidgetOrdinal) implemented the logic to "limit" the sliding effect in the single line:
int rowHovered = -1;
public void extendHoveredRow(int hoveredWidgetOrdinal) {
int newRowHovered = getRowByOrdinalHovered(hoveredWidgetOrdinal);
if (rowHovered != newRowHovered) {
// adjust position of items in the previously hovered row
int firstInPreviouslyHoveredRow = (rowHovered - 1) * itemsInARow;
shiftFirstItemLeft(firstInPreviouslyHoveredRow, false);
rowHovered = newRowHovered;
// extend this row
int firstInThisRow = getOrdinalFirstInThisRowByOrdinal(hoveredWidgetOrdinal);
shiftFirstItemLeft(firstInThisRow, true);
}
}
This is in short how I did the thing. And still there's some room for improvements, like adding animated removal.
Again, it's all about overriding DropController and manipulations with elements inside the "gallery" widget. The benefit of this approach is that I remain in the gwt-dnd operations framework, and also reused a bunch of existent code.
Some notes:
CSS transition is not supported in IE pre-9, but this is unrelated to
this topic.
Put a transparent "glass" div on top of the Image widget if you use it
as a face of dragProxy. This will save you tons of time trying to
understand why either setting element's draggable to false, or
calling event.preventDefault() somewhere else, or other workarounds don't work in one or several browsers and the image itself is being dragged instead of the whole dragProxy widget.
I would like to print a GWT widget which extends Composite. This widget is composed of a grid whose cells are built with a ListDataProvider. When the user clic on a button print, the widget to print is built. Once this is done, I launch the print:
Element element = widgetToPrint.getElement();
String content = element.getInnerHTML();
print(content);
public static native boolean print(String content)
/*-{
var mywindow = window.open('', 'Printing', '');
mywindow.document.write('<html><head><title>Test</title>');
mywindow.document.write('<link rel="stylesheet" href="/public/stylesheets/ToPrintWidget.css" type="text/css" media="all"/></head><body>');
mywindow.document.write(content);
mywindow.document.write('</body></html>');
mywindow.print();
return true;
}-*/;
So, here is my problem:
The window which is opened by this method contains the core of the widget (built by the UI Binder), but some children are missing...
If I look inside the ListDataProvider and its related FlowPanel, the data are consistent, i.e. I've got several item in my list and in the flowPanel.
Consequently, it should be visible on the printing window...
I thought that maybe the problem was related to the method used to print the widget, so I also tried to add this widget into a dialogbox just before launching the print, to see if the widget was properly built... and it was.
So my widget displays well on a dialogbox, but if I try to give its innerHTML to the print method, by using getElement(), some widgets are missing... I've the feeling that the widgets which should have been built when the ListDataProvider changes are not properly set in the DOM... Somehow it works when I add the widget to a regular component, but it doesn't work when I have to give directly its innerHTML...
Do you have any idea ?
Thanks in advance.
Widgets are not just the sum of their elements, and DOM elements are not just the string that they are serialized to. Widgets are the element, and all events sunk to the dom to listen for any changes or interactions by the user. Elements then have callback functions or handlers they invoke when the user interacts with them.
By serializing the element (i.e. invoking getInnerHTML()), you are only reading out the structure of the dom, not the callbacks, and additionally not the styles set by CSS. This probably shouldn't be expected to work correctly, and as your experience is demonstrating, it doesn't.
As this is just a print window you are trying to create, event handling is probably not a concern. You just want the ability to see, but not interact with, the content that would be in that set of widgets. Styles are probably the main problem here then (though your question doesn't specify 'some children are missing' doesn't tell us what is missing, or give us any more clues as to why...) - you are adding one stylesheet in your JSNI code, but CellTable (which I assume you are using since you reference ListDataProvider) needs additional CssResource instances to appear correctly. I'm not sure how you can hijack those to draw in a new window.
Are you only using this to print content, not to let the user directly interact with the data? If so, consider another approach - use a SafeHtmlBuilder to create a giant, properly escaped string of content to draw in the new window.
String content = element.toString();
This will include all hierarchy elements in the node.
Just a reminder, all the GWT handlers will not work, and you have to sink all the events using DOM.
You might want to grab the outer HTML rather than the inner one.
GWT unfortunately has no getOuterHTML, but it's relatively easy to emulate.
If your widget is the only child within an element, then simply get the inner HTML of the parent element (w.getElement().getParentElement().getInnerHTML())
Otherwise, clone your widget's node add it to a newly created parent element, from which you'll be able to get the inner HTML:
DivElement temp = Document.get().createDivElement();
temp.appendChild(w.getElement().cloneNode(true));
return temp.getInnerHTML();
First thank you for your answers, it helped me to work out this problem.
I've almost solve the problem:
First, I do not use ListDataProvider anymore, because it wasn't clear for me when and how the view was refreshed. Instead I add my widgets by hand, which makes sense since, they are not going to move anyway.
Then, I define the style of my widgets using a common CSS stylesheet. However, in order to do it, I can't rely on CssResource, which was the way I was used to do it with GWT. I think that this comes from the JS method which gets lost by this kind of styles... Instead, I have to specify everything in a static CSS stylesheet, and to give it to the JS.
It works perfectly well, ie, I have my widgets, with thei styles, and I can print it.
But...
The color of some widgets depends on the color of the object that they represent. Consequently, I cannot write a generic CSS stylesheet... And as I said, I can't add a style using CssResource... Do you have any ideas on the way to handle that ?
To make sure I'm clear on the way I'm adding styles, here is an example:
Label l = new Label("Here is a cell in my grid to be printed");
l.addStyleName("PrintLineCell-kind_1");
With, in a public CSS stylesheet:
.PrintLineCell-kind_1{
background-color: red;
}
I hope there is a better way than to write 300 styles to cover 300 different colors...
Does gwt have a built in parser for converting css based strings to java constructs?
IE I'm interested in writing my own widget that takes for instance height/width:
public class MyWidget extends Composite{
private double width;
private Style.Unit widthUnit;
private double height;
private Style.Unit heightUnit;
/**
* #width - width of my widget in CSS dimensions (ie. "10px", "1em" ...)
* #height - height of my widget in CSS dimensions (ie. "10px", "1em" ...)
*/
public MyWidget(String width, String height){
// Parse out strings and set dimensions / units
}
}
I could easily do some regex on this but I thought I'd see if there was some sort of built in mechanism for this, I'd hate to re-invent...
Also, I of course thought about just accepting double width, Style.Unit widthUnit in the constructor, but UiObject's setWidth takes a CSS string, so I still have to do some String munging, albeit the other way around. Ideally I'd have 2 constructors to achieve both possibilities with a simple mechanism to parse the Strings / dimensions and Units properly.
EDIT:
Forgot to mention the REASON that i'd like to parse out these values:
I'm trying to write a simple scroll panel (think iphone nav) so when a user clicks a button, a new 'item' scrolls in from the right.
On the backend this would be:
1) Fetch new scroll item
2) Extend scroll wrapper to accompany new item (this is where i need actual widths)
2) Append to item scroll wrapper
3) Animate scroll wrapper (by sliding left the width of 1 item) to expose newly added item to visible section of panel wrapper
The reason I need original widths is because I'd like to extend my scroller panel by the width of the item added to it. If I'm just getting strings, I can't exactly say setWidth( currentWidth + itemWidth)
Basically I'm trying to mimic JQuery Tools Scroller
GWT currently uses Flute to parse CSS, however there is a discussion to migrate to CSS Parser (http://cssparser.sourceforge.net/). You could directly use one of those parsers.
BUT, for the use case you mention - I don't think you need anything that heavy. GWT actually doesn't parse the width and height strings, it just passes it to browsers DOM and then the browser evaluates it appropriately. You should do the same.
Meaning, your API just takes a string. The caller can pass ems or pxs or percentages. Your API isn't concerned, because it will just push it to the browser to understand.