I want to display a text widget using dynamic values in flutter app.
Here's my code.
class _DetailsPageState extends State<DetailsPage> {
var descVar;
#override
void initState() {
var pick = widget.classDesc;
var picker = "ClassesInfo."+pick;
setState(() {
descVar = picker;
print(descVar);
});
}
...
In the build:
Text('${this.descVar}')
I wish to get value from external file:
class ClassesInfo {
static const String desc_class1 = "Sahaja Yoga";
}
I am just getting the string output, but not the value from the variable!
Desired Output : Sahaja Yoga
Getting Now: ClassesInfo.desc_class1
Even if i print the value in class it is getting : ClassesInfo.desc_class1
as i understood you want to print in text field value of desc_class const. So you can simply write:
Text(ClassesInfo.desc_class1)
Related
I am defining Strings in a class TextFile as
class TextFile {
..
..
**static const String TIMER = "Timer";**
}
And I am using this in another file as
TextFile.TIMER
There is no Text Widget Used
I am using it in a model defined as,
class SideMenuModel {
String name;
dynamic icon;
bool selected;
String imgpath;
String whiteimgpath;
SideMenuModel(this.name,
this.icon,
this.selected,
{this.imgpath,this.whiteimgpath});
}
and then appending that to a list. Like,
_navigationList.add(SideMenuModel(TextFile.TIMER, Icons.timer,
false,imgpath:"image/Timer.png",whiteimgpath:"image/newTimer.png"));
Now, how do I change the color of this String?
Please Respond. I am Stuck.
I am trying to get the input from a text box from the backoffice console.What is the proper way to get the input?
I tried following the link:
https://hybrisdiary.com/2018/10/15/backoffice-customization/
public class MyCustomInputController extends DefaultWidgetController
{
private Textbox textInput;
#Override
public void initialize(final Component comp)
{
super.initialize(comp);
}
#ViewEvent(componentID = "submitButton", eventName = Events.ON_CLICK)
public void doOperation()
{
textInput.getText()
//This is throwing a Null Pointer Exception
}
}
Expected result : Getting input from the following field:
<textbox id="textInput"/>
Actual result : Null pointer rexception
Individual back-office elements must be declared with #Wire annotation.
#Wire
private Textbox textInput;
I am loading a groovy script and I need the following class:
import java.util.function.Function
import java.util.function.Supplier
class MaskingPrintStream extends PrintStream {
private final Function<String,String> subFunction
private final Supplier<String> secretText
public MaskingPrintStream(PrintStream out, Supplier<String> secretText, Function<String,String> subFunction) {
super(out);
this.subFunction = subFunction;
this.secretText = secretText;
}
#Override
public void write(byte b[], int off, int len) {
String out = new String(b,off,len);
String secret = secretText.get();
byte[] dump = out.replace(secret, subFunction.apply(secret)).getBytes();
super.write(dump,0,dump.length);
}
}
right now I have it in a file called MaskingPrintStream.groovy. But, by doing this I effectively can only access this class as an inner class of the class that gets created by default that corresponds to the file name.
What I want to work is code more like this:
def stream = evaluate(new File(ClassLoader.getSystemResource('MaskingPrintStream.groovy').file))
But as you can see, I need to give it some values before it's ready. Perhaps I could load the class into the JVM (not sure how from another groovy script) and then instantiate it the old-fashioned way?
The other problem is: How do I set it up so I don't have this nested class arrangement?
groovy.lang.Script#evaluate(java.lang.String) has a bit different purpose.
For your case you need to use groovy.lang.GroovyClassLoader that is able to parse groovy classes from source, compile and load them. Here's example for your code:
def groovyClassLoader = new GroovyClassLoader(this.class.classLoader) // keep for loading other classes as well
groovyClassLoader.parseClass('MaskingPrintStream.groovy')
def buf = new ByteArrayOutputStream()
def maskingStream = new MaskingPrintStream(new PrintStream(buf), { 'secret' }, { 'XXXXX' })
maskingStream.with {
append 'some text '
append 'secret '
append 'super-duper secret '
append 'other text'
}
maskingStream.close()
println "buf = ${buf}"
And the output it produces in bash shell:
> ./my-script.groovy
buf = some text XXXXX super-duper XXXXX other text
I am kinda new to AS3, and I want to figure out how classes and strings work. I have two files:
Main.as:
package
{
import flash.display.*;
public class Main extends Sprite
{
public function Main():void
{
var f1:Flower = new Flower("rose");
var f2:Flower = new Flower("cactus");
var f3:Flower = new Flower("fff");
}
}
}
Flower.as:
package
{
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Flower
{
protected var name:String;
public function Flower(name:String):void
{
setName(name);
var updateTimer:Timer;
updateTimer = new Timer(500.6, 2);
updateTimer.addEventListener(TimerEvent.TIMER, TimerFun);
updateTimer.start();
}
public function TimerFun(e:TimerEvent):void
{
trace ("test " + getName());
setName("xxx");
}
public function setName(name:String):void
{
this.name = name;
}
public function getName():String
{
return name;
}
}
}
So basically from what I understand, every time I create an instance to the class "Flower.as" like this:
var f1:Flower = New Flower("rose"));
it saves each instance to a new line in the protected var name which is located at flower.as. then I can get the names with getnames. I placed a timer to check the values, and inside the timer I decided to try to assign a new name with the function setName("xxx"), and I set the timer to activate itself twice in order to see how the results change; However, all it did was replace all of the instances with the value "xxx", it didn't add anything(I expected a new string named "xxx" along with the others). I need an explanation about what
var f1 Flower:Flower = New Flower("rose");
does exactly and what setName("xxx") does. Thank you for your time.
Okey let me try to explain it for you :)
When u creat this : var f1:Flower; = new Flower("rose"); u creat a flower and give it name as "rose". But however when u creat a Flower type variable it trigger a TimerEvent. When TimerEvent start working first line will give to you
"test rose" on your output panel. But the second line is using setName("xxx") function for changing Flower's name.
So when u look your output panel u should see something like this :
test rose
test xxx
I hope this will enough to understand. If u have a question just ask :)
I am working on a C# class that imports text data from a web site. That works OK. Where I'm losing it is how to display the text in Unity 4.6 once I have all the text in a string variable. Any advice is appreciated.
Unity 4.6 UI system has component named Text. You can watch video tutorial here. Also I suggest you to check out its API.
As always, you have two options on how to add this component to the game object. You can do it from editor (just click on game object you want to have this component in hierarchy and add Text component). Or you can do it from script using gameObject.AddComponent<Text>().
In case you are not familiar with components yet, I suggest you to read this article.
Anyway, in your script you'll need to add using UnityEngine.UI; at the very top of it, because the Text class is in UnityEngine.UI namespace. Ok, so now back to script that will set the value of Text component.
First you need variable that refers to Text component. It can be done via exposing it to editor:
public class MyClass : MonoBehaviour {
public Text myText;
public void SetText(string text) {
myText.text = text;
}
}
And attaching gameObject with text component to this value in Editor.
Another option:
public class MyClass : MonoBehaviour {
public void SetText(string text) {
// you can try to get this component
var myText = gameObject.GetComponent<Text>();
// but it can be null, so you might want to add it
if (myText == null) {
myText = gameObject.AddComponent<Text>();
}
myText.text = text;
}
}
Previous script is not a good example, because GetComponent is actually expensive. So you might want to cache it’s reference:
public class MyClass : MonoBehaviour {
Text myText;
public void SetText(string text) {
if (myText == null) {
// looks like we need to get it or add
myText = gameObject.GetComponent<Text>();
// and again it can be null
if (myText == null) {
myText = gameObject.AddComponent<Text>();
}
}
// now we can set the value
myText.text = text;
}
}
BTW, the patter of ‘GetComponent or Add if it doesn’t exist yet’ is so common, that usually in Unity you want to define function
static public class MethodExtensionForMonoBehaviourTransform {
static public T GetOrAddComponent<T> (this Component child) where T: Component {
T result = child.GetComponent<T>();
if (result == null) {
result = child.gameObject.AddComponent<T>();
}
return result;
}
}
So you can use it as:
public class MyClass : MonoBehaviour {
Text myText;
public void SetText(string text) {
if (myText == null) {
// looks like we need to get it or add
myText = gameObject.GetOrAddComponent<Text>();
}
// now we can set the value
myText.text = text;
}
}
make sure you import the ui library - using UnityEngine.UI
gameObject.GetComponent<Text>().text - replace .text with any other field for UI Text
I assume that the issue is creating dynamic sized "textbox" rather than just assigning the string to a GUIText GameObject. (If not - just put a GUIText GameObject into your scene, access it via a GUIText variable in your script and use myGUIText.text = myString in Start or Update.)
If I am correct in my assumption, then I think you should just be using a GUI Label:
http://docs.unity3d.com/ScriptReference/GUI.Label.html
If you need to split the string up to place text into different labels or GUITexts, you will need to use substrings