【问题标题】:How to add TabBarView inside a widget element in BottomNavigationBar如何在 BottomNavigationBar 的小部件元素内添加 TabBarView
【发布时间】:2020-09-28 04:47:43
【问题描述】:

现在我的应用如下所示

我想在 Browse 小部件中添加一个 TabBar。现在在浏览小部件中它显示一个简单的文本小部件。

代码如下

import 'package:flutter/material.dart';

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

/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

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

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
   Text(
     'slkdfjds',
     style: optionStyle
   ),
    Text(
      'Shopping List Window',
      style: optionStyle,
    ),
    Text(
      'Account Window',
      style: optionStyle,
    ),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Browse Flyers'),
      ),
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.label),
            title: Text('Browse'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.shopping_cart),
            title: Text('Shopping List'),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.account_box),
            title: Text('Account'),
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.blue[300],
        onTap: _onItemTapped,
      ),
    );
  }
}

我想要实现的是当用户单击浏览选项卡时有一个如下所示的 TabBarView

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    您可以定义一个Widget _tabSection() 并将其放入_widgetOptions
    代码sn-p

    @override
      initState() {
        _widgetOptions = <Widget>[
          _tabSection(),
          Text(
            'Shopping List Window',
            style: optionStyle,
          ),
          Text(
            'Account Window',
            style: optionStyle,
          ),
        ];
      }
    
      Widget _tabSection() {
        return DefaultTabController(
          length: 3,
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Container(
                height: 50,
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    /// This Widget is the main application widget.
    class MyApp extends StatelessWidget {
      static const String _title = 'Flutter Code Sample';
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: _title,
          home: MyStatefulWidget(),
        );
      }
    }
    
    class MyStatefulWidget extends StatefulWidget {
      MyStatefulWidget({Key key}) : super(key: key);
    
      @override
      _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      int _selectedIndex = 0;
      static const TextStyle optionStyle =
          TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
      static List<Widget> _widgetOptions;
    
      @override
      initState() {
        _widgetOptions = <Widget>[
          _tabSection(),
          Text(
            'Shopping List Window',
            style: optionStyle,
          ),
          Text(
            'Account Window',
            style: optionStyle,
          ),
        ];
      }
    
      Widget _tabSection() {
        return DefaultTabController(
          length: 3,
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Container(
                height: 50,
                color: Colors.black12,
                child: TabBar(
                    labelColor: Colors.deepOrange,
                    unselectedLabelColor: Colors.white,
                    tabs: [
                      Tab(text: "SMALL"),
                      Tab(text: "MEDIUM"),
                      Tab(text: "LARGE"),
                    ]),
              ),
              Expanded(
                child: Container(
                  color: Colors.green,
                  child: TabBarView(children: [
                    Container(
                      child: Text("SMALL Body"),
                    ),
                    Container(
                      child: Text("MEDIUM Body"),
                    ),
                    Container(
                      child: Text("LARGE Body"),
                    ),
                  ]),
                ),
              ),
            ],
          ),
        );
      }
    
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Browse Flyers'),
          ),
          body: Center(
            child: _widgetOptions.elementAt(_selectedIndex),
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.label),
                title: Text('Browse'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.shopping_cart),
                title: Text('Shopping List'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.account_box),
                title: Text('Account'),
              ),
            ],
            currentIndex: _selectedIndex,
            selectedItemColor: Colors.blue[300],
            onTap: _onItemTapped,
          ),
        );
      }
    }
    

    【讨论】:

    • 谢谢,这绝对是我想要的。如果您能稍微解释一下代码,那就太好了。但我查了一下..谢谢
    • _widgetOptions 可以放一个List of Widgets,这样你就可以创建一个tabSection Widget 有你的TabBar 和TabBarView 的逻辑。要让 TabBar 和 TabBarView 适合整个屏幕,您可以使用 Expanded。
    【解决方案2】:

    您需要创建一个小部件 Browse,然后将您的 tabview 添加到其中。然后替换 Text( 'slkdfjds', style: optionStyle ), 在我的情况下使用您的新小部件TabBarDemo

    一个例子

     import 'package:flutter/material.dart';
    
    void main() {
      runApp(TabBarDemo());
    }
    
    class TabBarDemo extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: DefaultTabController(
            length: 3,
            child: Scaffold(
              appBar: AppBar(
                bottom: TabBar(
                  tabs: [
                    Tab(icon: Icon(Icons.directions_car)),
                    Tab(icon: Icon(Icons.directions_transit)),
                    Tab(icon: Icon(Icons.directions_bike)),
                  ],
                ),
                title: Text('Tabs Demo'),
              ),
              body: TabBarView(
                children: [
                  Icon(Icons.directions_car),
                  Icon(Icons.directions_transit),
                  Icon(Icons.directions_bike),
                ],
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 这是在主小部件中添加标签栏的方式。我想要的是在 bottomNavigationBar 项目中添加标签栏。例如,当用户单击底部的浏览按钮时,我希望标签栏出现
    【解决方案3】:

    您想要的底部导航栏视图之一需要有标签栏,如下所示:

    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      int _selectedIndex = 0;
      static const TextStyle optionStyle =
          TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
      static List<Widget> _widgetOptions = <Widget>[
       DefaultTabController(
            length: 3,
            child: Scaffold(
              appBar: AppBar(
                bottom: TabBar(
                  tabs: [
                    Tab(icon: Icon(Icons.directions_car)),
                    Tab(icon: Icon(Icons.directions_transit)),
                    Tab(icon: Icon(Icons.directions_bike)),
                  ],
                ),
                title: Text('Tabs Demo'),
              ),
              body: TabBarView(
                children: [
                  Icon(Icons.directions_car),
                  Icon(Icons.directions_transit),
                  Icon(Icons.directions_bike),
                ],
              ),
            ),
          ),
        Text(
          'Shopping List Window',
          style: optionStyle,
        ),
        Text(
          'Account Window',
          style: optionStyle,
        ),
      ];
    
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Browse Flyers'),
          ),
          body: Center(
            child: _widgetOptions.elementAt(_selectedIndex),
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.label),
                title: Text('Browse'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.shopping_cart),
                title: Text('Shopping List'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.account_box),
                title: Text('Account'),
              ),
            ],
            currentIndex: _selectedIndex,
            selectedItemColor: Colors.blue[300],
            onTap: _onItemTapped,
          ),
        );
      }
    }
    

    这种方式只有browse页面有标签栏。

    【讨论】:

    • 此代码不起作用,因为您已在小部件中初始化了 Scaffold。在这种情况下,它也会给你一个错误,The arguments must not be null
    猜你喜欢
    • 2019-10-06
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2019-11-28
    • 1970-01-01
    相关资源
    最近更新 更多