Display Int or Double Widget Equivalent to Text()? - flutter

I want to display a double (or int) similar to how you would a string with a Text() widget. Is there an equivalent to this widget for ints or doubles in Flutter? Sorry if this is obvious, but I could not find anything.

Text("${<Your double/int variable>"})

You use a Text widget for this as well, simply use string interpolation to display your variables as a String:
final int two = 2;
final double pi = 3.14;
return Text("$two is an integer, and $pi is a double.");
You can also use the toString() method on any object in Dart (which is what the string interpolation above does implicitly). So if you want to print a double by itself, it's possible to write:
Text(pi.toString());
More on Strings and interpolation

Related

Are logical pixels the units returned by the dx property of the Offset class?

Does the dx property of the Offset class return the double value of the logical pixels on the x-axis? I need to know if I can measure say a percentage of the dx against MediaQuery.of(context).size.width?
Short answer: The dx property of an Offset object and the Width size of a Media Query are both doubles (pixel, yes) thus you can divide, multiply or do any math operation with them (separately or with each other). So, you can do your OffsetObject.dx/MediaQuery(context).Size.Width to get the percentage. You can run the code block below on Dart pad.
Loooong Answer: Not sure it adds more info.
Yes, of course you can measure the dx property of an Offset object against the width property of a MediaQuery because they are both of type double.
The mere fact that they are both of type doubles should allow you to carry out any math/arithmetic calculation you need. See the sample app below. the text returns the numbers (you can run it on Dartpad). I explained the variables/objects defined below as well.
ScreenWidthSize: the MediaQuery width, note the type is double. MediaQuery.of(context).size.width returns an object of type double.
offsetMagnitude: This defines the Offset object (I am not sure how you will get your Offset object so this for illustration. Then I need to get the dx.
horizontalOffset: This defines/extracts the dx property of the offsetMagnitude, the offset object. Note it is a double too.
horizontalOffsetToScreenWidthRatio: Since both ScreenWidthSize and horizontalOffset are doubles, I can carry out any mathematical operation, as long as they are applicable to the type double. I have therefore measured the percentage (we can format the result better but you get it).
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
double screenWidthSize = MediaQuery.of(context).size.width;
Offset offsetMagnitude = Offset(12,25);
double horizontalOffset = offsetMagnitude.dx;
double horizontalOffsetToScreenWidthRatio = offsetMagnitude.dx/screenWidthSize;
return Text(
'Hello, my screen is $screenWidthSize wide and my horizontal offset is $horizontalOffset, so the horizontal offset to screen width ratio is $horizontalOffsetToScreenWidthRatio',
style: Theme.of(context).textTheme.headline4,
);
}
}
/* Not sure what you are trying to achieve but it seems like some form of adaptive or responsive operation. I think if you check the docs, you will find a close widget needing only little manipulation to achieve what you want. Check the Align widget, might be what you need*/

When I use list.filled() and initialize the value, why do I still prompt for syntax errors?

When I use list.filled() and initialize the value, why do I still prompt for syntax errors?
enter image description here
you can not fill list of widget with number
List<Widget> list= List.filled(10,SizedBox.shrink());
If you want List of int, define List as int.
You defined a List of Widget but filled by int. See this enter link description here
List<int> list= List.filled(0,10);
// or
List<Widget> list= List.filled(0,Container());
This happens because you declared your type of your List as Widget.
List<Widget> list = List.filled(0, Container());
If you want it as type int then declare it as the following.
List<int> list = List.filled(0, 10);
as you have declared list with data type widget it can only store widgets like
List<Widgets> widgets=[Container(),Container(),Container()];
and if you need to store int as data type use this code
List<int> intList=[1,2,3,4,5];
by the way dart is smart you do not need to specify data type you can use it just like
List data =[]; //store anything here

Replace all element by image in flutter

I try to replace all patterns in a string by Flutter Image.network.
I do this:
String text = ":smiley: very goog"
text.replaceAll(":smiley:", Image.Network(src))
It does not work because replaceAll second argument must a text not a widget.

How to pass a variable (ui object) in flutter/dart?

im a very newbie to flutter integration testing. For now I only know how to write basic sheets of finding elements on a screen and make some actions to them.
Can please anyone help me on how to pass a variable in a function if i want to reuse it? Example:
static Future<void> createProjectFromWizard(FlutterDriver driver, String projectName, bool direct) async {
final faker = new Faker();
final buttonStart = find.byValueKey(OnboardingIds.buttonStart);
await driver.waitFor(buttonStart).then((_) => driver.tap(buttonStart));
I want to reuse this function but instead of final buttonStart I need to find another ui-button. How can I do this without re-writing this function? (Because it is a very long one, i pasted a short piece)

What's the difference between double.infinity and double.maxFinite in Dart?

What's the difference between double.infinity and double.maxFinite in Dart?
In Flutter, it looks like both do the same thing. When should I use each of them in Flutter?
The difference can be summarized into:
I want to be as big as my parent allows (double.infinity)
Some Widgets allow their children to be as big as they want to be
Column,
ListView,
OverflowBox
In that situation use double.infinity
According to the documentation, these are the values assigned to the various double constants.
static const double infinity = 1.0 / 0.0;
static const double negativeInfinity = -infinity;
static const double minPositive = 5e-324;
static const double maxFinite = 1.7976931348623157e+308;
I hope this answers your question