我试图制作一个简单的图像,当按下按钮时该图像会出现或消失。该按钮与图像位于不同的类中,因此在Flutter中,这会引起严重的问题。
我已经阅读了很多关于此的论坛,并且尝试了所有提出的解决方案,但是没有一个对我有用。
我正在尝试做的事情:
new Person()
这是我想在coinVisible上触发SetState()的单独类,
class SinglePlayerMode extends StatefulWidget {
@override
SinglePlayerModeParentState createState() => SinglePlayerModeParentState();
}
class SinglePlayerModeParentState extends State<SinglePlayerMode> {\
bool coinVisible = false;
toggleCoin() {
setState(() {
coinVisible = !coinVisible;
});
}
Widget topMenuRow() {
return Stack(
children: [
Column(
children: [
coinVisible == true ?
Padding(
padding: EdgeInsets.all(50),
child: Container(
height: 60,
width: 60,
color: Colors.blueGrey[0],
decoration: BoxDecoration(
color: Colors.blueAccent,
image: DecorationImage(
image: ExactAssetImage('lib/images/coin_head.jpg'),
fit: BoxFit.cover,
),
),
),
) : Container(
height: 60,
width: 60,
color: Colors.black,
),
],
),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
child: ListView(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
children: [
topMenuRow(),
SizedBox(height: 40),
],
),
),
);
}
但是我尝试过的所有东西都没有起作用,而且我已经失去了工作时间。
答案 0 :(得分:1)
很简单,您需要发送SinglePlayMode :: toggleCoin函数作为dropDownMenu类的回调。
class dropDownMenu extends StatefulWidget {
final _callback; // callback reference holder
//you will pass the callback here in constructor
dropDownMenu( {@required void toggleCoinCallback() } ) :
_callback = toggleCoinCallback;
@override
_dropDownMenuState createState() => _dropDownMenuState();
}
class _dropDownMenuState extends State<dropDownMenu> {
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget> [
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
child: Opacity(
opacity: 0.0,
child: FloatingActionButton(
heroTag: null,
onPressed: (){
widget?._callback(); // callback calling
},
),
),
);
}
}
然后,当您在SinglePlayerMode类中创建dropDownMenu类实例时,您将执行
dropDownMenu(
toggleCoinCallback: toogleCoin,
);