Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable TabView animation on Tab click event?

Tags:

flutter

How can I disable TabView animation when Tab in TabBar clicked ? I added

physics: NeverScrollableScrollPhysics()

for TabView but that doesn't apply for TabBar.

I'm using DefaultTabController.

like image 684
harisk92 Avatar asked Jul 03 '18 16:07

harisk92


People also ask

How do you get rid of the ripple effect from tab bar flutter?

You can also disable the ripple/highlight/splash effect with the code below. Add the Theme with the data of ThemeData where the hightlight and splash color is both transparent. return Scaffold( appBar: PreferredSize( preferredSize: Size. fromHeight(70), child: Theme( data: ThemeData( highlightColor: Colors.

How do I stop the scrolling tab bar in flutter?

To disable Swipe TabBar user can by changing how the page view should respond to user input using the physics property. and we have a NeverScrollableScrollPhysics for that purpose so just change physics to that like this : You can disable swiping from TabBarView()


1 Answers

Based on a very good answer on github about this issue, which achieves something similar to what your looking for (but with a bottomNavigationBar) here I share with you another workaround. It consists of combining a DefaultTabController with a PageView, a PageController and a simple index. Try this out.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Tabs with no animation',
      theme: ThemeData.dark(),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  PageController _pageController;
  final int currentTab = 0;

  @override
  void initState() {
    // TODO: implement initState
    _pageController = PageController(initialPage: currentTab);
    super.initState();
  }

  final List<Tab> myTabs = <Tab>[
    Tab(text: 'One'),
    Tab(
      text: 'Two',
    ),
  ];

  var tabs = [
    TabOne(),
    TabTwo(),
  ];

  @override
  Widget build(BuildContext context) {
    var pageView = PageView(
      controller: _pageController,
      physics: NeverScrollableScrollPhysics(),
      children: tabs,
    );

    return DefaultTabController(
      length: myTabs.length,
      child: Scaffold(
          extendBodyBehindAppBar: true,
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            elevation: 0.0,
            automaticallyImplyLeading: false,
            title: Center(
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(30),
                  color: Colors.grey.shade800,
                ),
                width: 200,
                height: 50,
                child: TabBar(
                  onTap: (index) {
                    _pageController.jumpToPage(index);
                  },
                  unselectedLabelColor: Colors.white,
                  indicator: BoxDecoration(
                      borderRadius: BorderRadius.circular(30),
                      color: Colors.black),
                  tabs: myTabs,
                ),
              ),
            ),
          ),
          body: pageView),
    );
  }
}

class TabOne extends StatelessWidget {
  const TabOne({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(child: Text('Tab one')),
    );
  }
}

class TabTwo extends StatelessWidget {
  const TabTwo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(child: Text('Tab two')),
    );
  }
}

Doing so, you have a something identical to a TabBarView but without animation.

like image 200
Iván Yoed Avatar answered Sep 30 '22 18:09

Iván Yoed