gpt4 book ai didi

flutter 无法使用静态访问访问另一个类的方法

转载 作者:IT王子 更新时间:2023-10-29 07:22:37 25 4
gpt4 key购买 nike

标题基本上解释了我收到的错误方法。抱歉代码量太大,但我基本上只是想在用户触摸“下一个”文本时调用入职以翻转页面。 (onTap:() => _OnboardingState.jumpForward())”我该怎么做?

我尝试过使用流(我对它很不满意),也许解决方案与 bloc 相关。 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 方法使用回调。您正在尝试以静态方式访问非静态方法,此外,您做错了,因为即使该方法是静态的,您也不会访问事件的 State 和 Controller 。

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

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

final PageViewModel viewModel;
final VoidCallback onNextPressed;
(...)

在你的NEXT点击时调用它

 GestureDetector(
onTap:() => onNextPressed(),
child: Text(
"NEXT",
style: lightTheme.primaryTextTheme.button,
)
)

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

OnboardingPage(viewModel:pages[0],onNextPressed: jumpForward)

关于 flutter 无法使用静态访问访问另一个类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54847812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com