首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TabBar控制器出现问题( tabBar没有tabBar控制器)

TabBar控制器出现问题( tabBar没有tabBar控制器)
EN

Stack Overflow用户
提问于 2020-06-05 22:15:19
回答 3查看 492关注 0票数 0

所以我正在尝试创建一个有3个选项卡的个人资料屏幕...个人资料,最近和审查,但在尝试这样做时,我面临着一个错误。我不能代表所有的3个选项卡。“最近”选项卡上有此小部件。我已经用完了字符,所以这里有一个完整代码的链接:https://docs.google.com/document/d/1qs4ajPJ0DBjserBJ3iBZmPXPz1zTP7tIYSh8vceVQn8/edit?usp=sharing

代码语言:javascript
复制
import 'package:econoomaccess/UpdateMapPage.dart';
import 'package:econoomaccess/drawer.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'localization/language_constants.dart';
import 'main.dart';
import 'localization/language.dart';
import 'package:flutter/cupertino.dart';

class UserProfilePage extends StatefulWidget {
  final Map map;
  UserProfilePage({this.map});
  @override
  _UserProfilePageState createState() => _UserProfilePageState();
}

class _UserProfilePageState extends State<UserProfilePage> {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  Firestore firestore = Firestore.instance;
  var _name, _uid, _phone, _language, _location, _image, menuType = "profile";
  Language language;
  void _changeLanguage(Language language) async {
    Locale _locale = await setLocale(language.languageCode);
    MyApp.setLocale(context, _locale);
  }

  void getData() async {
    var temp;
    _auth.onAuthStateChanged.listen((user) async {
      temp = await firestore.collection('users').document(user.uid).get();
      setState(() {
        _uid = user.uid;
        _name = temp.data['name'];
        _phone = temp.data['mobileno'];
        _image = temp.data['image'];
        _language = temp.data['language'];
        _location = temp.data['city'];
      });
    });
  }

//PS THERE'S A LOT MORE CODE WHICH WAS IRRELEVANT TO THE CONTEXT OF QUES HENCE //I'VE DELETED IT
  TabController tabController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Padding(
          padding: EdgeInsets.fromLTRB(20, 0, 20, 20),
          child: SizedBox(
            height: 54,
            child: BottomNavigationBar(
              showSelectedLabels: false,
              showUnselectedLabels: false,
              backgroundColor: Color.fromRGBO(255, 255, 255, 0.8),
              currentIndex: _selectedIndex,
              selectedItemColor: Color(0xffFE506D),
              onTap: _onItemTapped,
              items: [
                BottomNavigationBarItem(
                  icon: Icon(
                    Icons.explore,
                    color: Colors.black,
                  ),
                  title: Text("Explore"),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.search, color: Colors.black),
                  title: Text("Search"),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.bookmark_border, color: Colors.black),
                  title: Text("Faavorites"),
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.perm_identity, color: Color(0xffFE506D)),
                  title: Text("Shop"),
                )
              ],
            ),
          )),
      drawer: DrawerWidget(uid: this.uid),
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.black),
        elevation: 0,
      ),
      backgroundColor: Color(0xffE5E5E5),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Column(
              children: <Widget>[
                ClipRRect(
                  borderRadius: BorderRadius.only(
                      bottomRight: Radius.circular(25),
                      bottomLeft: Radius.circular(25)),
                  child: Container(
                    height: 230,
                    width: MediaQuery.of(context).size.width,
                    decoration: new BoxDecoration(
                      // border: new Border.all(width: 1.0, color: Colors.black),
                      shape: BoxShape.rectangle,
                      color: Colors.white,
                      boxShadow: <BoxShadow>[
                        BoxShadow(
                          color: Colors.black,
                          offset: Offset(20.0, 30.0),
                          blurRadius: 40.0,
                        ),
                      ],
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Container(
                            width: 155,
                            height: 155,
                            decoration: new BoxDecoration(
                                shape: BoxShape.circle,
                                image: new DecorationImage(
                                    fit: BoxFit.cover,
                                    image: new NetworkImage("$_image")))),
                        SizedBox(height: 10),
                        Text("$_name",
                            style: TextStyle(
                                fontSize: 25, fontWeight: FontWeight.bold))
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 15.0),
                      child: Container(
                        // margin: EdgeInsets.only(left: 15.0),
                        child: TabBar(
                          labelPadding: EdgeInsets.only(right: 30.0),
                          indicatorColor: Colors.transparent,
                          controller: tabController,
                          labelColor: Colors.black,
                          unselectedLabelColor: Colors.black26,
                          labelStyle: TextStyle(fontSize: 18,fontFamily: "Gilroy"),
                          unselectedLabelStyle: TextStyle(fontSize: 15,fontFamily: "Gilroy"),
                          isScrollable: true,
                          tabs: [
                            Tab(
                              text: getTranslated(context, "profile"),
                            ),
                            Tab(
                              text: getTranslated(context, "reviews"),
                            ),
                            Tab(
                              text: getTranslated(context, "recent"),
                            ),
                          ]
                        )
                      ),
                  ),
                ),
                Expanded(
                    child: TabBarView(
                        controller: tabController,
                        children: [
                          ProfileItems(), ReviewItems(), RecentItems()
                        ]
                    )
                )
              ],
            ),
          )
        ],
      )
    );
  }
}

我无法显示这3个选项卡。我面临TabBar没有TabController的错误,错误消息

代码语言:javascript
复制
The following assertion was thrown building Container:
No TabController for TabBar.

In this case, there was neither an explicit controller nor a default controller.
The relevant error-causing widget was: 
Container file:///C:/Flutter/Naniz_eats/lib/UserProfilePage.dart:658:30

(2) Exception caught by widgets library ═══════════════════════════════════════════════════
No TabController for TabBarView.
The relevant error-causing widget was: 
Expanded file:///C:/Flutter/Naniz_eats/lib/UserProfilePage.dart:684:17
EN

回答 3

Stack Overflow用户

发布于 2020-06-05 22:43:00

您必须在有状态小部件的initState方法中初始化tabController。检查下面的代码:

//创建TabController TabController tabController实例;

代码语言:javascript
复制
  void initState() {
    // TODO: implement initState
    // initialise the tab controller here
    tabController = TabController(vsync: this, length: 3);
    super.initState();
    getUser();
    getData();
  }

另外,向有状态小部件添加一个mixin。检查下面的代码:

class _UserProfilePageState extends State<UserProfilePage> with SingleTickerProviderStateMixin{}

票数 1
EN

Stack Overflow用户

发布于 2020-06-05 22:23:57

您是否尝试过初始化 tabController实例:

代码语言:javascript
复制
tabController = TabController(vsync: this, length: 3);

要实现这一点,您还必须使用SigleTickerProviderStateMixin

代码语言:javascript
复制
class _TabbedPageState extends State<TabbedPage> with SingleTickerProviderStateMixin
票数 0
EN

Stack Overflow用户

发布于 2020-06-05 23:39:09

所以这个问题的答案。在@T.TSage的帮助下,首先我必须使用SingleTickerProviderStateMixin扩展userProfilePage类,如下所示:

代码语言:javascript
复制
class _UserProfilePageState extends State<UserProfilePage> with SingleTickerProviderStateMixin

然后用DefaultTabController包装脚手架,记住要从Tab Widget中删除tabcontroller属性:

代码语言:javascript
复制
 Widget build(BuildContext context) {
    return DefaultTabController(
        length: 3,
        child: Scaffold(...)
   )
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62217410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档