【问题标题】:Unable to display Tabview in TabBar, and link the content无法在 TabBar 中显示 Tabview,并链接内容
【发布时间】:2021-01-27 15:12:27
【问题描述】:

我需要一些帮助来在我的代码中实现 TabView。我创建了两个单独的类,一个带有我的 defaultTabController 和 TabBar,一个带有 TabView。在我的 Scaffold 中,我只能让它显示我的 TabBar,但我无法弄清楚如何将内容链接到选项卡。任何帮助将不胜感激!

import 'package:anestesi_v1/shared/drawer.dart';
import "package:flutter/material.dart";
import 'package:anestesi_v1/shared/appbar.dart';


class BarneAnestesi extends StatelessWidget {
  static const String routeName ='/BarneAnestesi';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: ApplicationToolbar(),

      backgroundColor: Colors.white,
      drawer: MyDrawer(),

      body: barnTab(),
      extendBody: barnTabView(),

    );
  }
}
class barnTab extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: DefaultTabController(
        length: 2,

        child: TabBar(
          tabs: [
            Tab(icon: Icon(Icons.child_care, color: Colors.white,)),
            Tab(icon: Icon(Icons.article, color: Colors.white,)),
          ],

        ),),

    );

  }
}

class barnTabView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
        child: TabBarView(children: <Widget>[
          Text("Test1"),
          Text("Test2"),
        ])

    );
  }
}

appbar.dart

import 'package:flutter/material.dart';


class ApplicationToolbar extends StatelessWidget with PreferredSizeWidget{
  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: Text('Ane'),
      backgroundColor: Colors.red[600],
      centerTitle: false,

      actions: <Widget> [
        FlatButton.icon(
          icon: Icon(Icons.person, color:Colors.white,),
          label: Text('Logg ut',),
          textColor: Colors.white,


        ),
        FlatButton.icon(
          icon: Icon(Icons.settings, color:Colors.white,),
          label: Text("Innstillinger"),
          textColor: Colors.white,

        )
      ],);
  }

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您只需将ScaffoldDefaultTabController 包装起来,因此TabBarTabBarView 在小部件树中位于DefaultTabController 下方

    import 'package:anestesi_v1/shared/drawer.dart';
    import "package:flutter/material.dart";
    import 'package:anestesi_v1/shared/appbar.dart';
    
    class BarneAnestesi extends StatelessWidget {
      static const String routeName = '/BarneAnestesi';
      @override
      Widget build(BuildContext context) {
        return DefaultTabController(
          length: 2,
          child: Scaffold(
            appBar: ApplicationToolbar(),
            backgroundColor: Colors.white,
            drawer: MyDrawer(),
            body: barnTabView(),
          ),
        );
      }
    }
    
    
    class barnTabView extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Container(
            child: TabBarView(
          children: <Widget>[
          Text("Test1"),
          Text("Test2"),
        ]));
      }
    }
    

    appbar.dart

    import 'package:flutter/material.dart';
    
    
    class ApplicationToolbar extends StatelessWidget with PreferredSizeWidget{
      @override
      Widget build(BuildContext context) {
        return AppBar(
          title: Text('Ane'),
          backgroundColor: Colors.red[600],
          centerTitle: false,
          // add here the TabBar widget
          bottom: TabBar(
            tabs: [
              Tab(
                  icon: Icon(
                Icons.child_care,
                color: Colors.white,
              )),
              Tab(
                  icon: Icon(
                Icons.article,
                color: Colors.white,
              )),
            ],
          ),
          actions: <Widget> [
            FlatButton.icon(
              icon: Icon(Icons.person, color:Colors.white,),
              label: Text('Logg ut',),
              textColor: Colors.white,
    
    
            ),
            FlatButton.icon(
              icon: Icon(Icons.settings, color:Colors.white,),
              label: Text("Innstillinger"),
              textColor: Colors.white,
    
            )
          ],);
      }
    
      @override
      Size get preferredSize => Size.fromHeight(kToolbarHeight);
    }
    

    【讨论】:

    • 我已经尝试过该解决方案。但是我在extendBody-argument 上遇到错误。这是使用错误的论点吗?
    • 我会在一分钟内更新我的答案。将 TabBarView 分配给 body 参数,并且 TabBar 需要在 AppBar 小部件的“底部”属性内分配。这应该适合你
    • 我正在使用自定义 AppBar,在我将 TabBar 分配给 body-argument 之前,“bottom”-argument 给了我错误。我将在主帖中添加我的 appbar-code
    • 它不会给你一个错误,因为现在'DefaultTabController'位于小部件树的顶部
    • 我已经将它添加到第一个帖子中。它在帖子的底部。
    猜你喜欢
    • 1970-01-01
    • 2015-11-08
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2011-01-03
    相关资源
    最近更新 更多