Flutter有状态小部件状态中断

时间:2020-02-23 18:47:34

标签: flutter

Flutter的新手,我遇到一些问题,试图让状态变量“喜欢”在if条件下工作。

每当我在likedPressed()方法中添加“ if”条件时,它都会导致图标停止更改颜色,并且打印日志仅显示一个正在注册的条件。

即使我在if/else条件下注释掉API调用,它仍然永远不会到达remove调用...

TLDR :我从没见过Removing a like from ad被解雇。到底是怎么回事?:

I / flutter(16128):“喜欢的”按钮说:true

print(“向广告添加喜欢的图片”);

import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart';
import 'package:testApp_app_androidx/api_helper_functions/testApp_api_helper.dart';
import 'package:testApp_app_androidx/resources/assets.dart';
import 'package:testApp_app_androidx/widgets/home/reusable_widgets/interactable_widget.dart';
import 'package:testApp_app_androidx/widgets/home/reusable_widgets/liked_button.dart';
import 'package:flutter/cupertino.dart';

//-------------------------------------------------------
//Liked button was pressed class
//-------------------------------------------------------
class testAppLikeVideoAction extends StatefulWidget{
  @override
  PostState createState() => new PostState();
}
class PostState extends State<testAppLikeVideoAction>{

  bool liked = false;
  testAppAPIHelper testApp_api_helper = new testAppAPIHelper(false);

  void likedPressed(){
    HapticFeedback.vibrate();
    setState(() {
      this.liked = !this.liked;
      print("And the liked button says: ${this.liked}");
    });
    //---------------------------------------------------------------------------
    //Like Button Actions that are breaking the state
    //---------------------------------------------------------------------------
    if(this.liked = true) {
      print("Adding a like to ad");
      testApp_api_helper.addtestAppAdLikeRequest("like");  //api async http get request
    }
    else{ ///this condition is never met!!!!!!!!!!!!!!!!
      print("Removing a like from ad");
      testApp_api_helper.removetestAppAdLikeRequest("remove_like"); //api async http get request
    }
    //---------------------------------------------------------------------------
  }

  @override
  Widget build(BuildContext context) {
    return
      GestureDetector(
        onTap: () => likedPressed(),
            child: Container(
                child: Column(children: <Widget>[
                  new IconPictureWidget( Icon(liked ? Icons.favorite : Icons.favorite_border,
                    color: liked ? Colors.red : Colors.white, size: 50,) ),
                  Text(
                    "500k",
                    style: TextStyle(fontSize: 10, color: Colors.white),
                  ),
                ],)
            ),
          );
  }
}










//---------------------------------------------------------------------------
//Picture icon
//---------------------------------------------------------------------------

class IconPictureWidget extends StatelessWidget {
  Icon incoming_icon;
  IconPictureWidget(Icon incoming_icon){
    this.incoming_icon = incoming_icon;

  }
  @override
  Widget build(BuildContext context) {
    return new DecoratedBox(
      //child: new Center(widthFactor: 2.0, child: new Text("my image")),
      child: this.incoming_icon,
      position: DecorationPosition.background,
      decoration: new BoxDecoration(
        // shape: BoxShape.circle, //for visual diagnosis
        // color: Colors.transparent, //for visual diagnosis
        // image:  new DecorationImage(image: new AssetImage('assets/avatar.png')), //haven't used yet
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

if(this.liked = true) {
      print("Adding a like to ad");
      testApp_api_helper.addtestAppAdLikeRequest("like");  //api async http get request
    }

您正在将true分配给liked,因此它将始终为true。 更改为:

if(liked) {
      print("Adding a like to ad");
      testApp_api_helper.addtestAppAdLikeRequest("like");  //api async http get request
    } else {
  ...
}