NonHierarchicalViewBasedAlgorithm from android-maps-utils use pixel or dp on constructor params? - google-maps-android-api-3

The class NonHierarchicalViewBasedAlgorithm from googlemaps/android-maps-utils have a constructor:
public NonHierarchicalViewBasedAlgorithm(int screenWidth, int screenHeight) {
mViewWidth = screenWidth;
mViewHeight = screenHeight;
}
But Im not sure if I should send the width in pixels or in dp.
Any ideas?

Pretty sure all of the Google android map parts work in raw pixels, not dip.

From the source code of NonHierarchicalViewBasedAlgorithm class (https://github.com/googlemaps/android-maps-utils/blob/main/library/src/main/java/com/google/maps/android/clustering/algo/NonHierarchicalViewBasedAlgorithm.java):
/**
* #param screenWidth map width in dp
* #param screenHeight map height in dp
*/
public NonHierarchicalViewBasedAlgorithm(int screenWidth, int screenHeight) {
mViewWidth = screenWidth;
mViewHeight = screenHeight;
}

Related

Real height in flutter?

I am trying to retrieve the real height in Flutter. I tried different options:
MediaQuery.of(context).size.height * MediaQuery.of(context).devicePixelRatio
or
WidgetsBinding.instance.window.physicalSize.height
I add these 2 lines in a new Flutter app (from scratch, just the new counter app screen that flutter creates)
I also tried to use a global key, and it does not work (meaning by that that I get the same result). I am testing it in a Samsung a10, which, according to wikipedia, has a 720 x 1520 pixels. The width I have no problem in calculating it, but the height, is always giving me 1424.0. Why I am not getting the full height? Is happening me with more phone models.
Please see the documentation for the physicalSize property:
This value does not take into account any on-screen keyboards or other system UI. The padding and viewInsets properties provide information about how much of each side of the view may be obscured by system UI.
try to use this utility class it gives me the right result
class ScreenUtil {
static ScreenUtil instance = new ScreenUtil();
int width;
int height;
bool allowFontScaling;
static MediaQueryData _mediaQueryData;
static double _screenWidth;
static double _screenHeight;
static double _screenHeightNoPadding;
static double _pixelRatio;
static double _statusBarHeight;
static double _bottomBarHeight;
static double _textScaleFactor;
static Orientation _orientation;
ScreenUtil({
this.width = 1080,
this.height = 1920,
this.allowFontScaling = false,
});
static ScreenUtil getInstance() {
return instance;
}
void init(BuildContext context) {
MediaQueryData mediaQuery = MediaQuery.of(context);
_mediaQueryData = mediaQuery;
_pixelRatio = mediaQuery.devicePixelRatio;
_screenWidth = mediaQuery.size.width;
_screenHeight = mediaQuery.size.height;
_statusBarHeight = mediaQuery.padding.top;
_bottomBarHeight = mediaQuery.padding.bottom;
_textScaleFactor = mediaQuery.textScaleFactor;
_orientation = mediaQuery.orientation;
_screenHeightNoPadding =
mediaQuery.size.height - _statusBarHeight - _bottomBarHeight;
}
static MediaQueryData get mediaQueryData => _mediaQueryData;
static double get textScaleFactory => _textScaleFactor;
static double get pixelRatio => _pixelRatio;
static Orientation get orientation => _orientation;
static double get screenWidth => _screenWidth;
static double get screenHeight => _screenHeight;
static double get screenWidthPx => _screenWidth * _pixelRatio;
static double get screenHeightPx => _screenHeight * _pixelRatio;
static double get screenHeightNoPadding => _screenHeightNoPadding;
static double get statusBarHeight => _statusBarHeight * _pixelRatio;
static double get bottomBarHeight => _bottomBarHeight * _pixelRatio;
get scaleWidth => _screenWidth / instance.width;
get scaleHeight => _screenHeight / instance.height;
setWidth(int width) => width * scaleWidth;
setHeight(int height) => height * scaleHeight;
setSp(int fontSize) => allowFontScaling
? setWidth(fontSize)
: setWidth(fontSize) / _textScaleFactor;
}
in your build method first call
ScreenUtil().init(context);
then you can call ScreenUtil.screenHeight

The operator '*' can't be unconditionally invoked because the receiver can be 'null'. Try adding a null check to the target ('!')

I used this code for responsiveness in my UI. So what this code basically does is calculate the size of the screen and I use the functions below to put the exact font size according to the design provided to me in Figma or Adobe XD. Using this method, I was able to create pixel-perfect UI.
After upgrading to Flutter 2.0.3, I am getting null safety errors. I was able to solve most of them but I am not able to solve this error.
Please advice.
Complete Code
import 'package:flutter/material.dart';
class SizeConfig {
static MediaQueryData? _mediaQueryData;
static double? screenWidth;
static double? screenHeight;
static double? defaultSize;
static Orientation? orientation;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData!.size.width;
screenHeight = _mediaQueryData!.size.height;
orientation = _mediaQueryData!.orientation;
if (orientation == Orientation.landscape) {
defaultSize = screenHeight! * 0.024;
} else {
defaultSize = screenWidth! * 0.024;
}
}
}
double getSize(double size) {
var defaultsSize = SizeConfig.defaultSize * size;
return (defaultsSize / 10);
}
// Get the proportionate height as per screen size
double getProportionateScreenHeight(double inputHeight) {
double screenHeight = SizeConfig.screenHeight!;
// 812 is the layout height that designer use
return (inputHeight / 812.0) * screenHeight;
}
// Get the proportionate width as per screen size
double getProportionateScreenWidth(double inputWidth) {
double screenWidth = SizeConfig.screenWidth!;
// 375 is the layout width that Figma provides
return (inputWidth / 375.0) * screenWidth;
}
Error
Because SizeConfig.defaultSize is nullable, you need to make sure that its value should not be null.
You can add some assertion to notify the caller that SizeConfig should be initialized first. Then, you can change it to SizeConfig.defaultSize!.
Sample...
double getSize(double size) {
assert(
SizeConfig.defaultSize != null,
"SizeConfig should be initialized (only once) before calling getSize(...). Refer to SizeConfig.init(...).",
);
var defaultsSize = SizeConfig.defaultSize! * size;
return (defaultsSize / 10);
}
Problem:
You get this error because the object you're invoking * on can be null.
Example:
int? count = 1;
void main() {
print(count * 2); // error: The operator '*' can't be unconditionally invoked ...
}
Solutions:
Use a local variable:
int? count = 1;
void main() {
var i = count;
if (i != null) {
print(i * 2); // Prints 2
}
}
Use bang operator (!)
int? count = 1;
void main() {
print(count! * 2); // Prints 2
}
You have three options:
Use the bang operator:
int? count = 1;
void main() {
// will throw an error if count is null
print(count! * 2);
}
Use the ?? operator:
int? count = 1;
void main() {
// safe
print((count ?? 1) * 2);
}
Use an if - else statement:
int? count = 1;
void main() {
if(count != null) {
print(count! * 2);
} else {
print('count is null');
}
}

