Expanded(
child: RaisedButton(
splashColor: Colors.red,
elevation: 1.0,
onPressed: (){
_buttonTag = ButtonPressed(2);
setState(() {
_visible = false;
});
Navigator.of(context).push(_ProfileToStory());
print('pressed2');},
color: Colors.white,
child: Icon(
Icons.favorite
),
),
)
I am new to Flutter.
We used to have an outlet for button globally so that we can access that button from anywhere in the class. How we can do that in Flutter?
You dont need to access button. Instead access the variable inside the Raised button by declaring it yourself. If your Raised button is in a StatefulWidget class. You can simply change the state of the function with setState().
And instead of using row string, u can declear a variable in place of title or color and change it with setState() function from another function.
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
String btnTitle = "Some Text";
void myFunction(){
setState(() {
btnTitle = "Changed Title";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[300],
appBar: AppBar(
title: Text("Experiment"),
),
body: Column(
children: <Widget>[
RaisedButton(
child: Text(btnTitle),
onPressed: (){
},
),
RaisedButton(
child: Text("Change Title"),
onPressed: (){
myFunction();
},
)
],
),
);
}
}
Here, I am changing the title of one RaisedButton from another.
This is how I figured out by taking a bool _button1Pressed and changing its value in setSate method. Is this right approach?
class Profile extends StatefulWidget {
@override
_ProfileState createState() => _ProfileState();
}
class _ProfileState extends State<Profile> {
bool _visible = true;
bool _button1Pressed = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Madelyn Johnson'),
centerTitle: true,
backgroundColor: Colors.grey,
),
Row(
children: <Widget>[
Expanded(
child: RaisedButton(
elevation: 1.0,
color: _button1Pressed ? Colors.pinkAccent : Colors.white,
onPressed: () {
setState(() {
_visible = true;
_button1Pressed = true;
});
print('pressed1');
},
child: Icon(Icons.dashboard),
),
),
Expanded(
child: RaisedButton(
splashColor: Colors.red,
elevation: 1.0,
onPressed: () {
setState(() {
_visible = false;
_button1Pressed = false;
});
Navigator.of(context).push(_ProfileToStory());
print('pressed2');
},
color: _button1Pressed ? Colors.white : Colors.pinkAccent,
child: Icon(Icons.favorite),
),
)
],
),
],
),
),
);}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.