【问题标题】:Flutter tab losing focus or not responding to tapFlutter 选项卡失去焦点或没有响应点击
【发布时间】:2019-04-01 09:25:41
【问题描述】:

我有一个flutter 应用程序,其中主屏幕是tabBar,如下所示:

class MainRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          body: TabBarView(
            physics: NeverScrollableScrollPhysics(),
            children: [
              Container(
                color: Color.fromRGBO(40, 40, 40, 1.0),
                child: Tab1(),
              ),
              Container(
                color: Color.fromRGBO(40, 40, 40, 1.0),
                child: Tab2(),
              ),
              Container(
                color: Color.fromRGBO(40, 40, 40, 1.0),
                child: Tab3(),
              ),
            ],
          ),
          bottomNavigationBar: TabBar(
            labelStyle: null,
            tabs: [
              Tab(
                icon: Icon(MdiIcons.home),
              ),
              Tab(
                icon: Icon(MdiIcons.settings),
              ),
              Tab(
                icon: Icon(MdiIcons.account),
              )
            ],
            indicatorColor: Colors.transparent,
            labelColor: Color.fromRGBO(255, 255, 255, 1),
            unselectedLabelColor: Color.fromRGBO(255, 255, 255, .3),

          ),
          backgroundColor: Colors.black,
        ),
      ),
    );

例如,我的 Tab3() 看起来像这样:

class Tab3 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => Tab3State();
}

class Tab3State extends State<Tab3> {

  @override
  Widget build(BuildContext context) {
    // TODO: Implement build
    return Scaffold(
      body: Center(
        child: Container(
          padding: EdgeInsets.only(top: 50),
          width: MediaQuery
              .of(context)
              .size
              .width,
          height: MediaQuery
              .of(context)
              .size
              .height,
          color: Colors.white,
          child: Column(
            children: <Widget>[
              Container(
                padding: EdgeInsets.all(10),
                child: RaisedButton(
                  padding: const EdgeInsets.all(20.0),
                  textColor: Colors.white,
                  onPressed: () {
                    print("GAllery tapped");
                  },
                  color: Colors.blue,
                  elevation: 10,
                  child: Text("Gallery"),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

应用程序在模拟器中运行正常,但是当我在真正的 Adnroid 设备上运行时,点击时出现问题。

当显示MainRoute 时,如果我尝试通过点击图标导航到最后一个选项卡,我第一次点击它时无法识别(失焦???),从现在开始按预期工作,仅在第一次点击时与所有选项卡相同。

现在,当 Tab3 加载时,图库按钮无法识别第一次点击(也失焦???),但随后识别第二次点击,并且从现在开始工作。

是不是代码有问题?代码架构有问题?我的真实设备(Samsung Galaxy S7)有问题?

【问题讨论】:

    标签: flutter navigationbar


    【解决方案1】:

    我不知道它是否相关,但你不应该有 2 个 Scaffold 小部件。 尝试删除 Tab3State 类中的那个,反正你不需要它。

    【讨论】:

    • 不,它没有帮助移除脚手架,但是谢谢!
    • 我粘贴了您的代码(减去额外的脚手架)并在摩托罗拉 g5 plus 上运行它,似乎工作正常。我会尝试用一些愚蠢的东西(也许只是一个容器)而不是你的 Tab3() 来尝试缩小问题的来源..
    • 真的吗?在启动后第一次点击 tab3 上的底部栏和凸起的按钮点击有效?,这是非常好的信息......也许它与我的设备有关......
    • 它可能与您的其他选项卡有关。尝试用简单的容器替换它们。即,而不是:Container( color: Color.fromRGBO(40, 40, 40, 1.0), child: Tab1(), ),,使用一个孩子:Container(color: Colors.white, child: Center(child: Text('tab1'),) 或其他东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    相关资源
    最近更新 更多