TabBar控制器中的问题(对于TabBar,没有tabBar控制器)

问题描述 投票:0回答:3

因此,我试图创建一个包含3个标签的个人资料屏幕...个人资料,最新资料,但是在尝试查看时,我却遇到了错误。我无法代表所有3个标签。最近选项卡具有此小部件。我的字符用完了,因此这里是完整代码的链接:https://docs.google.com/document/d/1qs4ajPJ0DBjserBJ3iBZmPXPz1zTP7tIYSh8vceVQn8/edit?usp=sharing

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个标签。我遇到的错误是没有TabController的TabController,错误消息

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
android-studio flutter dart flutter-layout flutter-dependencies
3个回答
1
投票

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

  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{}


0
投票

您是否尝试过Initializing tabController实例:

tabController = TabController(vsync: this, length: 3);

不是要使用此功能,还必须使用SigleTickerProviderStateMixin

class _TabbedPageState extends State<TabbedPage> with SingleTickerProviderStateMixin

0
投票

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

class _UserProfilePageState extends State<UserProfilePage> with SingleTickerProviderStateMixin

然后用DefaultTabController包装支架,还记得从Tab Widget中删除tabcontroller属性:

 Widget build(BuildContext context) {
    return DefaultTabController(
        length: 3,
        child: Scaffold(...)
   )
}

© www.soinside.com 2019 - 2024. All rights reserved.