为什么我的Flutter类导航小部件返回Null?

huangapple go评论74阅读模式
英文:

Why is my Flutter class navigation widget returning Null?

问题

我正在构建一个应用程序,其中包含导航到新屏幕的瓦片。

我的“约会瓦片”是我的社交瓦片类的一个实例,我试图将导航作为参数输入。但是根据我的打印输出,我似乎只能返回 Null。

我觉得问题出在构造函数上,但我无法解决它。

我的意思是问题可能是我几乎不知道自己在做什么,但这就是学习的过程。

请看下面的代码,这是我的问题所在。

这是我试图调用它的实例。

class SocialTab extends StatelessWidget {
  const SocialTab({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      child: Scaffold(
        body: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          decoration: const BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.bottomRight,
                end: Alignment.topLeft,
                stops: [0.25, 1],
                colors: [Color(0xff1A83E8), Color(0xffA111FF)]),
          ),
          child: Container(
            alignment: Alignment.centerLeft,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      socialTile(
                          tileTitle: 'messenger',
                          inputIcon: Icon(
                          )),
                      socialTile(
                          tileTitle: 'Dating',
                          inputIcon: Icon(
                            Icons.heart_broken_rounded,
                            size: 140,
                          ),
                          navigationToRoute: (context) => DatingPage(),),
                    ]),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

这是类本身。

class socialTile extends GeneralUITile {
  const socialTile({
    Key? key,
    this.tileTitle = '',
    this.inputIcon,
    Widget Function(BuildContext)? navigationToRoute,
  }) : super(key: key);

  final String tileTitle;
  final Icon? inputIcon;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GestureDetector(
        onTap: () {
          print('$navigationToRoute Tap registered');
          if (navigationToRoute != null) {
            Navigator.push(
              context,
              MaterialPageRoute(builder: navigationToRoute!),
            );
          }
        },
        child: Container(
          height: 175,
          width: 175,
          color: Colors.purpleAccent,
          alignment: Alignment.center,
          child: Column(
            children: [
              if (inputIcon != null) ...[inputIcon!],
              Text(
                '$tileTitle',
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

请帮助我,我已经准备好蜷缩成一团,只是开始使用Bard AI。

英文:

I am building an App that has tiles that navigate to a new screen.

My 'Dating Tile' is an instance of my social tile class and I am tryin to enter Navigation as an Argument. I only seem to be able to return Null as per my printing feature.

I feel like the problem is with the constructor but I can't work it out.

I mean the problem could be I barley know what I'm doing but hey that's learning for you.

See below for the code to my problem.

This is the instance I'm trying to call it in.


class SocialTab extends StatelessWidget {
  const SocialTab({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      child: Scaffold(
        body: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          decoration: const BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.bottomRight,
                end: Alignment.topLeft,
                stops: [0.25, 1],
                colors: [Color(0xff1A83E8), Color(0xffA111FF)]),
          ),
          child: Container(
            alignment: Alignment.centerLeft,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      socialTile(
                          tileTitle: 'messenger',
                          inputIcon: Icon(
                          )),
                      socialTile(
                          tileTitle: 'Dating',
                          inputIcon: Icon(
                            Icons.heart_broken_rounded,
                            size: 140,
                          ),
                          navigationToRoute: (context) => DatingPage(),),
                    ]),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

This is the Class itself.


class socialTile extends GeneralUITile {
  const socialTile({
    Key? key,
    this.tileTitle = '',
    this.inputIcon,
    Widget Function(BuildContext)? navigationToRoute,
  }) : super(key: key);

  final String tileTitle;
  final Icon? inputIcon;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GestureDetector(
        onTap: () {
          print(' $navigationToRoute Tap registered');
          if (navigationToRoute != null) {
            Navigator.push(
              context,
              MaterialPageRoute(builder: navigationToRoute!),
            );
          }
        },
        child: Container(
          height: 175,
          width: 175,
          color: Colors.purpleAccent,
          alignment: Alignment.center,
          child: Column(
            children: [
              if (inputIcon != null) ...[inputIcon!],
              Text(
                '$tileTitle',
                style: TextStyle(
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Please help, I'm ready to curl up in a ball and just start using Bard AI.

答案1

得分: 0

我相信你正在尝试为每个社交瓷砖传递一个路由,你可以像这样传递下一个路由的小部件:

socialTile(
   tileTitle: 'Dating',
   inputIcon: Icon(
       Icons.heart_broken_rounded,
       size: 140,
   ),
   navigationToWidget: DatingPage(),
),

在你的社交瓷砖文件中,

onTap: () {
       print('$navigationToRoute Tap registered');
         if (navigationToRoute != null) {
           Navigator.push(
            context,
           MaterialPageRoute(builder:(context)=> navigationToWidget!),
        );
      }
 },

你的构造函数应该如下所示:

const socialTile({
    Key? key,
    this.tileTitle = '',
    this.inputIcon,
    this.navigationToWidget,
  }) : super(key: key);

  final String tileTitle;
  final Icon? inputIcon;
  final Widget? navigationToWidget;
英文:

I believe you are trying to pass a route for every social tile you have, you can pass the widget for the next route, like:

socialTile(
   tileTitle: 'Dating',
   inputIcon: Icon(
       Icons.heart_broken_rounded,
       size: 140,
   ),
   navigationToWidget: DatingPage(),),

In your social tile file,

onTap: () {
       print(' $navigationToRoute Tap registered');
         if (navigationToRoute != null) {
           Navigator.push(
            context,
           MaterialPageRoute(builder:(context)=> navigationToWidget!),
        );
      }
 },

Your constructor should look like:

const socialTile({
    Key? key,
    this.tileTitle = '',
    this.inputIcon,
    this.navigationToWidget,
  }) : super(key: key);

  final String tileTitle;
  final Icon? inputIcon;
  final Widget? navigationToWidget;

huangapple
  • 本文由 发表于 2023年6月1日 17:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定