Mobx不会更改屏幕状态

问题描述

我想使用mobx更改屏幕的选项卡,但它没有更改。认情况下,我使用的是bool follwing变量,如果为true,则显示绿色Container,否则显示红色Container,但是fool值正在更改,但状态未更改。如何解决这个问题?我是第一次(Mobx)使用观测器,但没有用

HomeScreenviewmodel model;   
  void initState() {
    super.initState();
    model = HomeScreenviewmodel();
    model.init();
  }

  @override
  Widget build(BuildContext context) {
    return Observer(
      builder: (context) {
        return Scaffold(
            body: PageView.builder(
                    physics: AlwaysScrollableScrollPhysics(),scrollDirection: Axis.vertical,itemBuilder: (context,position) {
                      return Container(
                        color: Colors.black,child: Stack(
                          children: <Widget>[
                            model.following
                                ? Container(
                                    height: 200,width: 200,color: Colors.green,)
                                : Container(
                                    height: 200,color: Colors.red,),Positioned(
                                top: 50,left: 50,right: 50,child: _tabrow(context)),//  search(),],);
                    },itemCount: 5));
      },);
  }
_tabrow(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[
          FlatButton(
              onpressed: () {
                model.following = false;
              },child: Container(
                padding: EdgeInsets.all(10.0),child: Text(
                  "For you",style: TextStyle(
                      fontSize: !model.following ? 25.0 : 17.0,color: Colors.white),)),FlatButton(
              onpressed: () {
                model.following = true;
              },child: Text(
                  "Following",style: TextStyle(
                      fontSize: model.following ? 25.0 : 17.0,);
  }

import 'package:mobx/mobx.dart';

part 'home-screen-view-model.g.dart';

class HomeScreenviewmodel = HomeScreenviewmodelImpl with _$HomeScreenviewmodel;

abstract class HomeScreenviewmodelImpl with Store {
  PostRepository _postRepository = PostRepository();

  List<PostData> forYouList;
  List<PostData> followingList;

  @observable
  bool loading = true;
  @observable
  bool following = true;

  void init() {
    getPostsForYou();
    getPostsFollowing();
  }

  @action
  Future<void> getPostsFollowing() async {
    followingList = await _postRepository.getPostsFollowing();
    loading = false;
  }

  @action
  Future<void> getPostsForYou() async {
    forYouList = await _postRepository.getPostsForYou();
    loading = false;
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)