微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mobx不会更改屏幕状态

如何解决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 举报,一经查实,本站将立刻删除。