为什么在第一次更改图像时此按钮图像会闪烁?

时间:2019-05-20 06:50:56

标签: flutter

我的Flutter应用程序中有一个按钮,该按钮具有我要在onTap,onTapCancel和onTapUp(当按下按钮或未按下按钮时)中更改的DecorationImage。一切工作都很好,除了非常快的闪光外,当第一次更改图像时,图像似乎消失了。之后,它可以无缝运行。

起初,我认为这是由于图像未加载到内存或其他原因,所以我尝试在initState中初始化图像变量,但没有执行任何操作。

class _MainPageState extends State<MainPage> {

static AssetImage buttonUnselected;
static AssetImage buttonSelected;

AssetImage button;

@override
void initState() {
  buttonUnselected = new AssetImage(‘assets/label.png’);
  buttonSelected = new AssetImage(‘assets/label_selected.png’);

  button = buttonUnselected;
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: UIWidgets.getAppBar(),
    body: new Stack(
        children: <Widget>[
          new Container(
            decoration: new BoxDecoration(
              image: new DecorationImage(
                image: new AssetImage(“assets/background_main.jpg”),
                fit: BoxFit.fitHeight,),
            ),
          ),
          new Container(
              margin: EdgeInsets.only(left: 15, right: 15),
              child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new GestureDetector(
                        onTapDown: (TapDownDetails details) => this.setState(() {
                          button = buttonSelected;
                        }),
                        onTapCancel: () => this.setState(() {
                          button = buttonUnselected;
                        }),
                        onTapUp: (TapUpDetails details) => this.setState(() {
                          button = buttonUnselected;
                        }),
                        child: Container(
                            margin: EdgeInsets.only(top: 10, bottom: 10),
                            child: Center(child: Text(“Button”)),
                            height: 100,
                            width: 400,
                            decoration: BoxDecoration(
                                image: DecorationImage(
                                    image: button
                                )
                            )
                        )
                    ),
                  ]
              )
          )
        ]
    ),
  );
}
}

1 个答案:

答案 0 :(得分:0)

可能是initState()中没有super()。对我来说很好。

 @override
  void initState() {
    super.initState();