【问题标题】:How to switch class when a tab is clicked using google_nav_bar Flutter如何在使用 google_nav_bar Flutter 单击选项卡时切换类
【发布时间】:2021-11-16 08:52:29
【问题描述】:

我正在使用来自 pub 的 google_nav_barline_icons

我有 3 个名为 Home()、Likes()、Profile() 的类。我想在单击底部导航栏选项卡时切换类,我已经列出了类,但是我不确定单击选项卡时如何更改类。

这是我目前的代码:

class MainScreen extends StatefulWidget {
      @override
      _MainScreenState createState() => _MainScreenState();
    }
    
    class _MainScreenState extends State<MainScreen> {
      int _page = 0;
    
      final screens = [
        Home(),
        Likes(),
        Profile()
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.grey[100],
          bottomNavigationBar: GNav(
            rippleColor: Colors.grey[300],
            hoverColor: Colors.grey[100],
            gap: 8,
            activeColor: Colors.black,
            iconSize: 24,
            padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
            duration: Duration(milliseconds: 400),
            tabBackgroundColor: Colors.grey[100],
            color: Colors.black,
            tabs: [
              GButton(
                icon: LineIcons.home,
                text: 'Home',
              ),
              GButton(
                icon: LineIcons.heart,
                text: 'Likes',
              ),
              GButton(
                icon: LineIcons.user,
                text: 'Profile',
              ),
            ],
            selectedIndex: _page,
            onTabChange: (index) {
              setState(() {
                _page = index;
              });
            },
          ),
          body: /*new Container(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            child: _page == 0
                ? Home()
                : _page == 1
                    ? Likes()
                            : Profile(),
          ),*/
          Center(
            child: screens.elementAt(_page),
          ),
        );
      }
    }

我想在单击第二个选项卡时将底部导航栏导航到 Likes() 类,并在单击第三个导航栏时导航到 Profile() 类..

【问题讨论】:

  • 小部件中的GNav 是什么。
  • 它来自 google_nav_bar 一种底部导航栏

标签: flutter dart flutter-layout


【解决方案1】:

一切正常。将 Tab 小部件替换为不同的小部件,例如 Home(), Likes(), Profile().

现在会是这样的


class Tab extends StatelessWidget {
  final int tab;
  const Tab({
    Key? key,
    required this.tab,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("$tab"),
    );
  }
}

class MainScreen extends StatefulWidget {
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  int _page = 0;

  final screens = const [Tab(tab: 1), Tab(tab: 2), Tab(tab: 3)];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],
      bottomNavigationBar: GNav(
        rippleColor: Colors.grey[300]!,
        hoverColor: Colors.grey[100]!,
        gap: 8,
        activeColor: Colors.black,
        iconSize: 24,
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
        duration: const Duration(milliseconds: 400),
        tabBackgroundColor: Colors.grey[100]!,
        color: Colors.black,
        tabs: const [
          GButton(
            icon: LineIcons.home,
            text: 'Home',
          ),
          GButton(
            icon: LineIcons.heart,
            text: 'Likes',
          ),
          GButton(
            icon: LineIcons.user,
            text: 'Profile',
          ),
        ],
        selectedIndex: _page,
        onTabChange: (index) {
          setState(() {
            _page = index;
          });
        },
      ),
      body: Center(
        child: screens[_page],
      ),
    );
  }
}

你的情况能解决吗?

【讨论】:

    【解决方案2】:

    试试下面的代码希望它对你有帮助。只需在您的课程中添加您的小部件

    您的BottomBar 小部件。

    class Example extends StatefulWidget {
      @override
      _ExampleState createState() => _ExampleState();
    }
    
    class _ExampleState extends State<Example> {
      int _selectedIndex = 0;
    
      static const List<Widget> _widgetOptions = <Widget>[
        Home(),
        Likes(),
        Profile(),
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            elevation: 20,
            title: const Text('GoogleNavBar'),
          ),
          body: Center(
            child: _widgetOptions.elementAt(_selectedIndex),
          ),
          bottomNavigationBar: Container(
            decoration: BoxDecoration(
              color: Colors.white,
              boxShadow: [
                BoxShadow(
                  blurRadius: 20,
                  color: Colors.black.withOpacity(.1),
                )
              ],
            ),
            child: SafeArea(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 8),
                child: GNav(
                  rippleColor: Colors.grey[300]!,
                  hoverColor: Colors.grey[100]!,
                  gap: 8,
                  activeColor: Colors.black,
                  iconSize: 24,
                  padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
                  duration: Duration(milliseconds: 400),
                  tabBackgroundColor: Colors.grey[100]!,
                  color: Colors.black,
                  tabs: [
                    GButton(
                      icon: LineIcons.home,
                      text: 'Home',
                    ),
                    GButton(
                      icon: LineIcons.heart,
                      text: 'Likes',
                    ),
                    GButton(
                      icon: LineIcons.user,
                      text: 'Profile',
                    ),
                  ],
                  selectedIndex: _selectedIndex,
                  onTabChange: (index) {
                    setState(() {
                      _selectedIndex = index;
                    });
                  },
                ),
              ),
            ),
          ),
        );
      }
    }
    

    您的Home 班级

    class Home extends StatelessWidget {
      const Home({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Text(
          'Home',
        );
      }
    
    }
    

    您的Likes 班级

    class Likes extends StatelessWidget {
      const Likes({
        Key? key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Text(
          'Likes',
        );
      }
    }
    

    您的Profile 班级

    class Profile extends StatelessWidget {
      const Profile({
        Key? key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Text(
          'Profile',
        );
      }
    }
    

    【讨论】:

      【解决方案3】:

      为了更好地切换使用 IndexedStack:

      class MainScreen extends StatefulWidget {
        @override
        _MainScreenState createState() => _MainScreenState();
      }
      
      class _MainScreenState extends State<MainScreen> {
        int _page = 0;
      
        final screens = [
          Home(),
          Likes(),
          Profile()
        ];
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            backgroundColor: Colors.grey[100],
            bottomNavigationBar: GNav(
              rippleColor: Colors.grey[300],
              hoverColor: Colors.grey[100],
              gap: 8,
              activeColor: Colors.black,
              iconSize: 24,
              padding: EdgeInsets.symmetric(horizontal: 20, vertical: 12),
              duration: Duration(milliseconds: 400),
              tabBackgroundColor: Colors.grey[100],
              color: Colors.black,
              tabs: [
                GButton(
                  icon: LineIcons.home,
                  text: 'Home',
                ),
                GButton(
                  icon: LineIcons.heart,
                  text: 'Likes',
                ),
                GButton(
                  icon: LineIcons.user,
                  text: 'Profile',
                ),
              ],
              selectedIndex: _page,
              onTabChange: (index) {
                setState(() {
                  _page = index;
                });
              },
            ),
            body: IndexedStack(
              index: _page,
              children: screens,
            ),
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-08-27
        • 2014-08-31
        • 1970-01-01
        • 2016-03-13
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-29
        相关资源
        最近更新 更多