【问题标题】:I have got no error regarding syntax but the app doesn't work我没有关于语法的错误,但应用程序不起作用
【发布时间】:2021-09-27 23:41:06
【问题描述】:

我正在尝试制作一个标签栏并在每个标签中包含一些页面。我没有收到错误,模拟器运行应用程序,但是,它只显示标签栏及其动画,而不是我需要的其他页面。

当我在页面的按钮上设置 Widget buildPages 时出现了一些错误,因此我将 _MyAppState 类下的小部件代码关联起来。

我不确定代码的哪一部分错位了,或者是否有任何额外的功能,例如 var index;


import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:gg/tabBarScreens/Following.dart';
import 'package:gg/tabBarScreens/Liked%20Albums.dart';
import 'package:gg/tabBarScreens/profileTabBar.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  //State class
  @override
  _MyAppState createState() => _MyAppState();
}


class _MyAppState extends State<MyApp> {
  int page = 0;
  
  Widget buildPages(){
    var index;
    switch (index){
      case 2:
        return profileTabBar();
      case 1:
        return likedAlbums();
      case 0:
      default: 0;
      return Following();
    }
  }

  GlobalKey<CurvedNavigationBarState> _bottomNavigationKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return MaterialApp( home: Scaffold(
        bottomNavigationBar: CurvedNavigationBar(
          key: _bottomNavigationKey,
          items: <Widget>[
            Icon(Icons.add, size: 30),
            Icon(Icons.list, size: 30),
            Icon(Icons.compare_arrows, size: 30),
          ],
          onTap: (index) {
            setState(() {
              page = index;
            });
          },
        ),
        body: buildPages(),


    ),

    );
  }
}

【问题讨论】:

    标签: flutter tabbar


    【解决方案1】:

    在您的 buildPages 函数中删除“var index”并在 switch statement 中将您的 variable_expression 替换为页面

      Widget buildPages() {
        switch (page) {
          case 2:
            return profileTabBar();
          case 1:
            return likedAlbums();
          case 0:
            return Following();
          default:
            return Following();
        }
      }
    

    一个基本工作示例的完整代码,您甚至可以粘贴 here 并查看它的实际效果

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      //State class
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      int page = 0;
    
      Widget buildPages() {
        switch (page) {
          case 2:
            return Container(color: Colors.red);
          case 1:
            return Container(color: Colors.blue);
          case 0:
            return Container(color: Colors.green);
    
          default:
            return Container(color: Colors.green);
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            bottomNavigationBar: BottomNavigationBar(
              items: <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  label: "",
                  icon: Icon(Icons.add, size: 30),
                ),
                BottomNavigationBarItem(
                  label: "",
                  icon: Icon(Icons.list, size: 30),
                ),
                BottomNavigationBarItem(
                  label: "",
                  icon: Icon(Icons.compare_arrows, size: 30),
                ),
              ],
              onTap: (index) {
                setState(() {
                  page = index;
                });
              },
            ),
            body: buildPages(),
          ),
        );
      }
    }
    

    点击第三个标签

    第一个标签被点击

    【讨论】:

    • 感谢您的付出。我已经尝试了您更改的代码,它将所有这些代码放在一个恰好是最后一个选项卡中。我在不同的标签中尝试这些页面!例如;在第一个标签中关注,在第二个标签中关注关注者......
    • 我更新了答案。如果这不是您想要的,请提供更多详细信息和更多解释示例。
    • 我需要 CurvedNavigationBar 在不同的选项卡中加载不同的页面。例如,当点击标签栏上的最后一个图标时,显示我制作和导入的关注或关注页面。谢谢
    • 根据 Scaffold 的 body 参数加载不同的页面,所以没关系
    猜你喜欢
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2012-04-29
    相关资源
    最近更新 更多