【问题标题】:Setting a background in flutter from a list从列表中设置背景
【发布时间】:2020-10-17 01:10:31
【问题描述】:

我想要一个名为 Theme 的页面,带有网格图像,当我单击图像时,选择该图像作为主页的背景。

这是我的清单:

List<Theme2> themeitems = [
  Theme2(
    background: 'assets/images/theme/deskWhite.png',
    selected: false,
  ),
  Theme2(
    background: 'assets/images/theme/greenDrop.png',
    selected: false,
  ),
  Theme2(
    background: 'assets/images/theme/leaf.png',
    selected: false,
  ),
],

这是我要设置背景图片的主页:

child: Scaffold(
          body: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/theme/sand.png'),
         ////// image: AssetImage(themeitems[idx].background.selected), // I know this is not correct
                fit: BoxFit.cover,
              ),
            ),
            child: Column(.........

那么,如何从这些列表项之一设置背景图像?

我尝试使用themeitems[index].selected = !themeitems[index].selected;,但我不确定如何正确使用它。如果网上有这样的问题,请给我一个链接。

感谢您的宝贵时间。

【问题讨论】:

    标签: list flutter background setstate


    【解决方案1】:

    根据您的代码,我认为将图像重命名为更顺序的名称会更容易,例如:image1.pngimage2.pngimage3.png、...

    您可以将其存储在list 中,但我认为您不需要这样做,只要您已将图像添加到您的资产文件夹中即可。

    然后,在您的代码中选择图像:

    int index = 0; //this variable is to store your selected index
    
    child: Scaffold(
              body: Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/theme/image$index.png'), //So if the index = 0, so image).png will appear. And, so on.
                    fit: BoxFit.cover,
                  ),
                ),
                child: Column( //YOUR CODE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      相关资源
      最近更新 更多