sajjad sohrabi
sajjad sohrabi

Reputation: 306

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

Upvotes: 3

Views: 5830

Answers (4)

Anthony Sette
Anthony Sette

Reputation: 837

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 :-)

Upvotes: 3

null
null

Reputation: 573

Try to use "NestedScrollView" and put the information outside the tabbar in "flexibleSpace"

Upvotes: 0

Sunny
Sunny

Reputation: 3265

You can create a custom list with the same view. Here is the sample code

ListView.builder(
              itemBuilder: (_, count) {
                return Container(
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10.0),
                      color: Colors.green),
                  child: GestureDetector(
                    onTap: (){
                      print("You clicked");
                    },
                    child: Text(
                      list[count],
                      style: TextStyle(color: Colors.white, fontSize: 12),textAlign: TextAlign.center,
                    ),
                  ),
                );
              },
              itemCount: list.length,
              scrollDirection: Axis.horizontal,
            )

Upvotes: 0

key
key

Reputation: 1402

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.

Upvotes: 1

Related Questions