Reputation: 21
I have created a RaisedButton.Now I want to know can I pass key to it and access its object in a function or is there any other way to access the Raisebutton other then storing it in a variable.
Upvotes: 1
Views: 3495
Reputation: 15789
Flutter is different than traditional Android Development. Here Everything is Widget and Widget has its own state.
According to Flutter documentation,
If you want to disable a button you just have to pass null to onPressed of the RaisedButton. And if you want to enable it pass a function, you can pass an empty function also like (){} this.
Check the below Example to understand it.
void main() {
runApp(new ButtonDemoNew());
}
class ButtonDemoNew extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new _ButtonStateDemo();
}
}
class _ButtonStateDemo extends State<ButtonDemoNew> {
bool enabled = false;
@override
Widget build(BuildContext context) {
// TODO: implement build
return new MaterialApp(
title: "Button Demo",
home: new Scaffold(
appBar: new AppBar(
title: new Text("Button Demo"),
),
body: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new RaisedButton(
onPressed: enabled ? () {} : null,
child: new Text("Demo Button"),
),
new RaisedButton(
onPressed: () {
setState(() {
enabled = enabled ? false : true;
});
},
child: new Text("Enable / Disable"),
)
],
),
),
);
}
}
Upvotes: 1
Reputation: 40433
Clarification on what is actually wanting to be performed would be very useful.
However, I think I can guess what the issue is - confusion about how flutter's statefulness works.
The general way that things like RaisedButton work in Flutter is that you hold the state
in a StatefulWidget
somewhere above it, which does the building of the RaisedButton. If you want to change the raisedButton, you set a variable on the StatefulWidget within a setState(() => ?? = ??);
and use it in the StatefulWidget's build function to decide which parameters to build the RaisedButton with.
Here's a short example that raises the button each time you press it.
class Raising extends StatefulWidget {
final String text;
Raising({@required this.text});
@override
State<StatefulWidget> createState() => RaisingState();
}
class RaisingState extends State<Raising> {
double elevation = 0.0;
@override
Widget build(BuildContext context) => new RaisedButton(
child: new Text(widget.text),
elevation: elevation,
onPressed: () => setState(() => elevation += 1),
);
}
Upvotes: 0