【问题标题】:CupertinoTabView resets on Hot ReloadCupertinoTabView 在热重载时重置
【发布时间】:2021-09-07 17:09:22
【问题描述】:

我正在使用 CupertinoApp 模型开发应用程序。我正在为应用程序使用 CupertinoTabView 模型。现在在开发过程中,标签索引会在热重载时重置。我尝试在选项卡控制器上设置状态,但它仍然会重置。

代码:

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    CupertinoTabController _controller = CupertinoTabController();

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

    List<Widget> tabs = [
      HomeScreen(_controller),
      PlaySearchScreen(),
      HomeScreen(_controller),
      ProfileScreen(),
    ];
    return CupertinoTabScaffold(
        controller: _controller,
        tabBar: CupertinoTabBar(
          items: [
            BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.home),
              label: 'Home',
            ),
            BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.play),
              label: 'Play Tennis',
            ),
            BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.calendar),
              label: ' My Schedule',
            ),
            BottomNavigationBarItem(
              icon: Icon(CupertinoIcons.person),
              label: 'Profile',
            ),
          ],
        ),
        tabBuilder: (context, index) {
          return CupertinoTabView(
            builder: (ctx) {
              return GestureDetector(
                child: tabs[index],
                onTap: () => setState(
                  () => _controller.index = index,
                ),
              );
            },
          );
        });
  }
}

class HomeScreen extends StatefulWidget {
  HomeScreen(
    this.controller, {
    Key key,
  }) : super(key: key);

  final CupertinoTabController controller;

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

有没有办法让它不重置?

【问题讨论】:

  • 将控制器从构建方法变成类属性。热重载会重新运行构建方法,因此您的控制器会被重置

标签: flutter dart flutter-cupertino cupertinotabbar cupertino-widgets


【解决方案1】:

编辑

正如 @p2kr 指出的那样,您在 build 函数中弄混了(因为您还有要在其中定义的 dispose 回调在build 函数之外),您可以使用文档所述的CupertinoTabScaffold

您必须监听 onTap 回调并使用新的调用 setState currentIndex 反映新的选择。 这个也可以 用 CupertinoTabScaffold 自动包装它。

取自here。因此,如果您不使用CupertinoTabScaffold,我的原始答案适用。在您的情况下,您的课程应如下所示:

class _HomePageState extends State<HomePage> {
    // Placing the _controller as a property of your class
    // instead of a local variable inside your build method
    // which would be re-instantiated on every build call -
    // as in the case of hot reload
    CupertinoTabController _controller = CupertinoTabController();

    // Also setting the dispose function correctly outside
    // of the build function
    @override
    void dispose() {
        _controller.dispose();
        super.dispose();
    }

    @override
    Widget build(BuildContext context) {
        List<Widget> tabs = [
            HomeScreen(_controller),
            PlaySearchScreen(),
            HomeScreen(_controller),
            ProfileScreen(),
        ];

        ...

原创

CupertinoTabBar 有一个currentIndex 属性来维护一个选定的选项卡。因此,您需要向您的 _HomePageState 添加一个附加属性:

class _HomePageState extends State<HomePage> {
    // Variable to maintain current tab index.
    // Setting it to 0 (first tab) as default here
    int _index = 0;

    ...
}

然后在实际的CupertinoTabBar 中添加设置currentIndex 属性并使用onTap 回调相应地更新我们的属性:

CupertinoTabBar(
    // Setting the index
    currentIndex: _index,
    // Callback to update our index once a tab is clicked
    onTap: (index) => setState(() => _index = index),
    items: [
        BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.home),
            label: 'Home',
        ),
        BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.play),
            label: 'Play Tennis',
        ),
        BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.calendar),
            label: ' My Schedule',
        ),
        BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.person),
            label: 'Profile',
        ),
    ],
),

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 2016-10-10
    • 1970-01-01
    • 2018-08-26
    • 2018-11-19
    • 2020-08-08
    • 2017-01-20
    • 2019-07-01
    相关资源
    最近更新 更多