Unfortunately, the DOM does not provide such functionality. This is why most libraries, such as jQuery, ExtJS, etc., provide helper methods. They essentially analyze the style and define it.
Here is an example:
<div style="width: 100px; height: 100px; padding: 5px; border: 1px solid #000"></div>
int getWidth(Element element) { var paddingLeft = element.style.paddingLeft.replaceAll('px', ''); // You may want to deal with different units. var paddingRight = element.style.paddingLeft.replaceAll('px', ''); return element.clientWidth - int.parse(paddingLeft) - int.parse(paddingRight); }
And use:
print(getWidth(query('#test')));
Result:
100
Notes:
- You can deal with different types of units (px, pt, em, ...).
- The
box-sizing property also has an effect that you might want to test.
If I or you happen to find the time, maybe release the Pub package or something else. :)
Kai sellgren
source share