Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom tabBar in flutter

I want to implement a custom scrollable TabBar in my body, not in the appBar

enter image description here

like image 544
sajjad sohrabi Avatar asked Nov 17 '25 23:11

sajjad sohrabi


2 Answers

PageView and PageController

So this isn't exactly what you are looking for, instead of bottom bar you could do a horizontal scroll (scrollView), but I hope this pushes you in the right direction. This code basically uses pageView to display pages, and since there is a page controller you can animate any button or onPress to a specific page.

Let me know if you have any questions!

import 'package:flutter/material.dart';

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

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

class _TestWidgetState extends State<TestWidget> {
  int _selectedIndex = 0;

  PageController _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PageController();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Tab Bar"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[ 
            Expanded(
              flex: 10,
              child: ButtonBar(
                alignment: MainAxisAlignment.center,
                children: <Widget>[
                  FlatButton(
                    splashColor: Colors.blueAccent,
                    color: Colors.blue,
                    onPressed: () {
                      _pageController.animateToPage(0, duration: Duration(milliseconds: 500), curve: Curves.ease);
                    },
                    child: Text("One",),
                  ),
                  FlatButton(
                    splashColor: Colors.blueAccent,
                    color: Colors.blue,
                    onPressed: () {
                      _pageController.animateToPage(1, duration: Duration(milliseconds: 500), curve: Curves.ease);
                    },
                    child: Text("Two",),
                  ),
                  FlatButton(
                    splashColor: Colors.blueAccent,
                    color: Colors.blue,
                    onPressed: () {
                      _pageController.animateToPage(2, duration: Duration(milliseconds: 500), curve: Curves.ease);
                    },
                    child: Text("Three",),
                  )
                ],
              ),
            ),
            Expanded(
              flex: 40,
              child: PageView(
                controller: _pageController,
                children: [
                  Text("Page One"),
                  Text("Page Two"),
                  Text("Page Three")
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

This basically allows you to use any tab bar or buttons you wont to switch page while keeping swipe functionality :-)

like image 164
Anthony Sette Avatar answered Nov 20 '25 14:11

Anthony Sette


I would start with a listView that scrolls horizontally, then build up thous ListTile elements, and make them clickable. If you want to swipe then add on a gesturedetector.

like image 32
key Avatar answered Nov 20 '25 14:11

key



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!