【问题标题】:How to change Scaffold body based on which navigation bar button is clicked in flutter?如何根据在颤动中单击哪个导航栏按钮来更改脚手架主体?
【发布时间】:2020-12-12 11:11:44
【问题描述】:

我是 Flutter 的新手,我想以正确的方式进行操作,问题是我有一个底部导航栏包 i Curved_navigation_bar 它有很棒的外观和动画,这个导航栏应该改变脚手架的主体小部件并在每次单击按钮时显示一个新小部件,我想要实现的是每次单击导航栏的按钮时执行以下操作:

  • 清除Scaffold的正文
  • 加载相关小部件

我希望这是在颤振导航中遵循的正确方法(更改屏幕或视图),如果这是错误的,请告诉我

    class _SituationState extends State<ScreenSituation>{
         int _page = 0;
         GlobalKey _bottomNavigationKey = GlobalKey();
         @override
    Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        //if list button is clicked preveiw widget_client
        //if people button is clicked preveiw widget_people
        //if launch button is clicked preveiw launch
      ),
      bottomNavigationBar: CurvedNavigationBar(
        key: _bottomNavigationKey,
        index: 0,
        height: 75.0,
        items: <Widget>[
          Icon(Icons.list, size: 30),
          Icon(Icons.people, size: 30),
          Icon(Icons.launch, size: 30),
        ],
        color: Colors.white,
        buttonBackgroundColor: Colors.white,
        backgroundColor: Colors.blueAccent,
        animationCurve: Curves.easeInOut,
        animationDuration: Duration(milliseconds: 300),
        onTap: (index) {
          setState(() {
            _page = index;
          });
        },
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-layout flutter-scaffold


    【解决方案1】:

    获取您的代码示例。

    import 'package:curved_navigation_bar/curved_navigation_bar.dart';
    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,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          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> {
      int _page = 0;
      GlobalKey _bottomNavigationKey = GlobalKey();
    
      Widget bodyFunction() {
        switch (_page) {
          case 0:
            return Container(color: Colors.red);
            break;
          case 1:
            return Container(color: Colors.blue);
            break;
          case 2:
            return Container(color: Colors.orange);
            break;
          default:
            return Container(color: Colors.white);
            break;
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: bodyFunction(),
          bottomNavigationBar: CurvedNavigationBar(
            key: _bottomNavigationKey,
            index: 0,
            height: 75.0,
            items: <Widget>[
              Icon(Icons.list, size: 30),
              Icon(Icons.people, size: 30),
              Icon(Icons.launch, size: 30),
            ],
            color: Colors.white,
            buttonBackgroundColor: Colors.white,
            backgroundColor: Colors.blueAccent,
            animationCurve: Curves.easeInOut,
            animationDuration: Duration(milliseconds: 300),
            onTap: (index) {
              setState(() {
                _page = index;
              });
            },
          ),
        );
      }
    }
    

    【讨论】:

    • 请再问一个问题,这是在颤振中使用底部导航栏切换屏幕/视图的正确方法吗?
    • 最好为每个主体创建类并将它们分别放在文件中,以避免在一个文件中过多。您还可以使用类创建一个数组,这样您就不需要使用开关盒
    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多