flutter specifying width, height, paddings, margins etc in percentages?

Is there a way in flutter which allows the widths, heights, paddings, margins etc being specified in percentages of screen dimensions instead of writing calculations in every widget?
Passing around a provider or extending some base class for this trivial thing is not feeling right.
The only known solution is to use MediaQuery. I implemented a helper class that I just call the methods for:
class SizeManager {
var _context;
double _screenHeight;
double _screenWidth;
SizeManager(this._context) {
_screenHeight = MediaQuery.of(_context).size.height;
_screenWidth = MediaQuery.of(_context).size.width;
}
double scaledHeight(double value) {
return value * _screenHeight / 100;
}
double scaledWidth(double value) {
return value * _screenWidth / 100;
}
}
In every build(), just before the return, call it like:
SizeManager sizeManager = new SizeManager(context);
And whenever you want to set margins/padding/size:
Container(
padding: EdgeInsets.symmetric(vertical: sizeManager.scaledHeight(2.5)), //Gives a 2.5 % height padding
),

Change size of text according to screen width in Flutter

I want the text size to remain the same however i want it to be bigger when on a larger screen width device.
MediaQueryData queryData;
#override
Widget build(BuildContext context) {
queryData = MediaQuery.of(context);
Text("Hello.",
style: TextStyle(
fontSize: queryData.size.width, color: Colors.white),
),
}
However, it became so big when i tried queryData.size.width. in this case i want the text to expand according to different devices screens.
You just need to multiply the result from the media query by a constant.
double width = MediaQuery.of(context).size.width;
return Text(
"Hello.",
style: TextStyle(
fontSize: width * 0.2,
color: Colors.white,
),
);
Define a class named sizeconfig:
class SizeConfig {
static MediaQueryData _mediaQueryData;
static double screenWidth;
static double screenHeight;
static double blockSizeHorizontal;
static double blockSizeVertical;
static double _safeAreaHorizontal;
static double _safeAreaVertical;
static double safeBlockHorizontal;
static double safeBlockVertical;
void init(BuildContext context) {
_mediaQueryData = MediaQuery.of(context);
screenWidth = _mediaQueryData.size.width;
screenHeight = _mediaQueryData.size.height;
blockSizeHorizontal = screenWidth / 100;
blockSizeVertical = screenHeight / 100;
_safeAreaHorizontal =
_mediaQueryData.padding.left + _mediaQueryData.padding.right;
_safeAreaVertical =
_mediaQueryData.padding.top + _mediaQueryData.padding.bottom;
safeBlockHorizontal = (screenWidth - _safeAreaHorizontal) / 100;
safeBlockVertical = (screenHeight - _safeAreaVertical) / 100;
}
}
And in your page initialize this class using SizeConfig().init(context)
then on your fontsize :SizeConfig.screenWidth * .02.
Your question is relevant also for Containers, Buttons, etc... So I suggest you to check how this amazing open source (using the alibaba redux) solved this issue using an utility-adapter file in several places like in the account header page.
Another excellent option is to check the flutter-screen-util dependency, they have a good example there.
You can use flutter_screenutil to set font size responsive to any screen layout also with this package you can make your other widgets responsive by its setWidth() and setHeight() methods,
You can check its documentation here : flutter_screenutil
We can use view vw and vh unit to provide font-size, which will make your text more visible with different viewport size.
h1 {
font-size: 5.9vw;
}
h2 {
font-size: 3.0vh;
}
p {
font-size: 2vmin;
}
1vw = 1% of viewport width
1vh = 1% of viewport height
1vmin = 1vw or 1vh, whichever is smaller
1vmax = 1vw or 1vh, whichever is larger

