Mobx不会更改屏幕状态

时间:2020-10-10 07:34:28

标签: flutter dart 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,
                                    width: 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: Container(
                padding: EdgeInsets.all(10.0),
                child: Text(
                  "Following",
                  style: TextStyle(
                      fontSize: model.following ? 25.0 : 17.0,
                      color: Colors.white),
                ),
              )),
        ],
      ),
    );
  }

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

0 个答案:

没有答案