颤动 - 切换选项卡时保存交互或状态

时间:2018-06-11 16:14:09

标签: dart flutter

我正在学习扑克,目前正在测试应用程序开发的不同方面。我最终的计划是有两个选项卡,每个选项卡从API中提取一个列表并在列表视图中显示它。我担心的是,每次切换选项卡时,选项卡都会全新重新绘制。如何保存选项卡和所有孩子的当前状态,以便在我再次访问选项卡时不会重置?我在想我错了吗?我是否应该手动将所有内容保存到变量中,并在每次绘制选项卡时重新填充它?

以下是我一直在测试的一个简单示例。这是一个简单的双选项卡应用程序,每页都有表单字段。如果我运行应用程序,然后在表单字段中键入内容,当我切换到选项卡二和后面时,表单字段的内容已被删除。

import 'package:flutter/material.dart';

void main() {
  print("start");
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Tab Test',
      home: new MyTabbedPage(),
    );
  }
}

class MyTabbedPage extends StatefulWidget {
  const MyTabbedPage({Key key}) : super(key: key);
  @override
  _MyTabbedPageState createState() => new _MyTabbedPageState();
}

class _MyTabbedPageState extends State<MyTabbedPage>
    with SingleTickerProviderStateMixin {
  final List<Tab> myTabs = <Tab>[
    new Tab(text: 'LEFT'),
    new Tab(text: 'RIGHT'),
  ];

  TabController _tabController;
  var index = 0;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(
        vsync: this, length: myTabs.length, initialIndex: index);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        bottom: new TabBar(
          controller: _tabController,
          tabs: myTabs,
        ),
      ),
      body: new TabBarView(
        controller: _tabController,
        children: myTabs.map((Tab tab) {
          return new Center(
              child: new TextFormField(
            decoration: new InputDecoration(labelText: 'Type Something:'),
          ));
        }).toList(),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

所以,我想我在setState中找到了答案。我在上面的示例中重新设计了一个按钮,该按钮分别增加每个选项卡上的计数器。使用setState更新投票计数允许我保存字段的状态并在我更改选项卡时保留它。我在下面添加我的示例代码以供参考。

import 'package:flutter/material.dart';

void main() {
  print("start");
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Tab Test',
      home: new MyTabbedPage(),
    );
  }
}

class MyTabbedPage extends StatefulWidget {
  const MyTabbedPage({Key key}) : super(key: key);
  @override
  _MyTabbedPageState createState() => new _MyTabbedPageState();
}

class _MyTabbedPageState extends State<MyTabbedPage>
    with SingleTickerProviderStateMixin {
  final List<Tab> myTabs = <Tab>[
    new Tab(text: 'LEFT'),
    new Tab(text: 'RIGHT'),
  ];

  TabController _tabController;
  var index = 0;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(
        vsync: this, length: myTabs.length, initialIndex: index);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  int votesA = 0;
  int votesB = 0;
  void voteUpA() {
    setState(() => votesA++);
  }
  void voteUpB() {
    setState(() => votesB++);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        bottom: new TabBar(
          controller: _tabController,
          tabs: myTabs,
        ),
      ),
      body: new TabBarView(
        controller: _tabController,
        children: <Widget>[
          new Column(
            children: <Widget>[
              Text("Hello Flutter - $votesA"),
              new RaisedButton(onPressed: voteUpA, child: new Text("click here"))
            ],
          ),
          new Column(
            children: <Widget>[
              Text("Hello Flutter - $votesB"),
              new RaisedButton(onPressed: voteUpB, child: new Text("click here"))
            ],
          ),
        ],
      ),
    );
  }
}
相关问题