【问题标题】:TabBar setting setting state of tabviewsTabBar设置tabviews的设置状态
【发布时间】:2018-11-06 07:17:56
【问题描述】:

我有一个带有 5 个标签的标签栏的主页。在主页中,我从 Internet 加载 JSON 并在每个选项卡中设置它的不同部分。这是一个世界杯应用程序,显示每场比赛的标签(小组、16 强、四分之一决赛、半决赛和决赛)。加载每个选项卡后,我会获取 json 并构建一个列表视图。

另外,我想设置一个按钮来重新加载信息(类似于工厂或应用栏中的操作)。但是,当我重新加载 JSON 时,如何设置实际选项卡的状态?

这是我的主页小部件构建:

Widget build(BuildContext context) {
return new Scaffold(
  appBar: new AppBar(
    leading: new IconButton(
      icon: new Icon(Icons.arrow_back),
      onPressed: () => Navigator.pop(context),
    ),
    title: new Text(title),
  ),
  body: new Center(
    child: new TabBarView(
      controller: _tabController,
      children: <Widget>[
        new GroupStageMatches(),
        new RoundOfSixteen(),
        new QuarterFinals(),
        new SemiFinal(),
        new Final(),
      ],
    ),
  ),
  bottomNavigationBar: new Material(
    color: Colors.blueAccent,
    child: new TabBar(
      controller: _tabController,
      tabs: <Widget>[
        new Tab(text: "Grupos"),
        new Tab(text: "Oitavas"),
        new Tab(text: "Quartas"),
        new Tab(text: "Semi"),
        new Tab(text:"Final"),
      ],
    ),
  ),
  floatingActionButton: new FloatingActionButton(
    onPressed: () {
      setState(() {
        sing.updateData();
      });
    }
  ),
);}

【问题讨论】:

    标签: flutter tabbar tabview


    【解决方案1】:

    最简单的解决方案是在你的单例中创建 valueNotifier

      ValueNotifier<String> valueNotifier = new ValueNotifier<String>("");
    

    然后,在initState 的每个选项卡中执行

    sing.valueNotifier.addListener(_valueChanged)
    

    dispose 上清理听众。

    要通知侦听器,您需要更改值。如果该值与前一个不同,它将通知侦听器。这将调用_valueChanged 方法

    sing.valueNotifier.value = "HelloWorld";
    

    一旦_valueChanged 被调用,您就可以在标签中设置状态。

    编辑:基于代码示例

    你看,值通知器保存你在类中需要的值。所以一旦 getJSON 解析,监听器就会在你的视图中被调用。

    class Singleton {
      static final Singleton _singleton = new Singleton._internal();
      ValueNotifier<Map<String, dynamic>> groupsJson =
          new ValueNotifier<Map<String, dynamic>>(null);
      ValueNotifier<Map<String, dynamic>> eliminationJson =
          new ValueNotifier<Map<String, dynamic>>(null);
    
      factory Singleton() {
        return _singleton;
      }
    
      Singleton._internal() {
        // updateData();
      }
    
    
      updateGroupsJson() async {
        _groupsJson.value = await this.getJson(
            "http://www.srgoool.com.br/call?ajax=get_classificacao2&id_fase=1796");
      }
    
      updateEliminationJson() async {
        _eliminationJson.value = await this.getJson(
            "http://www.srgoool.com.br/call?ajax=get_chaves&id_ano_campeonato=434");
      }
    
      Future<Map<String, dynamic>> getJson(url) async {
        print("loading");
        final response = await http.get(url);
    
        if (response.statusCode == 200) {
          print("loaded");
          var teste = json.decode(response.body);
          return teste;
        } else {
          print("erro");
          return null;
        }
      }
    }
    

    在你看来:

    void initState() {
      super.initState();
      sing = new Singleton();
      sing.groupsJson.addListener(onGroupJson);
      sing.updateGroupsJson();
    
      //you can already draw your frontend with latest groupsJson. Thiss will just update the view when you get a new one
    }
    
    void onGroupJson{
      setState ((){
        // assing fields that you need for drawing the view 
      });
    }
    

    【讨论】:

    • 但我想设置当前选项卡的状态。在选项卡中,我有一个名为 isLoading 的布尔变量,当它为 true 时,它​​显示 CricleProgress,当加载数据时,我想将其设置为 false。
    • 顺便说一句,我的方法 updateData() 不返回任何内容,它只是更新我的 Singleton 中的数据(sing 是它的实例)。
    • 它应该沿着树传播并改变需要重绘的状态。如果您想让它更加解耦,请查看此演示文稿。 youtube.com/watch?v=RS36gBEp8OI
    • 好的,我以为它在调用某个异步服务器。一定要检查我链接的演示文稿,它应该会阐明一些很酷的想法,在这个小答案中写得很大。
    • 为什么是 3 个 ValueNotifier?
    猜你喜欢
    • 2021-10-17
    • 2017-01-07
    • 2020-11-24
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2018-06-13
    • 2022-01-21
    相关资源
    最近更新 更多