I'm new to Flutter. I have made a stateful widget that has multiple options for states, and I have no clue how to switch between them, or if it's even possible. Basically I have:
class SWidget extends StatefulWidget {
State<StatefulWidget> createState(){
return _State1();
}
}
class _State1 extends State<SWidget> {
...
}
class _State2 extends State<SWidget> {
...
}
I want SWidget to switch from _State1 to _State2
Sorry if I don't understand your question but state is often tied to UI. If you need new state then chances are you're wanting to display something different. In that case, you'd want to distinguish the two states (and their UI components) by placing them in their own stateful widgets. You can then switch between the two widgets in a stateful or stateless widget:
class SWidget extends StatelessWidget {
SWidget(this.condition);
final bool condition;
#override
Widget build(BuildContext context) {
return condition ? Foo() : Bar();
}
}
class Foo extends StatefulWidget {
#override
_FooState createState() => _FooState();
}
class _FooState extends State<Foo> {
var _foo = 'foo';
#override
Widget build(BuildContext context) {
return Text(_foo);
}
}
class Bar extends StatefulWidget {
#override
_BarState createState() => _BarState();
}
class _BarState extends State<Bar> {
var _bar = 'bar';
#override
Widget build(BuildContext context) {
return Text(_bar);
}
}
I don't think I've ever seen anyone actually switch the state object out from underneath a widget. You can though easily change a widget's state by calling setState and toggling between values that way too:
class SWidget extends StatefulWidget {
#override
_SWidgetState createState() => _SWidgetState();
}
class _SWidgetState extends State<SWidget> {
var _value = true;
#override
Widget build(BuildContext context) {
return Switch(
value: _value,
onChanged: (value) => setState(() => _value = value),
);
}
}
Related
So I have 2 classes:
class A extends StatefulWidget {
A({Key? key}) : super(key: key);
#override
State<A> createState() => _AState();
}
class _AState extends State<A> {
#override
Widget build(BuildContext context) {
return Container();
}
void func(){}
}
And I want to override the _AState classes func() method. So I do this like this:
class B extends A{
final item = 10;
#override
State<A> createState() => _BState();
}
class _BState extends _AState{
#override
void func() {
widget.item //can't do this
}
}
I have no problem overriding the func() method, but now I also need to access my new variable item, that is declared in B class. And I know I can't do that because instance widget is provided by State<A> class.
So my question is: How to access the variable item from B class in _BState?
Cast the widget to B object
class _BState extends _AState{
#override
void func() {
// (widget as B).item
}
}
Like the below code:
class TestWidget extends StatefulWidget {
String name;
TestWidget(this.name);
void updateName(String name) {
//how to update Text data
}
#override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<KMSelectionItem> {
#override
Widget build(BuildContext context) {
return Text(widget.name);
}
}
I want to change the text data through the method updateName. But I don't know how can do it.
you should use setState method for this purpose, this method updates the ui based on the changes made. insert the function code like this and declare the function inside your stateful widget. to update the text:
class _TestWidgetState extends State<KMSelectionItem> {
#override
Widget build(BuildContext context) {
return Text(widget.name);
}
void updateName() {
//how to update Text data
setState(() {
widget.name = YOUR_NEW_TEXT;
});
}
}
class TestWidget extends StatefulWidget {
String name;
TestWidget(this.name);
void updateName(String name) {
setState(() {
name ="updated_name";
});
}
#override
_TestWidgetState createState() => _TestWidgetState();
}
class _TestWidgetState extends State<KMSelectionItem> {
#override
Widget build(BuildContext context) {
return Text(widget.name);
}
}
I stumped into a problem where I need to use a StatefulWidget parameter in its state class when it's constructed, but I couldn't find a way to do it since using widget.[whatever variable name] in the state's class constructor returns an unexpected null value, and the initState function only runs when the widget is being drawn to the screen.
For example:
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
final String text;
Test(this.text);
final state = _TestState();
#override
_TestState createState() => state;
}
class _TestState extends State<Test> {
String? changingText;
void updateChangingText(String moreText){
changingText = changingText! + moreText;
}
#override
void initState() {
super.initState();
changingText = widget.text;
}
#override
Widget build(BuildContext context) {
return Text(changingText!);
}
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
var w = Test('test');
w.state.updateChangingText(' text');
return MaterialApp(home: Scaffold(body:
Test('test text')
));
}
}
void main() {
runApp(App());
}
This doesn't work since changingText is being updated before initState gives it its initial value since it only runs when Text is being drawn to the screen and this:
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
final String text;
Test(this.text);
final state = _TestState();
#override
_TestState createState() => state;
}
class _TestState extends State<Test> {
String? changingText;
void updateChangingText(String moreText){
changingText = changingText! + moreText;
}
_TestState(){
changingText = widget.text;
}
#override
Widget build(BuildContext context) {
return Text(changingText!);
}
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
var w = Test('test');
w.state.updateChangingText(' text');
return MaterialApp(home: Scaffold(body:
Test('test text')
));
}
}
void main() {
runApp(App());
}
doesn't work either since you can't use widget.[whatever] in state class constructors (for some reason).
So what can I do to use widget parameters in the state class before the widget is drawn to the screen?
Thanks in advance for the help
You should use the initState method present in the State for this instead of the constructor
#override
void initState() {
changingText = widget.text;
super.initState();
}
bool Globalveriable = false;
class Class1 extends StatefulWidget {
#override
_Class1State createState() => _Class1State();
}
class _Class1State extends State<Class1> {
#override
Widget build(BuildContext context) {
return DayNightSwitch(
value: Globalveriable,
onChanged: (changed) {
setState(() {
Globalveriable = changed;
});
},
);
}
}
class Class2 extends StatefulWidget {
#override
_Class2State createState() => _Class2State();
}
class _Class2State extends State<Class2> {
#override
Widget build(BuildContext context) {
return Text(Globalveriable.toString());
}
}
//when clicking on the switch, the value of Globalveriable will be true for Class1 but I want it should be the same for Class2 at the Same time
add the static keyword will resolve your issue as
static bool Globalveriable = false;
Once the StatefulWidget dispose, (item out of screen) how to retrieve the state of the StatefulWidget?
I'm actually set a animatedlist but I think it's the same probleme. May be update the list might solved the probleme. But how?
I just want the same state.
You'll want the State that needs to be preserved to use AutomaticKeepAliveClientMixin.
Here's an example:
class Foo extends StatefulWidget {
#override
FooState createState() {
super.build(context);
return new FooState();
}
}
class FooState extends State<Foo> with AutomaticKeepAliveClientMixin {
#override
Widget build(BuildContext context) {
return Container(
);
}
#override
bool get wantKeepAlive => true;
}
Such Foo widget will preserve its state even if it leaves the screen inside a ListView