【问题标题】:CupertinoTabView doesn't have an "OnPressed()" functionality. How to simulate?CupertinoTabView 没有“OnPressed()”功能。如何模拟?
【发布时间】:2020-06-10 16:30:53
【问题描述】:

好的,我的程序有问题,我明白为什么它不能按我想要的方式运行,但我不明白如何解决它。

我正在使用以下代码在我的应用程序中切换选项卡。

        child: CupertinoTabView(builder: (context) {
          switch (i) {
            case 0:
              titleChanged = false;
              firstPage = Page1();
              return firstPage;
              break;
            case 1:
              pageTitle = 'Page 2';
              titleChanged = true;
              secondPage = SecondPage();
              return secondPage;
              break;
            default:
              return Container();
          }
        }

现在在代码的前面(在小部件树的上层)我有整个页面的标题,我想在第 1 页和第 2 页之间来回切换。我可以更改文本,但我需要执行 setState() 以使更改生效。

不幸的是,tabview“按钮”不像普通按钮那样起作用,因此,我不能对它们应用 setState() 函数,否则它会在构建过程中调用 setState。

有人告诉我也许要使用观察者,但我在网上找到的任何资源都不能真正指导我确切地这样做。

我不确定观察者是否是正确的方法,但我的经验也即将结束,因为我对颤动还很陌生。有什么想法吗?

【问题讨论】:

    标签: ios flutter setstate tabview


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    您可以使用onTapCupertinoTabBar
    示例代码更新AppBartitle
    代码片段

     tabBar: CupertinoTabBar(
                      items: [
                        ...
                      ],
                      onTap: (int index) {
                        switch (index) {
                          case 0:
                            _title = "1 first";
                            setState(() {});
                            break;
                          case 1:
                            _title = "2 second";
                            setState(() {});
                            break;
                          case 2:
                            _title = "3 third";
                            setState(() {});
                            break;
                        }
                      },
    

    工作演示

    完整代码

    import 'dart:async';
    
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String _title = "1 first";
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text(_title)),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Expanded(
                  child: CupertinoTabScaffold(
                    tabBar: CupertinoTabBar(
                      items: [
                        BottomNavigationBarItem(
                            title: Text("First"), icon: Icon(Icons.menu)),
                        BottomNavigationBarItem(
                            title: Text("Second"), icon: Icon(Icons.business)),
                        BottomNavigationBarItem(
                            title: Text("Third"), icon: Icon(Icons.account_box)),
                      ],
                      onTap: (int index) {
                        switch (index) {
                          case 0:
                            _title = "1 first";
                            setState(() {});
                            break;
                          case 1:
                            _title = "2 second";
                            setState(() {});
                            break;
                          case 2:
                            _title = "3 third";
                            setState(() {});
                            break;
                        }
                      },
                    ),
                    tabBuilder: (BuildContext context, int index) {
                      return CupertinoTabView(
                        builder: (context) {
                          switch (index) {
                            case 0:
                              return FirstPage();
                              break;
                            case 1:
                              return SecondPage();
                              break;
                            case 2:
                              return ThirdPage();
                              break;
                          }
                        },
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    class FirstPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text("First"),
          ),
          child: Center(
            child: CupertinoButton(
              child: Text("First button"),
              onPressed: () {},
            ),
          ),
        );
      }
    }
    
    class SecondPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text("Second"),
          ),
          child: Center(
            child: CupertinoButton(
              child: Text("Second button"),
              onPressed: () {},
            ),
          ),
        );
      }
    }
    
    class ThirdPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text("Thrid"),
          ),
          child: Center(
            child: CupertinoButton(
              child: Text("Third button"),
              onPressed: () {},
            ),
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      如果选项卡有子属性,请尝试使用 GestureDetector()

      【讨论】:

        猜你喜欢
        • 2019-07-22
        • 1970-01-01
        • 2021-04-29
        • 2020-07-06
        • 1970-01-01
        • 2018-07-19
        • 1970-01-01
        • 1970-01-01
        • 2021-12-24
        相关资源
        最近更新 更多