【问题标题】:No TabController for TabBarTabBar 没有 TabController
【发布时间】:2020-10-27 03:25:16
【问题描述】:

我正在尝试向我的应用程序添加一个 tabBar,但在构建它时遇到了一些问题。我已经按照文档添加了一个 tabController,但是我收到一个错误,说我没有。我的代码如下。

class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
  TabController _controller;
  final List<Tab> topTabs = <Tab>[
    new Tab(text: 'Profile'),
    new Tab(text: 'Match'),
    new Tab(text: 'Chat'),
  ];

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

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

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


  @override
  Widget build(BuildContext context) {
      return  Scaffold(
            appBar: AppBar(
              title: Text('MyApp'),
              bottom: TabBar(
                  controller: _controller,
                  tabs: topTabs,
              ),
            ),
            body: TabBarView(
                controller: _controller,
                children: [
              new Container(
                color: Colors.lightBlueAccent,
                child: Center(child: Text('Profile', style: TextStyle(color: Colors.white),),),
              ),
              new Container(
                color: Colors.purpleAccent,
                child: Center(child: Text('Match', style: TextStyle(color: Colors.white),),),
              ),
              new Container(
                color: Colors.lightGreenAccent,
                child: Center(child: Text('Chat', style: TextStyle(color: Colors.white),),),
            )
            ]),
          );
  }
}

确切的错误是颤振:══╡小部件库╞═════════════════════════════ ═══════════════════════════ 颤振:在构建 MediaQuery(MediaQueryData(size: Size(375.0, 667.0), 颤振:devicePixelRatio:2.0,textScaleFactor:1.0,platformBrightness:Brightness.light,填充: 颤振:EdgeInsets.zero,viewPadding:EdgeInsets.zero,viewInsets:EdgeInsets.zero,physicalDepth: 颤振:1.7976931348623157e+308,alwaysUse24HourFormat:false,accessibleNavigation:false,highContrast: 颤振:假,禁用动画:假,反转颜色:假,粗体文字:假)): 颤动:TabBar 没有 TabController。 颤振:创建 TabBar 时,您必须使用“控制器”提供显式 TabController flutter: 属性,或者你必须确保TabBar上方有一个DefaultTabController。 Flutter:在这种情况下,既没有显式控制器也没有默认控制器。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我可以毫无问题地运行它,您是否在创建包含 MaterialApp 的项目时覆盖了 MyApp 示例?

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: MyAppState()
        );
      }
    }
    
    class MyAppState extends StatefulWidget{
      
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyAppState> with TickerProviderStateMixin {
      TabController _controller;
      final List<Tab> topTabs = <Tab>[
        new Tab(text: 'Profile'),
        new Tab(text: 'Match'),
        new Tab(text: 'Chat'),
      ];
    
      @override
      void initState() {
       super.initState();
    
       _controller = TabController(vsync: this, length: 3);
      }
    
      @override
      void dispose() {
        _controller.dispose();
        super.dispose();
      }
    
    
      @override
      Widget build(BuildContext context) {
          return  Scaffold(
                appBar: AppBar(
                  title: Text('MyApp'),
                  bottom: TabBar(
                      controller: _controller,
                      tabs: topTabs,
                  ),
                ),
                body: TabBarView(
                    controller: _controller,
                    children: [
                  new Container(
                    color: Colors.lightBlueAccent,
                    child: Center(child: Text('Profile', style: TextStyle(color: Colors.white),),),
                  ),
                  new Container(
                    color: Colors.purpleAccent,
                    child: Center(child: Text('Match', style: TextStyle(color: Colors.white),),),
                  ),
                  new Container(
                    color: Colors.lightGreenAccent,
                    child: Center(child: Text('Chat', style: TextStyle(color: Colors.white),),),
                )
                ]),
              );
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2022-08-24
    • 2012-10-05
    • 2020-09-24
    • 2018-11-09
    • 1970-01-01
    相关资源
    最近更新 更多