Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 颤振:使用按钮在选项卡栏视图中更改当前选项卡_Dart_Flutter - Fatal编程技术网

Dart 颤振:使用按钮在选项卡栏视图中更改当前选项卡

Dart 颤振:使用按钮在选项卡栏视图中更改当前选项卡,dart,flutter,Dart,Flutter,我正在创建一个在其主页上包含选项卡栏的应用程序。我希望能够使用myFloatingActionButton导航到其中一个选项卡。此外,我希望保留导航到该选项卡的默认方法,即在屏幕上滑动或单击该选项卡 我还想知道如何将该选项卡链接到其他按钮 这是我的主页截图 您需要获取选项卡栏控制器,并从按钮onPressed()句柄调用其animateTo()方法 import 'package:flutter/material.dart'; void main() => runApp(new MyAp

我正在创建一个在其主页上包含选项卡栏的应用程序。我希望能够使用my
FloatingActionButton
导航到其中一个选项卡。此外,我希望保留导航到该选项卡的默认方法,即在屏幕上滑动或单击该选项卡

我还想知道如何将该选项卡链接到其他按钮

这是我的主页截图


您需要获取
选项卡栏
控制器,并从按钮
onPressed()
句柄调用其
animateTo()
方法

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyTabbedPage(),
    );
  }
}

class MyTabbedPage extends StatefulWidget {
  const MyTabbedPage({Key key}) : super(key: key);

  @override
  _MyTabbedPageState createState() => new _MyTabbedPageState();
}

class _MyTabbedPageState extends State<MyTabbedPage> with SingleTickerProviderStateMixin {
  final List<Tab> myTabs = <Tab>[
    new Tab(text: 'LEFT'),
    new Tab(text: 'RIGHT'),
  ];

  TabController _tabController;

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

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

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Tab demo"),
        bottom: new TabBar(
          controller: _tabController,
          tabs: myTabs,
        ),
      ),
      body: new TabBarView(
        controller: _tabController,
        children: myTabs.map((Tab tab) {
          return new Center(child: new Text(tab.text));
        }).toList(),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: () => _tabController.animateTo((_tabController.index + 1) % 2), // Switch tabs
        child: new Icon(Icons.swap_horiz),
      ),
    );
  }
}
您可以在任何地方进行以下操作:


MyApp.\u myTabbedPageKey.currentState.\u tabController.animateTo(…)

如果要跳转到特定页面,可以使用

PageController.jumpToPage(int)
但是,如果需要动画,可以使用

PageController.animateToPage(page, duration: duration, curve: curve)

简单的例子来演示它

// create a PageController
final _controller = PageController();
bool _shouldAnimate = true; // whether we animate or jump

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    floatingActionButton: FloatingActionButton(
      onPressed: () {
        if (_shouldAnimate) {
          // animates to page1 with animation
          _controller.animateToPage(1, duration: Duration(seconds: 1), curve: Curves.easeOut);  
        } else {
          // jump to page1 without animation
          _controller.jumpToPage(1);
        }
      },
    ),
    body: PageView(
      controller: _controller, // assign it to PageView
      children: <Widget>[
        FlutterLogo(colors: Colors.orange), // page0
        FlutterLogo(colors: Colors.green), // page1
        FlutterLogo(colors: Colors.red), // page2
      ],
    ),
  );
}
//创建页面控制器
最终_controller=PageController();
bool _shouldAnimate=true;//我们是动画还是跳跃
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(),
浮动操作按钮:浮动操作按钮(
已按下:(){
如果(你应该设置动画){
//使用动画设置到第1页的动画
_controller.animateToPage(1,持续时间:持续时间(秒数:1),曲线:Curves.easeOut);
}否则{
//不带动画跳转到第1页
_控制器。跳线页(1);
}
},
),
正文:页面视图(
控制器:\ u控制器,//将其分配给页面视图
儿童:[
徽标(颜色:colors.orange),//第0页
徽标(颜色:colors.green),//第1页
徽标(颜色:colors.red),//第2页
],
),
);
}
是正确的,但为了进一步澄清/提示,如果您想“从任何地方”调用您的tabcontroller,还需要通过删除下划线确保tabcontroller不是类的私有属性,否则即使使用GlobalKey,远程类也无法看到带有所提供示例的tabcontroller

换句话说,改变

TabController _tabController;
致:

改变

MyApp._myTabbedPageKey.currentState._tabController.animateTo(...);
致:


您可以在任何其他地方引用tabcontroller。

您可以使用
tabcontroller

