在BottomNavigationBar的屏幕之间水平滑动?

时间:2019-07-19 10:08:02

标签: user-interface flutter dart

我希望可以在BottomNavigationBar的屏幕(BottomNavigationBarItems)之间水平滑动。

什么是实现此目标的好方法?

1 个答案:

答案 0 :(得分:2)

以下是示例操作方法:

import 'package:flutter/material.dart';

class SwipeTabBar extends StatefulWidget {
  @override
  _SwipeTabBarState createState() => _SwipeTabBarState();
}

class _SwipeTabBarState extends State<SwipeTabBar> {

  final _pageViewController = PageController();

  int _activePage = 0;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageViewController,
        children: <Widget>[
          Container(color: Colors.red),
          Container(color: Colors.green),
          Container(color: Colors.blue)
        ],
        onPageChanged: (index) {
          setState(() {
            _activePage = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _activePage,
        onTap: (index) {
          _pageViewController.animateToPage(index, duration: Duration(milliseconds: 200), curve: Curves.bounceOut);
        },
        items: [
          BottomNavigationBarItem(icon: Text("R"), activeIcon: Text("Active"), title: Text("Red")),
          BottomNavigationBarItem(icon: Text("G"), activeIcon: Text("Active"), title: Text("Green")),
          BottomNavigationBarItem(icon: Text("B"), activeIcon: Text("Active"), title: Text("Blue")),
        ],
      ),
    );
  }
}