TabBar的索引在隐藏时会更改

时间:2019-10-23 14:19:55

标签: flutter view tabbar

我正在构建一个带有TabBar作为BottomNavigationBar和一个TabView作为主体的支架。问题是当我出于某种原因打开键盘以某种形式在表格中输入数据时,TabBar会更改索引(即使TabView内部没有)。为什么会这样?我怎么证明呢?

    class _SignState extends State<Sign> with TickerProviderStateMixin {
  static Container navBar;
  static TabBarView pages;

  @override
  Widget build(BuildContext context) {
    final tabController = TabController(length: 2, vsync: this);

    print(tabController.index);
    void _goLogin() {
      //  tabController.animateTo(tabController.previousIndex);
    }

    pages = TabBarView(controller: tabController, children: [
      LoginPage(
        keyRoot: widget.keyRoot,
      ),
      RegisterPage(
        onRegistered: _goLogin,
      ),
    ]);

    navBar = Container(
      height: 50,
      alignment: Alignment.center,
      padding: EdgeInsets.only(left: 50, right: 50),
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: Container(
                decoration: BoxDecoration(
                    color: Colors.transparent,
                    borderRadius: BorderRadius.circular(30),
                    border: Border.all(
                        width: 0.3,
                        color: Colors.black.withOpacity(0.5),
                        style: BorderStyle.solid))),
          ),
          TabBar(
              controller: tabController,
              labelPadding: EdgeInsets.all(10),
              labelColor: Colors.white,
              unselectedLabelColor: Theme.of(context).primaryColor,
              indicator: BoxDecoration(
                boxShadow: [
                  BoxShadow(
                    color: Colors.black,
                    blurRadius: 1,
                  )
                ],
                borderRadius: BorderRadius.circular(30),
                color: Theme.of(context).primaryColor,
              ),
              tabs: [Text('Accedi'), Text('Registrati')]),
        ],
      ),
    );

    // Main page

    return Scaffold(
      body: Column(
        children: <Widget>[
          _returnTop(),
          Expanded(
            child: pages,
          ),
        ],
      ),
      persistentFooterButtons: _returnButtons(),
      bottomNavigationBar: Padding(
        padding: const EdgeInsets.only(bottom: 15),
        child: navBar,
      ),
    );
  }

      Widget _returnTop() {
        return Stack(children: <Widget>[
          Center(child: Image.asset('assets/logo_sign.png')),
          Positioned(
            bottom: 0,
            right: 20,
            child: InkWell(
              splashColor: Colors.transparent,
              child: Text('salta >'),
              onTap: () {
                Navigator.pushNamed(context, "/home", arguments: widget.keyRoot);
              },
            ),
          )
        ]);
      }

      List<Widget> _returnButtons() {
        return [
          Text('oppure con'),
          IconButton(
              //TODO: Aggiungere funzioni
              color: Theme.of(context).accentColor,
              icon: Icon(FontAwesomeIcons.googlePlusG),
              tooltip: 'Login con Google',
              onPressed: () {}),
          IconButton(
              //TODO: Aggiungere funzioni
              color: Theme.of(context).accentColor,
              icon: Icon(FontAwesomeIcons.facebook),
              tooltip: 'Login con Facebook',
              onPressed: () {}),
          IconButton(
              //TODO: Aggiungere funzioni
              color: Theme.of(context).accentColor,
              icon: Icon(FontAwesomeIcons.twitter),
              tooltip: 'Login con Twitter',
              onPressed: () {}),
        ];
      }
    }

修改:这是我的完整代码:)

1 个答案:

答案 0 :(得分:0)

问题是您已在build方法中添加了TabController初始化程序。然后发生的事情是当键盘出现,运行build方法并再次初始化选项卡控制器时。正确的方法是使用initState方法初始化TabController。

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: choices.length);
  }