function - self 更新的 Flutter 模型

标签 function flutter model

为了开发我的应用程序,我想使用我创建的模型,因为我必须在 3 个页面中显示这个小部件。

这个小部件有一个函数,当小部件被点击时调用,当按钮被点击时,文本和颜色应该改变。 这应该发生在列表 itemsuserID 时(这些项目是用户数据列表,列表的结构类似于 [{''userID' : 'keykeykey, 'userName': 'Ryan', ...}, {..}, {..}]) 等于登录用户的userID。

为了这个问题的目的,我创建了一个小部件(但原来的部件里面有更多的东西

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class SomeRandomCard extends StatelessWidget {
  SomeRandomCard({@required this.onPressed, this.text, this.color});

  final GestureTapCallback onPressed;
  String text;
  Color color;

  @override
  Widget build(BuildContext context) {
    return RawMaterialButton(
      child: Text(text,
      style: TextStyle(
        color: color
      ),),
      onPressed: onPressed,
    );
  }
}

然后我在 ListView.builder 中调用 SomeRandomCard 小部件:

userID = 'TestTestTestTestTestTestTest';
String text = 'a';
Color color = Colors.green;

  changeText(someID) {
    if (someID == userID) {
      setState(() {
        print(text);
        if (text == 'a') {
          text = 'b';
          color = Colors.green;
        } else {
          text = 'a';
          color = Colors.red;
        }
      });
    }
  }

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: style.coral,
      body: Container(
      child: ListView.builder
        (
          itemCount: items.length,
          itemBuilder: (BuildContext ctxt, int index) {
            return SomeRandomCard(onPressed: changeText(items[index]['userID']), text: text, color: color,);
          }
      )
      ),
      floatingActionButton: MyFloatingButton(),
    );
  }

但文本和颜色没有改变,函数也没有被调用。

我认为刷新问题是由 StatelessWidget 引起的,然后我编辑模型并编写了这个 StatefulWidget:

class SomeRandomCard extends StatefulWidget {
  SomeRandomCard({@required this.onPressed, this.text, this.color});

  final GestureTapCallback onPressed;
  String text;
  Color color;


  @override
  State<StatefulWidget> createState() => new _SomeRandomCardState();
}

class _SomeRandomCardState extends State<SomeRandomCard> {

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

  @override
  Widget build(BuildContext context) {
    return RawMaterialButton(
      child: Text(widget.text,
      style: TextStyle(
        color: widget.color
      ),),
      onPressed: widget.onPressed,
    );
  }
}

但是和之前一样,函数没有被调用。

我知道真正的问题是调用函数时的变量,因为如果我创建了一个不需要参数的新函数

SomeRandomCard(onPressed: changeText, text: text, color: color,);

函数被调用并改变屏幕上的值。

但是我要检查userId是否相同,我该怎么做呢?

最佳答案

这才是真正的问题

SomeRandomCard(onPressed: changeText(items[index]['userID']), text: text, color: color,);

SomeRandomCard(onPressed: changeText, text: text, color: color,);

第一个,你将 void 或什么都没有(null)传递给 onPressed 因为你调用了 changeText和 changeText 返回 void

第二个之所以有效,是因为您传递了一个实际的 function 而不是 nullvoid

根据您的要求。

the function is been called and change the value on the screen. But I have to check if the userId is the same, how can I do it?

你可以这样做:

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: style.coral,
      body: Container(
      child: ListView.builder
        (
          itemCount: items.length,
          itemBuilder: (BuildContext ctxt, int index) {
            return SomeRandomCard(onPressed: () { changeText(items[index]['userID']) }, text: text, color: color,);
          }
      )
      ),
      floatingActionButton: MyFloatingButton(),
    );
  }

顺便说一句,index的保存可能会造成混淆。我以前有过这种困惑,所以我提供了一个最小的例子,你可以在 DartPad 上试试看看它是如何工作的。

class TestCallback {
  final Function onPress;
  TestCallback(this.onPress);

  call() {
    onPress();
  }
}

main(){
  List<TestCallback> callbacks = new List<TestCallback>();
  for(int i = 0; i < 5; i++)
    callbacks.add(TestCallback((){ print(i); }));
  for(int i = 0; i < 5; i++)
    callbacks[i].call();
}

输出: 0 1 2 3 4

关于function - self 更新的 Flutter 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57704294/

相关文章:

function - 向两个用户发送确认电子邮件

php - use function 不导入 PHP 中的函数

android-studio - 自动完成功能在带有 Flutter 的 Android Studio 中无法正常工作 - 第一个建议无关紧要

flutter doctor - 系统找不到指定的路径[不重复]

dart - 如何根据先前下载的信息获取图像列表?

javascript - 如何用一个按钮调用两个函数

function - 如何在 D 中模拟标准库函数

ruby-on-rails - 从 Ruby on Rails 应用程序中的模型访问数据

python - Django - 如何正确建模?

mysql - Codeigniter db->get() 返回空记录集