TabController _controller = TabController(
  vsync: this,
  length: 3,
  initialIndex: 0,
);

_controller.animateTo(_currentTabIndex);

return Scaffold(
  appBar: AppBar(
    bottom: TabBar(
      controller: _controller,
      tabs: [
        ...
      ],
    ),
  ),
  body: TabBarView(
    controller: _controller,
    children: [
      ...
    ],
  ),
);
然后,
设置状态
以更新屏幕:

int _currentTabIndex = 0;

setState(() {
  _currentTabIndex = 1;
});
class选项卡栏
类TabBarScreen扩展StatefulWidget{
TabBarScreen({Key}):超级(Key:Key);
@凌驾
_TabBarScreenState createState()=>TabBarScreenState();
}
最终列表选项卡=[
选项卡(文本:“第1页”),
选项卡(文本:“第2页”),
];
类_TabBarScreenState使用SingleTickerProviderStateMixin扩展状态{
TabController TabController;
@凌驾
void initState(){
super.initState();
tabController=新的tabController(vsync:this,长度:tabs.length);
}
@凌驾
无效处置(){
tabController.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回DefaultTabController(
长度:2,
孩子:脚手架(
背景色:主题。背景色,
appBar:appBar(
背景色:主题。背景色,
标题:对,
形状:边框(底部:边框边(颜色:Colors.white)),
标题:文本(“选项卡栏”),
底部:选项卡栏(
控制器:选项卡控制器,
标签:标签,
指标重量:5,
指示颜色:颜色。白色,
labelColor:Colors.white,
),
),
正文:选项卡视图(
控制器:选项卡控制器,
儿童:[
PageOneScreen(控制器:选项卡控制器),
PageTwoScreen(控制器:选项卡控制器),
],
),
),
);
}
}
第一页
类PageOneScreen扩展StatefulWidget{
@凌驾
_PageOneScreenState createState()=>\u PageOneScreenState();
PageOneScreen({controller}){
tabController=控制器;
}
}
TabController TabController;
类_PageOneScreenState扩展状态{
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
升起的按钮(
已按下:(){
tabController.animateTo(1);//编号:索引页
},
子:文本(
“转到第2页”,
),
),
],
);
}
}

我迟到了,但希望有人能从中受益。只需将这一行添加到按下的按钮上,并确保将索引编号更改为首选索引:

DefaultTabController.of(context).animateTo(1);

是否存在从其他有状态小部件回调触发器更新tabView页面内容的回调。但是如何在它们之间传递数据?是的,您如何能够将参数传递到目标小部件?\u tabController.animateTo((\u tabController.index+1)帮助我导航到下一个选项卡。非常感谢Chemamolins。是否有一种方法可以像使用导航器那样将参数传递到目标页面。push(上下文,MaterialPageRoute(生成器:(\u)=>LogoScreen(颜色:“红色”))
?有一个
onPageChanged
回调,您会给您当前页面,因此您可以相应地检索参数,如果我没有正确理解您的问题,您可以简单地问一个新问题。虽然此代码可以回答问题,但提供了有关如何和/或为什么解决问题的其他上下文将提高答案的长期价值。我一直喜欢一行解决方案:)谢谢你,伙计!正是我想要的,对我来说不起作用。据说调用null方法'animateTo'时调用null。接收者:nul
TabController _controller = TabController(
  vsync: this,
  length: 3,
  initialIndex: 0,
);

_controller.animateTo(_currentTabIndex);

return Scaffold(
  appBar: AppBar(
    bottom: TabBar(
      controller: _controller,
      tabs: [
        ...
      ],
    ),
  ),
  body: TabBarView(
    controller: _controller,
    children: [
      ...
    ],
  ),
);
int _currentTabIndex = 0;

setState(() {
  _currentTabIndex = 1;
});
DefaultTabController.of(context).animateTo(1);
DefaultTabController(
              length: 4,
              initialIndex: 0,
              child: TabBar(
                tabs: [
                  Tab(
                    child: Text(
                      "People",
                      style: TextStyle(
                        color: Colors.black,
                      ),
                    ),
                  ),
                  Tab(
                    child: Text(
                      "Events",
                      style: TextStyle(
                        color: Colors.black,
                      ),
                    ),
                  ),
                  Tab(
                    child: Text(
                      "Places",
                      style: TextStyle(
                        color: Colors.black,
                      ),
                    ),
                  ),
                  Tab(
                    child: Text(
                      "HashTags",
                      style: TextStyle(
                        color: Colors.black,
                      ),
                    ),
                  ),
                ],
              ),
            )