【问题标题】:How to set conditional statement for image in Flutter?如何在 Flutter 中为图像设置条件语句?
【发布时间】:2018-10-01 06:02:12
【问题描述】:

我有条件语句从 Flutter 资产中设置图像,但在 Scaffold 主体中不起作用。

如何在 Flutter 中为图片设置条件语句?

String _backgroundImage;

void _setImage() {
  String _mTitle = "${widget.title.data}";

  if(_mTitle == “Goodmorrning”) {
    _backgroundImage = "assets/mobil_hello/goodmorrning.jpg";
  } else if(_mTitle == “Good day”) {
    _backgroundImage = "assets/mobil_hello/goodday.jpg";
  } 

  print("_mTitle: $_mTitle");  // works
  print("_backgroundImage: $_backgroundImage"); // works
}


Widget build(BuildContext contest) {

  return Scaffold(
    body: new Container(
        decoration: BoxDecoration(
            color: widget.backgroundColor,
                image: new DecorationImage(
                        fit: BoxFit.cover,
                        image: new AssetImage("$_backgroundImage") // not working
                        ),
        ),
    ),
  );
}

【问题讨论】:

  • 你应该在 setImage 中调用 setState
  • 如果您使用的是statefullwidget,那么。 if(_mTitle == "Goodmorrning") { setState((){ _backgroundImage = "assets/mobil_hello/goodmorrning.jpg"; }); } 同样适用于elseif

标签: image dart conditional flutter assets


【解决方案1】:

在这里,你创建了 void _setImage() 方法,它什么都不返回,而且你不能像这样使用它 new AssetImage(_setImage()) ,所以你必须像 String _setImage( ) ,返回String(_backgroundImage),所以可以直接在new AssetImage(_setImage())中调用这个方法。

将您的代码替换为以下代码:

import 'package:flutter/material.dart';
    String _backgroundImage;
    String _setImage() {
      String _mTitle = "${widget.title.data}";

      if(_mTitle == "Goodmorrning") {
        _backgroundImage = "assets/mobil_hello/goodmorrning.jpg";
      } else if(_mTitle == "Good day") {
        _backgroundImage = "assets/mobil_hello/goodday.jpg";
      }
      print("_mTitle: $_mTitle");  
      print("_backgroundImage: $_backgroundImage");
      return _backgroundImage; // here it returns your _backgroundImage value

    }


    Widget build(BuildContext contest) {

      return Scaffold(
        body: new Container(
          decoration: BoxDecoration(
            color: widget.backgroundColor,
            image: new DecorationImage(
                fit: BoxFit.cover,
                image: new AssetImage(_setImage()) //call your method here
            ),
          ),
        ),
      );
    }

【讨论】:

    【解决方案2】:

    你可以这样做:

    String _setImage() {
      String _mTitle = "${widget.title.data}";
    
      if(_mTitle == “Goodmorrning”) {
        return "assets/mobil_hello/goodmorrning.jpg";
      } else if(_mTitle == “Good day”) {
        return "assets/mobil_hello/goodday.jpg";
      } 
    
      print("_mTitle: $_mTitle");  // works
      print("_backgroundImage: $_backgroundImage"); // works
    }
    
    
    Widget build(BuildContext contest) {
    
      return Scaffold(
        body: new Container(
            decoration: BoxDecoration(
                color: widget.backgroundColor,
                    image: new DecorationImage(
                            fit: BoxFit.cover,
                            image: new AssetImage(_setImage()) // not working
                            ),
            ),
        ),
      );
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 2018-12-03
      • 2019-06-02
      • 2021-03-02
      • 2011-10-15
      • 2021-01-01
      • 1970-01-01
      • 2012-05-22
      相关资源
      最近更新 更多