自行更新的 Flutter 模型 (Flutter model that updates itself)


問題描述

自行更新的 Flutter 模型 (Flutter model that updates itself)

為了開發我的應用程序,我想使用我創建的模型,因為我必須在 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 小部件


參考解法

方法 1:

Here is the real problem betweeen

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

and

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

The first one, you pass a void or nothing(null) to onPressed because you called changeText, and changeText return void.

The second one works because you passed an actual function instead of null or void.

According to your requirement.

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?

You could do It as below:

@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(),
    );
  }

By the way, It might be confusion on the preservation of index. I had this confusion before, so I provide a minimal exmaple, you could try It on DartPad to see how It works.

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();
}

Output: 0 1 2 3 4

方法 2:

Here is the fix:

Don't do this:

onPressed: widget.onPressed

Do this:

onPressed: () => widget.onPressed()

Here is the complete code for you:

import 'package:flutter/material.dart';

main() {
  runApp(MyStateWidget());
}

class MyStateWidget extends StatefulWidget {
  @override
  _MyStateWidgetState createState() => _MyStateWidgetState();
}

class _MyStateWidgetState extends State<MyStateWidget> {
  var userID = 'TestTestTestTestTestTestTest';
  String text = 'a';
  Color color = Colors.green;

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
            child: ListView.builder(
                itemCount: 1,
                itemBuilder: (BuildContext ctxt, int index) {
                  return SomeRandomCard(
                    onPressed: () => changeText(items[index]['userID']),
                    text: text,
                    color: color,
                  );
                })),
        floatingActionButton: FloatingActionButton(
          onPressed: () {print("Hello");},
        ),
      ),
    );
  }
}

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

  final Function onPressed;
  String text;
  Color color;

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

方法 3:

I think you just change onPressed: changeText(items[index]['userID']) to onPressed: ()=>changeText(items[index]['userID'])will work.

(by alessandro buffoliTokenyetsagar surimafanwei)

參考文件

  1. Flutter model that updates itself (CC BY‑SA 2.5/3.0/4.0)

#Model #function #Flutter






相關問題

MVVM - 如何將 ViewModel 包裝在 ViewModel 中? (MVVM - How to wrap ViewModel in a ViewModel?)

Rails - 訪問 HABTM 中間人的關係數據 (Rails - Access the HABTM middle-man's relationship data)

ruby on rails 友誼以下節目總是我的名字 (ruby on rails friendship following shows always my name)

Мадэль, вызначаная ў выглядзе? (A model defined in a view?)

MVC3 + MongoDB 架構:將模型直接存儲到數據庫? (MVC3 + MongoDB Architecture: Store models directly to database?)

模型可以鏈接到數據庫中的視圖而不是 CakePHP 中的表嗎? (Can a model link to a View in the database instead of a table in CakePHP?)

我應該對 Rails 棄用警告感到害怕嗎? (Should I freak out about Rails deprecation warnings?)

嵌套集模型優化 (Nested set model optimization)

無法從“System.Web.Mvc.FormCollection”轉換為“MyProject.Models.EditViewModel” (Cannot convert from 'System.Web.Mvc.FormCollection' to 'MyProject.Models.EditViewModel')

Qt MVC - 用戶點擊時獲取文件名? (Qt MVC - Get filename when user clicks?)

自行更新的 Flutter 模型 (Flutter model that updates itself)

Modelchoicefield保存在數據庫django中 (Modelchoicefield save in database django)







留言討論