是否有解决方案,以防止热重装后图像在抖动中消失?

时间:2019-07-28 13:10:14

标签: flutter dart

我正在创建一个新的flutter应用程序,我想要解决在flutter热重新加载后图像消失的问题。


import 'package:flutter/material.dart';


class Home extends  StatefulWidget {

  @override

  State <StatefulWidget> createState() {

    return HomeState();
  }
}

class HomeState extends State<Home> {

  @override

  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.redAccent,
        title: Text('Login Page '),
      ),
      body: Container(
        padding: EdgeInsets.all(33.0),
        alignment: Alignment.center,
        child: Column(
          children: <Widget>[
            Image.asset('img/userlogin.png'),
            Container(
              child: Column(
                children: <Widget>[
                  TextField(
                    controller: null,
                    decoration: InputDecoration(
                        icon: Icon(Icons.person), hintText: 'Yor Name'),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您可以使用preCacheImage方法内部的initState函数预先缓存图像 像这样:

class HomeState extends State<Home> {
  @override
  void initState() {
    precacheImage(new AssetImage('img/userlogin.png'));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // Widgets...
  }
}