在文本小部件中使用变量 [英] Use variable in Text Widget

查看:78
本文介绍了在文本小部件中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Text Widget中使用一个变量,它表示无效的const值,因此,我需要使用const,但是我正在以动态方式使用Text Widget。有没有办法使用带有变量的文本?还是有另一个我可以使用的小部件?


我有这样的东西:

  class PlaceCardStateextends StatelessWidget {
PlaceCardState(this._placeCard);
Place _placeCard;

@override
小部件构建(BuildContext上下文){
return ListTile(
开头:const Icon(Icons.album),
title:Text( _placeCard.title),
字幕:const Text('今晚来跳舞!'),
);
}
}

place.dart

  class Place {
Place([this.title ='',this.description ='',this.image ='',this.value = 0.0]);
字符串标题;
字符串描述;
字符串图像;
双重价值;
}

我遇到了这个问题:


解决方案

更改此内容:

  const ListTile(
开头:const Icon(Icons.album),
标题:Text(_placeCard.title),
副标题:const Text('Come今晚跳舞!'),
);

为此:

  const ListTile(
开头:const Icon(Icons.album),
标题:const Text(_placeCard.title),
字幕:const Text('Come and dance今晚!'),
);

由于您的屏幕截图中的 ListTile 是常量那么所有属性也需要保持不变,因此在 Text(_placeCard.title), const >

I'm trying to use a variable in Text Widget, it says Invalid const value, so, I need to use a const, but I'm using Text Widget in a dynamic way. Is there a way to use a Text with variables? or is there another Widget that I could use?

I have something like this:

class PlaceCardState extends StatelessWidget {
  PlaceCardState(this._placeCard);
  Place _placeCard;

  @override
  Widget build(BuildContext context) {
    return ListTile(
            leading: const Icon(Icons.album),
            title: Text(_placeCard.title),
            subtitle: const Text('Come and dance tonight!'),
          );
  }
}

place.dart

class Place {
  Place([this.title = '', this.description = '', this.image='',     this.value=0.0]);
  String title;
  String description;
  String image;
  double value;
}

I get this issue:

解决方案

Change this:

const ListTile(
        leading: const Icon(Icons.album),
        title: Text(_placeCard.title),
        subtitle: const Text('Come and dance tonight!'),
      );

into this:

const ListTile(
        leading: const Icon(Icons.album),
        title: const Text(_placeCard.title),
        subtitle: const Text('Come and dance tonight!'),
      );

Since in your screenshot ListTile is a constant then all the properties need to be constant also, therefore add const before Text(_placeCard.title),

这篇关于在文本小部件中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