如何在小部件底部显示TabBar

时间:2020-08-26 19:26:17

标签: flutter

  Widget build(BuildContext context) {
return Scaffold(
  body: SingleChildScrollView(
    child: Column(
      children: [
        Container(
          color:Colors.blueGrey.shade900,
          width: double.infinity,
          height: MediaQuery.of(context).size.height * 0.15,
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Row(
                   mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                Text("Available Balance     ₹ 10050", style: TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                ),),
                ],),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Row(
                   mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                RaisedButton(onPressed: () {}, color: Colors.teal, child: Text("Recharge", style: TextStyle(
                  color: Colors.white
                ),),),
                RaisedButton(onPressed: () {}, color: Colors.teal, child: Text("Read Rule", style: TextStyle(
                  color: Colors.white
                ),),),
                Icon(Icons.refresh_rounded, color: Colors.white,)
                ],),
              ),
              SizedBox(
                height: 5,
              ),
              // I need to insert the tabBarHere
            ],
          ),
        ),
      ],
    ),
  ),
);

我是从flutter创建的窗口小部件,但是在此窗口小部件之后我无法显示TabBar。我已经看到了使用DefaultTabController()创建选项卡的基本方法,但是当我们在应用程序栏的底部使用它时,它就可以正常工作

enter image description here

2 个答案:

答案 0 :(得分:3)

您可以使用NestedScrollView

Scaffold(
  body: SafeArea(
    child: DefaultTabController(
      length: 2,
      child: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxScrolled) {
          return <Widget>[
            SliverToBoxAdapter(
              child: Container(
                color: Colors.blueGrey.shade900,
                width: double.infinity,
                height: MediaQuery.of(context).size.height * 0.15,
                child: Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Text(
                            "Available Balance     ₹ 10050",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          RaisedButton(
                            onPressed: () {},
                            color: Colors.teal,
                            child: Text(
                              "Recharge",
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                          RaisedButton(
                            onPressed: () {},
                            color: Colors.teal,
                            child: Text(
                              "Read Rule",
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                          Icon(
                            Icons.refresh,
                            color: Colors.white,
                          )
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 5,
                    ),
                  ],
                ),
              ),
            ),
            SliverToBoxAdapter(
              child: Container(
                color: Colors.blueGrey.shade900,
                child: TabBar(
                  tabs: <Widget>[
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 10.0),
                      child: Icon(Icons.card_giftcard),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 10.0),
                      child: Icon(Icons.store),
                    ),
                  ],
                ),
              ),
            ),
          ];
        },
        body: Column(
          children: <Widget>[
            Flexible(
              child: TabBarView(
                children: [
                  Center(child: Text('tab 1')),
                  Center(child: Text('tab 2')),
                ],
              ),
            ),
          ],
        ),
      ),
    ),
  )
)

结果:

res

答案 1 :(得分:0)

您可以使用bottomNavigationBar

class _MyHomePageState extends State<MyHomePage> {

  var selectedIndex = 0;

  void onItemTapped(int index) {
    setState(() {
      selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("data"),
        backgroundColor: Colors.blueGrey.shade900,
        actions: <Widget>[Icon(Icons.notifications)],
      ),
      drawer: Drawer(),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.amber,
        elevation: 12.0,
        type: BottomNavigationBarType.shifting,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.store), title: Text('Home')),
          BottomNavigationBarItem(
              icon: Icon(Icons.warning), title: Text('Warning')),
        ],
        showUnselectedLabels: true,
        currentIndex: selectedIndex,
        unselectedItemColor: Colors.white,
        fixedColor: Colors.teal,
        onTap: onItemTapped,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              color: Colors.blueGrey.shade900,
              width: double.infinity,
              height: MediaQuery.of(context).size.height * 0.15,
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text(
                          "Available Balance     ₹ 10050",
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 18,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        RaisedButton(
                          onPressed: () {},
                          color: Colors.teal,
                          child: Text(
                            "Recharge",
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                        RaisedButton(
                          onPressed: () {},
                          color: Colors.teal,
                          child: Text(
                            "Read Rule",
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                        Icon(
                          Icons.refresh,
                          color: Colors.white,
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  // I need to insert the tabBarHere
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }