【问题标题】:Changing the TabBar shape in flutter在颤动中更改 TabBar 形状
【发布时间】:2020-09-17 05:18:42
【问题描述】:

我制作了一个底部应用栏,其 shape 属性设置为“CircularNotchedRectangle”,效果非常棒!问题是我正在寻找 TabBar 提供的“滑动以更改页面”功能,但我看不到任何方法可以将其形状更改为 CircularNotchedRectangle。我可以改变它的形状吗?还是我应该尝试制作自己的“滑动以更改页面”功能? 谢谢!

我目前的BottomNavigationBar:

BottomAppBar(
    shape: CircularNotchedRectangle(),
    notchMargin: 2.0,
    child: Stack(
        children: [
          Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                IconButton(
                    icon: Icon(Icons.search),
                    iconSize: 35,
                    color: widget.currentTab == 0 ? Colors.purple[500] : Colors.black,
                    onPressed: (){
                      setState(() {
                        widget.currentTab = 0;
                      });
                    }
                ),
                IconButton(
                    icon: Icon(Icons.account_circle),
                    iconSize: 35,
                    color: widget.currentTab == 1 ? Colors.purple[500] : Colors.black,
                    onPressed: (){
                      setState(() {
                        widget.currentTab = 1;
                      });
                    }
                ),
                SizedBox(width: 40),
                IconButton(
                    icon: Icon(Icons.group),
                    iconSize: 35,
                    color: widget.currentTab == 2 ? Colors.purple[500] : Colors.black,
                    onPressed: (){
                      setState(() {
                        widget.currentTab = 2;
                      });
                    }
                ),
                IconButton(
                    icon: Icon(Icons.chat_bubble),
                    iconSize: 35,
                    color: widget.currentTab == 3 ? Colors.purple[500] : Colors.black,
                    onPressed: (){
                      setState(() {
                        widget.currentTab = 3;
                      });
                    }
                ),
              ]
          )
        ]
    )
);

这是我想通过 TabBar 获得的形状

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    使用当前代码实现 swipe to change page 功能
    可以直接使用PageView
    代码sn-p

    PageController pageController = PageController(
        initialPage: 0,
        keepPage: true,
      );
    
      Widget buildPageView() {
        return PageView(
          controller: pageController,
          onPageChanged: (index) {
            pageChanged(index);
          },
          children: <Widget>[
            Red(),
            Blue(),
            Yellow(),
            Green(),
          ],
        );
      }
    ...
    void bottomTapped(int index) {
        setState(() {
          currentTab = index;
          pageController.animateToPage(index,
              duration: Duration(milliseconds: 500), curve: Curves.ease);
        });
      }  
    ...
    IconButton(
                  icon: Icon(Icons.search),
                  iconSize: 35,
                  color: currentTab == 0 ? Colors.purple[500] : Colors.black,
                  onPressed: () {
                    bottomTapped(0);
                  }),   
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      double width;
      Color primaryColor = Colors.blue;
    
      int currentTab = 0;
    
      PageController pageController = PageController(
        initialPage: 0,
        keepPage: true,
      );
    
      Widget buildPageView() {
        return PageView(
          controller: pageController,
          onPageChanged: (index) {
            pageChanged(index);
          },
          children: <Widget>[
            Red(),
            Blue(),
            Yellow(),
            Green(),
          ],
        );
      }
    
      @override
      void initState() {
        super.initState();
      }
    
      void pageChanged(int index) {
        setState(() {
          currentTab = index;
        });
      }
    
      void bottomTapped(int index) {
        setState(() {
          currentTab = index;
          pageController.animateToPage(index,
              duration: Duration(milliseconds: 500), curve: Curves.ease);
        });
      }
    
      @override
      Widget build(BuildContext context) {
        width = MediaQuery.of(context).size.width;
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: buildPageView(),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
          floatingActionButton: FloatingActionButton(
            backgroundColor: Colors.red,
            child: const Icon(
              Icons.add,
            ),
            onPressed: () {},
          ),
          bottomNavigationBar: BottomAppBar(
              shape: CircularNotchedRectangle(),
              notchMargin: 2.0,
              child: Stack(children: [
                Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
                  IconButton(
                      icon: Icon(Icons.search),
                      iconSize: 35,
                      color: currentTab == 0 ? Colors.purple[500] : Colors.black,
                      onPressed: () {
                        bottomTapped(0);
                      }),
                  IconButton(
                      icon: Icon(Icons.account_circle),
                      iconSize: 35,
                      color: currentTab == 1 ? Colors.purple[500] : Colors.black,
                      onPressed: () {
                        bottomTapped(1);
                      }),
                  SizedBox(width: 40),
                  IconButton(
                      icon: Icon(Icons.group),
                      iconSize: 35,
                      color: currentTab == 2 ? Colors.purple[500] : Colors.black,
                      onPressed: () {
                        bottomTapped(2);
                      }),
                  IconButton(
                      icon: Icon(Icons.chat_bubble),
                      iconSize: 35,
                      color: currentTab == 3 ? Colors.purple[500] : Colors.black,
                      onPressed: () {
                        bottomTapped(3);
                      }),
                ])
              ])),
        );
      }
    }
    
    class Red extends StatefulWidget {
      @override
      _RedState createState() => _RedState();
    }
    
    class _RedState extends State<Red> {
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Colors.purple,
        );
      }
    }
    
    class Blue extends StatefulWidget {
      @override
      _BlueState createState() => _BlueState();
    }
    
    class _BlueState extends State<Blue> {
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Colors.blueAccent,
        );
      }
    }
    
    class Yellow extends StatefulWidget {
      @override
      _YellowState createState() => _YellowState();
    }
    
    class _YellowState extends State<Yellow> {
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Colors.yellowAccent,
        );
      }
    }
    
    class Green extends StatefulWidget {
      @override
      _GreenState createState() => _GreenState();
    }
    
    class _GreenState extends State<Green> {
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Colors.greenAccent,
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      您需要做的就是像这样在 Scaffold 中定义 Fab 位置:

      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      

      【讨论】:

        猜你喜欢
        • 2020-08-27
        • 2019-05-19
        • 2020-10-27
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        • 1970-01-01
        • 2019-02-24
        • 2015-07-08
        相关资源
        最近更新 更多