Accessing setState from a Stateful Widget

1,638 views
Skip to first unread message

K Wilde

unread,
Jul 29, 2018, 6:03:14 PM7/29/18
to Flutter Dev
I have a StatefulWidget along with it's associated state.  I'd like to be able to change it's value and call setSate for the widget's state.  However, from the StatefulWidget definition I can't figure out how to call setState.   This custom widget is intended to be nested in other  widgets like a ListView.  Any help would be appreciated.

import 'package:flutter/material.dart';
import 'types.dart' as userTypes;

final _rowHeight = 50.0;
final _borderRadius = BorderRadius.horizontal(left:Radius.zero,right:Radius.zero );
final _fontSize = 20.0;


class DataItem extends StatefulWidget {
final Color color;
final userTypes.Tag tag;

DataItem({
Key, key,
@required this.tag,
@required this.color,
}): assert(tag != null),
assert(color != null),
super(key:key);

@override
DataItemState createState() => new DataItemState(tag,color);



}

class DataItemState extends State<DataItem>{
userTypes.Tag tag;
final Color color;

DataItemState(this.tag,this.color);

// set tag(userTypes.Tag newValue){
// setState(() {
// tag = newValue;
// print ("setState tag");
// });
// }

@override
Widget build(BuildContext context){
return new Container(
height: _rowHeight,
child: InkWell(
borderRadius: _borderRadius,
highlightColor: color,
splashColor: color,
onTap: () {
print("I was tapped " + tag.tagPath);
},
child: Padding(
padding: EdgeInsets.all(4.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Text(
this.tag.label,
style: new TextStyle(fontSize: _fontSize),
),
),
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: Text(
tag.value.toStringAsFixed(2) + " "+ tag.units,

style: new TextStyle(fontSize: _fontSize),
)
),
),
]
)

),
)
);
}

}

Jonah Williams

unread,
Jul 29, 2018, 6:24:06 PM7/29/18
to K Wilde, Flutter Dev
Hello!


Generally if you want a child widget to be able to trigger setState in a parent, you would have your widget accept a closure and call that instead.  Then the parent widget can decided to setState when appropriate.


Just looking over your code though I am noticing a critical issue - you should not pass arguments to your State object.



@override
DataItemState createState() => new DataItemState(tag, color);


Instead access them through `widget.tag` and `widget.color`, otherwise updating your widget won't work as expected.


InkWell(
  borderRadius: _borderRadius,
  highlightColor: widget.color,
  splashColor: widget.color,
  onTap: () { },
)


--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

K Wilde

unread,
Jul 30, 2018, 10:11:41 AM7/30/18
to Flutter Dev
Thanks for your reply that was very helpful.  I'm still trying to get my head around Flutter as I'm new to mobile programming as well as Dart.

Out of curiosity, why can't you pass arguments to the State object.  I did that as an experiment (never saw it done anywhere else).  I figured since it 'let' me do it, it was OK if not a little weird.

Kurt
Reply all
Reply to author
Forward
0 new messages