How to use classes and methods in Java

I'm fairly new to Java and coding, but up until this point in the Java material, I haven't ran into any problems. What I really can't wrap my head around is how classes and methods actually work. I've been attempting to implement this over the last several hours to no avail:
Implement the class called Cylinder shown in UML below. The constructor accepts and initializes the radius and height for the Cylinder, while accessors and mutators allow them to be changed after object construction. The class also include methods that calculate and return the volume and surface area of the Cylinder. Lastly, it contains a toString method that returns the name of the shape, its radius, and its height. Create a main method which instantiates 4 Cylinder objects (any parameters), display them with toString(), change one parameter (your choice) in each, and display them again.
UML:
This is the code that I currently have:
class Cylinder {
private double radius;
private double height;
// Method #1
private void rad (){
}
// Constructor
public Cylinder (double radius, double height){
this.radius = radius;
this.height = height;
}
// Method #2 for calculating volume.
double calcVolume (){
double volume = Math.PI * Math.pow(radius, 2) * height;
return volume;
}
// Method #3 for calculating surface area.
double calcArea (){
double area = (2 * Math.PI * radius * height) + (2 * Math.PI * Math.pow(radius, 2));
return area;
}
// toString method.
public String toString (){
StringBuilder sb = new Stringbuilder();
sb.append(radius).append(height);
return sb.toString();
}
}
public class Murray_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(5, "can");
System.out.println(cylinder1);
Cylinder cylinder2 = new Cylinder(6, "cup");
Cylinder cylinder3 = new Cylinder(7, "jar");
Cylinder cylinder4 = new Cylinder(8, "paper roll");
}
}
What I really don't understand is how to use the 'get' and 'set' methods. Additionally, I'm not completely sure how to implement the toString method.
The following errors that I can't figure out how to correct are:
The constructor Cylinder() is undefined for -
Cylinder cylinder1 = new Cylinder();
Stringbuilder can't be resolved to a type for -
StringBuilder sb = new Stringbuilder();
Thank you for your help!
What I really don't understand is how to use the 'get' and 'set' methods.
The purpose of getters and setters is for encapsulation. They allow you to get or set the values of the class' variables without having to declare them as being public.
For example, if you were to have
public double radius;
public double height;
You will be able to access them as
cylinder1.radius = 1;
cylinder2.height = 10;
int a = cylinder3.radius;
int b = cylinder3.height + cylinder4.radius;
etc.
Now if instead we had
private double radius;
private double height;
The above code will fail. Which is why we need getters and setters. As the names imply, getters "get" the variable.
public double getHeight() {
return height;
}
While setters "set" the variable.
public void setHeight(double height) {
this.height = height;
}
As for why we do it this way, theres a lot of information on that.
Additionally, I'm not completely sure how to implement the toString method.
As for how to implement the toString() method, all it has to do is return a String. But as for what the String contains, there is no hard rule for that. A good start would be to return the radius and height, like you have done.
The constructor Cylinder() is undefined for - Cylinder cylinder1 = new Cylinder();
Your constructor is public Cylinder (double radius, double height). However, you are trying to make a cylinder that's an int and a String.
Cylinder cylinder1 = new Cylinder(5, "can");
Either
Change the constructor to something like public Cylinder(double radius, String name); or
The instantiate your Cylinders with two doubles, a radius and a height.
e.g.
Cylinder cylinder1 = new Cylinder(1.0, 2.0);
Stringbuilder can't be resolved to a type for - StringBuilder sb = new Stringbuilder();
The only problem here is that you forgot to capitalize the b. It's StringBuilder not Stringbuilder.
An example of get and set methods:
double getRadius() {
return radius;
}
void setRadius(r) {
radius = r;
}
The constructor Cylinder() is undefined for - Cylinder cylinder1 = new
Cylinder();
Something is attempting to invoke the default constructor (a constructor with no parameters). You can either find out exactly where this error is called, or else add a default constructor:
Cylinder() {
this.radius = 0;
this.height = 0;
}
Stringbuilder can't be resolved to a type for - StringBuilder sb = new Stringbuilder();
StringBuilder is actually the class java.lang.StringBuilder. Place this text at the top of your file to help it resolve the name.
import java.lang.StringBuilder;