Flutter 我的颤振代码返回“;方法';列表项';isn';t为类型'_FitnessAppState'&引用;
这是主飞镖Flutter 我的颤振代码返回“;方法';列表项';isn';t为类型'_FitnessAppState'&引用;,flutter,dart,Flutter,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(
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(
debugShowCheckedModeBanner: false,
home: FitnessApp(),
);
}
}
class FitnessApp extends StatefulWidget {
@override
_FitnessAppState createState() => _FitnessAppState();
}
class _FitnessAppState extends State<FitnessApp> {
String img_Header = "https://unsplash.com/photos/fG0p4Qh_aWI";
List<String> trainingImage = [
"https://unsplash.com/photos/Jc-UCKGhIlU"
"https://unsplash.com/photos/WvDYdXDzkhs"
"https://unsplash.com/photos/Lx_GDv7VA9M"
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 300.0,
pinned: false,
floating: false,
title: Text(
"FitApp",
style: TextStyle(color: Colors.white),
),
centerTitle: true,
backgroundColor: Colors.black,
toolbarHeight: 80.0,
leading: IconButton(
onPressed: () {},
icon: Icon(Icons.menu),
),
flexibleSpace: FlexibleSpaceBar(
background: Image.network(
img_Header,
fit: BoxFit.cover,
color: Color(0xaa212121),
colorBlendMode: BlendMode.darken,
),
),
bottom: PreferredSize(
child: Padding(
padding: const EdgeInsets.only(
bottom: 24.0, left: 12.0, right: 12.0),
child: TextField(
decoration: InputDecoration(
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(30.0),
borderSide: BorderSide.none,
),
contentPadding:
EdgeInsets.symmetric(vertical: 0.0, horizontal: 24.0),
hintText: "Search for the training program...",
prefixIcon: Icon(
Icons.search,
color: Colors.black,
),
),
),
),
preferredSize: Size.fromHeight(80.0),
),
actions: [
IconButton(
onPressed: () {},
icon: Icon(Icons.settings),
),
],
)
];
},
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(top: 20.0, right: 20.0, left: 20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Top Trends",
style: TextStyle(
color: Colors.black,
fontSize: 24.0,
fontWeight: FontWeight.w800),
),
SizedBox(
height: 20.0,
),
listItem(trainingImage[0], "Full Body Program", 53, 30),
listItem(trainingImage[1], "Yoga Training", 30, 10),
listItem(trainingImage[2], "Full Body ", 53, 30),
listItem(trainingImage[3], "Full Body Program", 53, 30),
],
),
),
),
),
);
}
}
可能是坏了的链环什么的。未为类fitnessApp定义列表项。尝试创建一个扩展statefullWidget的类并用对象确定变量,而不是创建一个小部件
class ListItem extends StatefulWidget {
final bg;
final title;
final exercises;
final time;
ListItem({this.bg, this.title, this.exercises, this.time});
@override
_ListItem State createState() => _ListItem State();
}
class _ListItem State extends State<ListItem > {
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,,
margin: EdgeInsets.only(bottom: 10.0),
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(bg),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(Color(0x60212121), BlendMode.darken),
),
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(
color: Colors.white,
fontSize: 24.0,
fontWeight: FontWeight.w800,
),
),
SizedBox(height: 10.0,
),
Text(
"${exercices} Exercices",
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.w400,
),
),
SizedBox(height: 4.0,
),
Text(
"${time} Minutes Per Day",
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.w400,
),
),
],
),
),
);
}
}
class ListItem扩展了StatefulWidget{
最终背景;
最终标题;
期末练习;
最后一次;
ListItem({this.bg,this.title,this.exercises,this.time});
@凌驾
_ListItem状态createState()=>\u ListItem状态();
}
类_ListItem状态扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
宽度:双精度。无穷大,,
边距:仅限边缘组(底部:10.0),
装饰:盒子装饰(
图像:装饰图像(
图像:网络图像(bg),
适合:BoxFit.cover,
colorFilter:colorFilter.mode(颜色(0x60212121),BlendMode.darken),
),
边界半径:边界半径。圆形(10.0),
),
孩子:填充(
填充:常数边集全部(10.0),
子:列(
mainAxisAlignment:mainAxisAlignment.start,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
正文(
标题
样式:TextStyle(
颜色:颜色,白色,
字体大小:24.0,
fontWeight:fontWeight.w800,
),
),
尺寸箱(高度:10.0,
),
正文(
“${exercies}exercies”,
样式:TextStyle(
颜色:颜色,白色,
字体大小:18.0,
fontWeight:fontWeight.w400,
),
),
尺寸箱(高度:4.0,
),
正文(
“${time}分钟/天”,
样式:TextStyle(
颜色:颜色,白色,
字体大小:18.0,
fontWeight:fontWeight.w400,
),
),
],
),
),
);
}
}
尝试执行此操作,并将此文件导入到健身应用程序文件中,然后在那里调用ListItem,您将看到要填写的变量。1。再次检查trainingImage中的链接是否为图像链接。2.您可以将listitem添加到main.dart页面,也可以导入它
class ListItem extends StatefulWidget {
final bg;
final title;
final exercises;
final time;
ListItem({this.bg, this.title, this.exercises, this.time});
@override
_ListItem State createState() => _ListItem State();
}
class _ListItem State extends State<ListItem > {
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,,
margin: EdgeInsets.only(bottom: 10.0),
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(bg),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(Color(0x60212121), BlendMode.darken),
),
borderRadius: BorderRadius.circular(10.0),
),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: TextStyle(
color: Colors.white,
fontSize: 24.0,
fontWeight: FontWeight.w800,
),
),
SizedBox(height: 10.0,
),
Text(
"${exercices} Exercices",
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.w400,
),
),
SizedBox(height: 4.0,
),
Text(
"${time} Minutes Per Day",
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
fontWeight: FontWeight.w400,
),
),
],
),
),
);
}
}