草庐IT

Flutter - TextField 焦点改变 TabBar 选中索引

coder 2023-07-22 原文

我有一个脚手架,其中有一个 TabBarTabBarView 和一个 TextField

TabBar 有 3 个选项卡(例如选项卡 A、B 和 C),TabBarView 有 3 个 View ,这个 TextField 位于最后一个选项卡(选项卡 C)。

一切正常,但每当我将焦点放在 TextField 上键入内容时,TabBar 就会从选项卡 C 更改为选项卡 A。非常烦人。这不应该发生。 TabBarView 保持不变。

我在 initState 中创建了 Controller 。像这样:

@override
void initState() {
  super.initState();
  widget._tabBarController =
      new TabController(length: 3, vsync: this);
}

知道为什么会这样吗?

代码:

class AtendimentoOrtoWidget extends StatefulWidget {
  TabController _tabBarController;

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


class _AtendimentoOrtoWidgetState extends State<AtendimentoOrtoWidget>
    with SingleTickerProviderStateMixin {

      @override
  void initState() {
    super.initState();


    widget._tabBarController =
        new TabController(length: 3, vsync: this);

  }

 @override
    Widget build(BuildContext context) {
    return SafeArea(
        top: false,
        child: new DefaultTabController(
            length: 3,
            child: new Scaffold(
                resizeToAvoidBottomPadding: false,
                appBar: new AppBar(
                  toolbarOpacity: 0.5,
                  automaticallyImplyLeading: true,
                  backgroundColor: Colors.white,
                  elevation: 2.0,
                  title: new TabBar(
                    controller: widget._tabBarController,
                    unselectedLabelColor: Colors.black,
                    indicatorColor: Colors.black,
                    labelColor: Colors.black,
                    // indicatorWeight: 0.0,
                    isScrollable: true,
                    labelStyle: new TextStyle(
                        fontSize: 16.0,
                        fontFamily: 'Caecilia',
                        fontWeight: FontWeight.w700),
                    tabs: <Widget>[
                      new Tab(
                              text: "TAB A",
                      ),
                      new Tab(
                              text: "TAB B",
                      ),
                      new Tab(
                              text: "TAB C",
                            )
                    ],
                  ),
                ),
                backgroundColor: Colors.white,
                body: new TabBarView(
                        controller: widget._tabBarController,
                        children: <Widget>[
                          new Container(),
                          new Container(),
                          new TextField()
                        ],
                      ))));
  }

  }

最佳答案

我试过了。检查下面的代码。如果您仍然面临同样的问题,请分享您的实现。

import 'package:flutter/material.dart';

class TabScreen extends StatefulWidget {
  @override
  _TabScreenState createState() => _TabScreenState();
}

class _TabScreenState extends State<TabScreen> with SingleTickerProviderStateMixin {
  GlobalKey<ScaffoldState> _scaffoldKey = new GlobalObjectKey<ScaffoldState>('TabScreen');

  TabController tabController;

  @override
  void initState() {
    super.initState();
    tabController = new TabController(length: 3, vsync: this);
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text("Tab Demo"),
      ),
      backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          TabBar(
            controller: tabController,
            tabs: <Widget>[
              Tab(
                child: Container(
                  child: new Text(
                    'A',
                    style: TextStyle(color: Colors.black),
                  ),
                ),
              ),
              Tab(
                child: Container(
                  child: Text(
                    'B',
                    style: TextStyle(color: Colors.black),
                  ),
                ),
              ),
              Tab(
                child: Container(
                  child: Text(
                    'C',
                    style: TextStyle(color: Colors.black),
                  ),
                ),
              )
            ],
          ),
          Flexible(
              child: TabBarView(
            controller: tabController,
            children: <Widget>[
              Placeholder(),
              Placeholder(),
              ListView(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: TextField(
                      decoration: InputDecoration(labelText: "Name"),
                    ),
                  ),
                ],
              ),
            ],
          ))
        ],
      ),
    );
  }
}

关于Flutter - TextField 焦点改变 TabBar 选中索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56395058/

