【问题标题】:Flutter switch between pages loaded using BottomNavigationBarFlutter 在使用 BottomNavigationBar 加载的页面之间切换
【发布时间】:2020-06-30 08:46:03
【问题描述】:

我通过这个链接https://api.flutter.dev/flutter/material/BottomNavigationBar-class.html创建了带有BottomNavigationBar的Flutter主页

它工作正常。但我想知道如何从第三页转到第一页。我的意思是,在第三页内,我有一个按钮,当用户点击它时,我必须将用户重定向到第一页。 (我不是在说底部导航栏上的按钮)

有可能吗?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    只需更改_selectedIndex 并调用setState 方法,如下所示:

    class TestWidget extends StatefulWidget {
      @override
      _TestWidgetState createState() => _TestWidgetState();
    }
    
    class _TestWidgetState extends State<TestWidget>
        with SingleTickerProviderStateMixin {
      int _selectedIndex = 0;
      static const TextStyle optionStyle =
          TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
    
      List<Widget> _widgetOptions;
    
      @override
      void initState() {
        super.initState();
        _widgetOptions = <Widget>[
          Text(
            'Index 0: Home',
            style: optionStyle,
          ),
          Text(
            'Index 1: Business',
            style: optionStyle,
          ),
          TestBusinessPage<Object>((data,index){
            _onItemTapped(index);
          }),
        ];
      }
    
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('BottomNavigationBar Sample'),
          ),
          body: Center(
            child: _widgetOptions.elementAt(_selectedIndex),
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text('Home'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.business),
                title: Text('Business'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.school),
                title: Text('School'),
              ),
            ],
            currentIndex: _selectedIndex,
            selectedItemColor: Colors.amber[800],
            onTap: _onItemTapped,
          ),
        );
      }
    }
    
    /// T is business data,index is target index
    typedef void OnButtonClicked<T>(T data, int index);
    
    class TestBusinessPage<T> extends StatelessWidget {
      final OnButtonClicked buttonClicked;
    
      static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
    
      TestBusinessPage(this.buttonClicked);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: RaisedButton(
            onPressed: () {
              buttonClicked(null,0);
            },
            child: Text(
              'Index 2: School',
              style: optionStyle,
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 这不是我的问题。我想从不同的页面切换到第一页。 _selectedIndex 在底部导航栏中。我的问题,现在我在“业务”页面,想从“业务”页面的按钮返回“主页”页面
    • @user2609021 在这种情况下,您可以创建一个回调并将其传递给业务页面,然后在单击按钮时调用回调。我已经更新了答案
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 2021-07-27
    • 2019-03-17
    • 2016-01-17
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多