Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振无法使用静态访问访问另一个类的方法_Flutter - Fatal编程技术网

Flutter 颤振无法使用静态访问访问另一个类的方法

Flutter 颤振无法使用静态访问访问另一个类的方法,flutter,Flutter,标题基本上解释了我收到的错误方法。很抱歉代码太多,但我基本上只是想打电话给Onboard,当用户触摸下一个文本时翻页。onTap:=>\u OnboardingState.jumpForward 我该怎么做 我曾经尝试过使用streams,但我对它的使用非常糟糕,也许有一个解决方案是与集团相关的。idk。我相信这是一个很容易解决的问题 import 'package:flutter/material.dart'; import '../../styling/canvas.dart'; impor

标题基本上解释了我收到的错误方法。很抱歉代码太多,但我基本上只是想打电话给Onboard,当用户触摸下一个文本时翻页。onTap:=>\u OnboardingState.jumpForward 我该怎么做

我曾经尝试过使用streams,但我对它的使用非常糟糕,也许有一个解决方案是与集团相关的。idk。我相信这是一个很容易解决的问题

import 'package:flutter/material.dart';
import '../../styling/canvas.dart';
import '../../styling/theme.dart';
import 'dart:async';

class Onboarding extends StatefulWidget {
  @override
  _OnboardingState createState() => _OnboardingState();
}

class _OnboardingState extends State<Onboarding> {
    PageController onboardingController;
    void jumpForward() async {
    onboardingController.nextPage(
      curve: Curves.easeIn,
      duration: Duration(milliseconds: 200)
    );
    }

  @override
  Widget build(BuildContext context) {
    return PageView(
      controller: onboardingController,
      pageSnapping: true,
      children: <Widget>[
        OnboardingPage(viewModel:pages[0]),
        OnboardingPage(viewModel:pages[1]),
        OnboardingPage(viewModel:pages[2]),
      ]
    );
  }

  // jumpTo() {
  //   hello.jump
  // }
}

class PageViewModel{
  PageViewModel({
    this.title,
    this.description,
    this.id,
  });

  final String title;
  final String description;
  final int id;
}

final pages = [
  PageViewModel(
    title: "Trade smart",
    description:
        "Filter stocks by metrics like short interest and short interest change.",
    id: 1,
  ),
  PageViewModel(
    title: "Connect",
    description:
        "Filter stocks by metrics like short interest and short interest change.",
    id: 2,
  ),
  PageViewModel(
    title: "Get an edge",
    description:
        "View legal insider trades filed with the SEC, made by top executives.",
    id: 3,
  ),
];

class OnboardingPage extends StatelessWidget {
  OnboardingPage({this.viewModel});

  final PageViewModel viewModel;
  @override
  Widget build(BuildContext context) {
    double _height = MediaQuery.of(context).size.height;
    double _width = MediaQuery.of(context).size.width;
    final double _circleHeight = .022 * _height;
    return Scaffold(
      body: Container(
        color: lightTheme.backgroundColor,
        height: _height,
        width: _width,
        child: Padding(
          padding: EdgeInsets.fromLTRB(0.0, .1 * _height, 0.0, .1 * _height),
          child: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.fromLTRB(
                    .044 * _width, 0.0, .044 * _width, .022 * _height),
                child: Container(
                  //illustration
                  decoration:
                      returnCanvasStyle(), //todo: change color to theme bloc
                  width: _width,
                  height: .433 * _height,
                ),
              ),
              Padding(
                padding:
                    EdgeInsets.fromLTRB(.044 * _width, 0.0, .044 * _width, 0.0),
                child: Container(
                  decoration: returnCanvasStyle(),
                  width: _width,
                  height: .344 * _height, //todo: change height
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.fromLTRB(0.022 * _width,
                            0.022 * _height, 0.022 * _width, 0.0),
                        child: Text(
                          viewModel.title,
                          style: lightTheme.primaryTextTheme.display1,
                          textAlign: TextAlign.center,
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.fromLTRB(0.044 * _width,
                            0.022 * _height, 0.044 * _width, 0.033 * _height),
                        child: Text(
                          viewModel.description,
                          style: lightTheme.primaryTextTheme.body1,
                          softWrap: true,
                          textAlign: TextAlign.center,
                        ),
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: <Widget>[
                          Padding(
                            padding: EdgeInsets.fromLTRB(
                                0.0, 0.0, .066 * _width, 0.0),
                            child: Container(
                              width: _circleHeight,
                              height: _circleHeight,
                              //color: Colors.red,
                              decoration: BoxDecoration(
                                  border: Border.all(
                                    color: lightTheme.primaryColor,
                                    width: .0055 * _height,
                                  ),
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(_circleHeight)),
                                  color: lightTheme.primaryColor),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.fromLTRB(
                                0.0, 0.0, .066 * _width, 0.0),
                            child: Container(
                              width: _circleHeight,
                              height: _circleHeight,
                              //color: Colors.red,
                              decoration: BoxDecoration(
                                  border: Border.all(
                                    color: lightTheme.primaryColor,
                                    width: .0055 * _height,
                                  ),
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(_circleHeight)),
                                  color: Colors.transparent),
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.fromLTRB(
                                0.0, 0.0, .0944 * _width, 0.0),
                            child: Container(
                              width: _circleHeight,
                              height: _circleHeight,
                              //color: Colors.red,
                              decoration: BoxDecoration(
                                  border: Border.all(
                                    color: lightTheme.primaryColor,
                                    width: .0055 * _height,
                                  ),
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(_circleHeight)),
                                  color: Colors.transparent),
                            ),
                          ),
                          Padding(
                              padding: EdgeInsets.fromLTRB(
                                  0.0, 0.0, .119 * _width, 0.0),
                              child: GestureDetector(
                                onTap:() => _OnboardingState.jumpForward(),
                                child: Text(
                                "NEXT",
                                style: lightTheme.primaryTextTheme.button,
                              )),)
                        ],
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

如果您想这样做,只需对State方法使用回调。您试图以静态方式访问非静态方法,而且,您做得不对,因为即使该方法是静态的,您也不会访问活动状态和控制器

因此,通过使用回调,您希望将其传递给类的构造函数

class OnboardingPage extends StatelessWidget {
  OnboardingPage({this.viewModel, this.onNextPressed});

  final PageViewModel viewModel;
  final VoidCallback onNextPressed;
  (...)
下一次轻触时呼叫它

然后在构建方法中将OnboardingPage添加到小部件树时传递引用


感谢您提供的提示,我如何修改jumpForward方法以修改活动状态和控制器?通过调用onboardingController.nextPage,您已经拥有了这种方法。。。应该足够了。啊,我明白了,我已经尝试了你的解决方案,它看起来像是在调用方法,但它告诉我“nextpage”是在null上调用的。我不知道它为什么会这样做,因为您没有实例化PageController,只需将它添加到onboardingController=PageController;刚刚修复了它,我所做的是实现了onNextPressed:jumpForward而不是onNextPressed:jumpForward,它在构建页面视图之前调用了这个函数。谢谢您抽出时间,好先生
 GestureDetector(
                onTap:() => onNextPressed(),
                   child: Text(
                       "NEXT",
                        style: lightTheme.primaryTextTheme.button,
                         )
                 )
OnboardingPage(viewModel:pages[0],onNextPressed: jumpForward)