有关Flutter - TextField 焦点改变 TabBar 选中索引的更多相关文章

  1. ruby-on-rails - 协会的 Rails 索引 - 2

    我发现自己需要这个。假设cart是一个包含用户列表的模型。defindex_of_itemcart.users.each_with_indexdo|u,i|ifu==current_userreturniendend获取此类关联索引的更简单方法是什么? 最佳答案 indexArray上的方法与您的index_of_item方法相同,例如cart.users.index(current_user)返回数组中第一个对象的索引==给obj。如果未找到匹配项,则返回nil。 关于ruby-on-

  2. ruby - Rails -- :id attribute? 所需的数据库索引 - 2

    因此,当我遵循MichaelHartl的RubyonRails教程时,我注意到在用户表中,我们为:email属性添加了一个唯一索引,以提高find的效率方法,因此它不会逐行搜索。到目前为止,我们一直在根据情况使用find_by_email和find_by_id进行搜索。然而,我们从未为:id属性设置索引。:id是否自动索引,因为它在默认情况下是唯一的并且本质上是顺序的?或者情况并非如此,我应该为:id搜索添加索引吗? 最佳答案 大多数数据库(包括sqlite,这是RoR中的默认数据库)会自动索引主键,对于RailsMigration

  3. ruby - 改变替换的大小写 - 2

    我有以下内容:text.gsub(/(lower)(upper)/,'\1\2')我可以将\2替换为大写吗?类似于:sed-e's/\(abc\)/\U\1/'这在Ruby中可行吗? 最佳答案 查看gsub文档:str.gsub(模式){|匹配|block}→new_str在block形式中,当前匹配字符串作为参数传入,$1、$2、$`、$&、$'等变量将被适当设置。block返回的值将替换为每次调用的匹配项。"alowerupperb".gsub(/(lower)(upper)/){|s|$1+""+$2.upcase}

  4. ruby - 引用具有指定索引的枚举器值 - 2

    假设我有一个可枚举对象enum,现在我想获取第三个项目。我知道一种通用方法是转换成数组,然后使用索引访问,如:enum.to_a[2]但这种方式会创建一个临时数组,效率可能很低。现在我使用:enum.each_with_index{|v,i|breakvifi==2}但这非常丑陋和多余。执行此操作最有效的方法是什么? 最佳答案 你可以使用take剥离前三个元素,然后剥离last从take给你的数组中获取第三个元素:third=enum.take(3).last如果您根本不想生成任何数组,那么也许:#Ifenumisn'tanEnum

  5. ruby - 将 Logstash 中的时间戳时区转换为输出索引名称 - 2

    在我的场景中,Logstash收到的系统日志行的“时间戳”是UTC,我们在Elasticsearch输出中使用事件“时间戳”:output{elasticsearch{embedded=>falsehost=>localhostport=>9200protocol=>httpcluster=>'elasticsearch'index=>"syslog-%{+YYYY.MM.dd}"}}我的问题是,在UTC午夜,Logstash在外时区(GMT-4=>America/Montreal)结束前将日志发送到不同的索引,并且索引在20小时(晚上8点)之后没有日志,因为“时间戳”是UTC。我们已

  6. ruby - 从特定索引开始迭代数组 - 2

    我想从特定索引开始遍历数组。我该怎么做?myj.eachdo|temp|...end 最佳答案 执行以下操作:your_array[your_index..-1].eachdo|temp|###end 关于ruby-从特定索引开始迭代数组,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/44151758/

  7. ruby - Array of Arrays,根据索引处的数组内容删除一个索引? - 2

    我一直在努力学习如何处理由数组组成的数组。假设我有这个数组:my_array=[['ORANGE',1],['APPLE',2],['PEACH',3]我将如何找到包含'apple'的my_array索引并删除该索引(删除子数组['APPLE',2]因为'apple'包含在该索引的数组中)?谢谢-我非常感谢这里的帮助。 最佳答案 您可以使用Array.select过滤掉项目:>>a=[['ORANGE',1],['APPLE',2],['PEACH',3]]=>[["ORANGE",1],["APPLE",2],["PEACH",3

  8. ruby - 如何使用部分字符串搜索数组并返回索引? - 2

    我想使用部分字符串搜索数组,然后获取找到该字符串的索引。例如:a=["Thisisline1","Wehaveline2here","andfinallyline3","potato"]a.index("potato")#thisreturns3a.index("Wehave")#thisreturnsnil使用a.grep将返回完整的字符串,使用a.any?将返回正确的true/false语句,但都不会返回匹配的索引找到了,或者至少我不知道该怎么做。我正在编写一段代码,该代码读取文件、查找特定header,然后返回该header的索引,以便它可以将其用作future搜索的偏移量。如果

  9. ruby-on-rails - Rails 4 从迁移索引中删除迁移 ID - 2

    如何在rakedb:migrate:status中删除带有“**NOFILE**”的迁移ID列表?例如:StatusMigrationIDMigrationName--------------------------------------------------up20131017204224Createusersup20131218005823**********NOFILE**********up20131218011334**********NOFILE**********我不明白为什么当我自己手动删除它时它仍然保留旧的迁移文件,因为我正在研究迁移的工作原理。这是为了记录吗?但

  10. ruby - 根据子哈希值获取数组索引 - 2

    假设我有这个:[{:id=>34,:votes_count=>3},{:id=>2,:votes_count=>0},]如何根据id获取索引?我想要做的是在搜索id:34时返回0,在搜索id:21/。什么是最有效的方法? 最佳答案 你可以将一个block传递给#index:array.index{|h|h[:id]==34}#=>0 关于ruby-根据子哈希值获取数组索引,我们在StackOverflow上找到一个类似的问题: https://stackove

随机推荐