Android 吸气剂';背景色';被调用为空
我试图将背景颜色从一个类中的一个容器扩展到另一个单独类中的另一个容器。但是在我运行之后,我得到了以下错误。我已经尝试了所有我知道我正在做的事情,但现在我感到困惑。我甚至不知道我做错了什么有什么帮助吗 这就是我得到的错误 对null调用了getter“backColor”。 收件人:空 尝试呼叫:背景色 这是头等舱Android 吸气剂';背景色';被调用为空,android,flutter,dart,flutter-layout,Android,Flutter,Dart,Flutter Layout,我试图将背景颜色从一个类中的一个容器扩展到另一个单独类中的另一个容器。但是在我运行之后,我得到了以下错误。我已经尝试了所有我知道我正在做的事情,但现在我感到困惑。我甚至不知道我做错了什么有什么帮助吗 这就是我得到的错误 对null调用了getter“backColor”。 收件人:空 尝试呼叫:背景色 这是头等舱 class FoodDetails extends StatefulWidget { @override _FoodDetailsState createState() =&g
class FoodDetails extends StatefulWidget {
@override
_FoodDetailsState createState() => _FoodDetailsState();
}
class _FoodDetailsState extends State<FoodDetails> {
static get rate => double;
static get color => Color; //This is what I am trying to get.
final List<Map<dynamic, dynamic>> demoFoods = [
{
'name': 'Burger n Fries +1',
'price': '20.00',
rate: 3.5,
'clients': '250',
'image': 'assets/images/coke.png',
color: Colors.amber,
},
{
'name': 'Peperoni Pizza',
'price': '45.00',
rate: 3.5,
'clients': '400',
'image': 'assets/images/pizza2.png',
color: Colors.orange,
},
{
'name': 'Regular Choco-Sundae',
'price': '25.00',
rate: 4.8,
'clients': '300',
'image': 'assets/images/regularsundae.png',
color: Colors.purpleAccent,
},
{
'name': 'Chicken Nuggets',
'price': '40.00',
rate: 5.0,
'clients': '200',
'image': 'assets/images/nugget1.png',
color: Colors.orangeAccent,
},
{
'name': 'Egg-Tart Pie',
'price': '35.05',
rate: 3.6,
'clients': '10',
'image': 'assets/images/eggtartpie.png',
color: Colors.yellowAccent,
}
];
@override
Widget build(BuildContext context) {
return Container(
height: 200.0,
child: PageView.builder(
itemCount: this.demoFoods.length,
itemBuilder: (context, index) {
Map<dynamic, dynamic> product = this.demoFoods[index];
return GestureDetector(
onTap: () {
Navigator.pushNamed(
context,
'details',
arguments: {
'product': product,
'index': index,
},
);
},
child: Hero(
tag: 'detail_food$index',
child: PopularCard(
width: SizeConfig.screenWidth / 2 - 30,
primaryColor: kPrimaryColor,
productName: product['name'],
productPrice: product['price'],
productUrl: product['image'],
productClients: product['clients'],
productRate: product[rate],
backColor: product[color], //I call in my code
),
),
);
}),
);
}
}
这是我所要求的常用卡代码
class PopularCard extends StatelessWidget {
final Color primaryColor, backColor;
final double width, productRate;
final String productUrl, productName, productClients, productPrice;
const PopularCard({
Key key,
this.primaryColor,
this.width,
this.productUrl,
this.productName,
this.productRate,
this.productClients,
this.productPrice,
this.backColor,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
width: width,
height: getProportionateScreenHeight(260),
child: Column(
children: [
Expanded(
flex: 3,
child: Stack(
children: [
Container(
width: double.infinity,
padding: EdgeInsets.all(5),
margin: EdgeInsets.only(right: 15),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey[300],
blurRadius: 4.0,
offset: Offset(3.0, 3.0),
),
],
borderRadius: BorderRadius.circular(12),
color: backColor,
// Color(0xFFFAFAFA),
image: DecorationImage(
image: AssetImage(productUrl),
fit: BoxFit.fitWidth,
),
),
child: infoCard(),
),
LikedWidget(primaryColor: primaryColor)
],
),
)
],
),
);
}
Column infoCard() {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
width: double.infinity,
height: 60,
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(20),
vertical: getProportionateScreenHeight(5)),
decoration: cardInfoDecoration,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
productName,
style: TextStyle(
color: Colors.white,
fontSize: 18.5,
fontWeight: FontWeight.w700,
),
),
Spacer(),
Container(
padding: const EdgeInsets.all(4.0),
decoration: likedWidgetDecoration,
child: Icon(
Icons.near_me,
size: 12.0,
color: primaryColor,
),
),
],
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
'$productRate ',
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
),
),
buildSmoothStarRating(),
SizedBox(width: getProportionateScreenWidth(10)),
Text(
'\($productClients\)',
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
),
),
Spacer(),
Text(
'\$ $productPrice',
style: TextStyle(
fontSize: 15.0,
),
),
],
),
),
),
],
),
),
],
);
}
SmoothStarRating buildSmoothStarRating() {
return SmoothStarRating(
allowHalfRating: false,
onRated: (v) {},
starCount: 5,
rating: productRate,
isReadOnly: true,
size: 13,
color: kRatingStarColor,
borderColor: kRatingStarColor,
);
}
}
class LikedWidget extends StatelessWidget {
const LikedWidget({
Key key,
@required this.primaryColor,
}) : super(key: key);
final Color primaryColor;
@override
Widget build(BuildContext context) {
return Positioned(
top: 5,
right: 20,
child: Container(
alignment: Alignment.topRight,
child: Container(
padding: const EdgeInsets.all(4.0),
decoration: likedWidgetDecoration,
child: Icon(
Icons.favorite,
size: 15.0,
color: primaryColor,
),
),
),
);
}
}
class NavBar extends StatefulWidget {
@override
_NavBarState createState() => _NavBarState();
}
class _NavBarState extends State<NavBar> {
int selectedPage = 0;
final List<Widget> _list = <Widget>[
HomePage(),
ExplorePage(),
NearMe(),
AccountPage(),
];
void navTappedChange(int index) {
setState(() {
selectedPage = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _list[selectedPage],
bottomNavigationBar: BottomNavigationBar(
elevation: 0,
unselectedItemColor: Colors.black,
selectedItemColor: Colors.redAccent,
type: BottomNavigationBarType.shifting,
currentIndex: selectedPage,
onTap: navTappedChange,
items: [
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.house_alt,
),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.compass,
),
label: 'Explore',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.location,
),
label: 'Near Me',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.person,
),
label: 'Account',
),
],
),
);
}
}
class PopularCard扩展了无状态小部件{
最终颜色原色、底色;
最终双宽度,生产率;
最终字符串productUrl、productName、ProductClient、productPrice;
康斯特普洛卡德酒店({
关键点,
这是原色,
这个宽度,
这个.productUrl,
此.productName,
这个生产率,
这是我们的客户,
这个价格,
这个,背景色,
}):super(key:key);
@凌驾
小部件构建(构建上下文){
返回大小框(
宽度:宽度,
高度:格特斯林厄特(260),
子:列(
儿童:[
扩大(
弹性:3,
子:堆栈(
儿童:[
容器(
宽度:double.infinity,
填充:边缘设置。全部(5),
页边距:仅限边集(右:15),
装饰:盒子装饰(
boxShadow:[
箱形阴影(
颜色:颜色。灰色[300],
半径:4.0,
偏移量:偏移量(3.0,3.0),
),
],
边界半径:边界半径。圆形(12),
颜色:背景色,
//颜色(0xFFFAFA),
图像:装饰图像(
图片:AssetImage(productUrl),
适合:BoxFit.fitWidth,
),
),
子项:信息卡(),
),
LikedWidget(primaryColor:primaryColor)
],
),
)
],
),
);
}
列信息卡(){
返回列(
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
容器(
宽度:double.infinity,
身高:60,
填充:EdgeInsets.symmetric(
水平方向:屏幕宽度(20),
垂直方向:按比例计算Creenheight(5)),
装饰:卡迪诺装饰,
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
划船(
儿童:[
正文(
产品名称,
样式:TextStyle(
颜色:颜色,白色,
字体大小:18.5,
fontWeight:fontWeight.w700,
),
),
垫片(),
容器(
填充:常数边集全部(4.0),
装饰:像Widget装饰,
子:图标(
图标。靠近我,
尺寸:12.0,
颜色:原色,
),
),
],
),
扩大(
孩子:填充(
填充:仅限常量边集(顶部:10.0),
孩子:排(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
正文(
“$productRate”,
样式:TextStyle(
字体大小:15.0,
颜色:颜色,白色,
),
),
buildSmoothStarting(),
SizedBox(宽度:GetProcentrateTesCreenWidth(10)),
正文(
“\($productClients\)”,
样式:TextStyle(
字体大小:15.0,
颜色:颜色,白色,
),
),
垫片(),
正文(
“\$$productPrice”,
样式:TextStyle(
字体大小:15.0,
),
),
],
),
),
),
],
),
),
],
);
}
SmoothStarting构建SmoothStarting(){
返回平滑星号(
AllowalFlating:错误,
关于:(v){},
星数:5,
评级:productRate,
isReadOnly:是的,
尺码:13,
颜色:kRatingStarColor,
边框颜色:kRatingStarColor,
);
}
}
类Widget扩展了无状态Widget{
类常量小部件({
关键点,
@需要这个。原色,
}):super(key:key);
最终颜色原色;
@凌驾
小部件构建(构建上下文){
返回定位(
前五名,
右:20,,
子:容器(
对齐:alignment.topRight,
子:容器(
填充:常数边集全部(4.0),
装饰:像Widget装饰,
子:图标(
我的最爱,
尺寸:15.0,
颜色:原色,
),
),
),
);
}
}
这是要求的routes类
class PopularCard extends StatelessWidget {
final Color primaryColor, backColor;
final double width, productRate;
final String productUrl, productName, productClients, productPrice;
const PopularCard({
Key key,
this.primaryColor,
this.width,
this.productUrl,
this.productName,
this.productRate,
this.productClients,
this.productPrice,
this.backColor,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
width: width,
height: getProportionateScreenHeight(260),
child: Column(
children: [
Expanded(
flex: 3,
child: Stack(
children: [
Container(
width: double.infinity,
padding: EdgeInsets.all(5),
margin: EdgeInsets.only(right: 15),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey[300],
blurRadius: 4.0,
offset: Offset(3.0, 3.0),
),
],
borderRadius: BorderRadius.circular(12),
color: backColor,
// Color(0xFFFAFAFA),
image: DecorationImage(
image: AssetImage(productUrl),
fit: BoxFit.fitWidth,
),
),
child: infoCard(),
),
LikedWidget(primaryColor: primaryColor)
],
),
)
],
),
);
}
Column infoCard() {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
width: double.infinity,
height: 60,
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(20),
vertical: getProportionateScreenHeight(5)),
decoration: cardInfoDecoration,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text(
productName,
style: TextStyle(
color: Colors.white,
fontSize: 18.5,
fontWeight: FontWeight.w700,
),
),
Spacer(),
Container(
padding: const EdgeInsets.all(4.0),
decoration: likedWidgetDecoration,
child: Icon(
Icons.near_me,
size: 12.0,
color: primaryColor,
),
),
],
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
'$productRate ',
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
),
),
buildSmoothStarRating(),
SizedBox(width: getProportionateScreenWidth(10)),
Text(
'\($productClients\)',
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
),
),
Spacer(),
Text(
'\$ $productPrice',
style: TextStyle(
fontSize: 15.0,
),
),
],
),
),
),
],
),
),
],
);
}
SmoothStarRating buildSmoothStarRating() {
return SmoothStarRating(
allowHalfRating: false,
onRated: (v) {},
starCount: 5,
rating: productRate,
isReadOnly: true,
size: 13,
color: kRatingStarColor,
borderColor: kRatingStarColor,
);
}
}
class LikedWidget extends StatelessWidget {
const LikedWidget({
Key key,
@required this.primaryColor,
}) : super(key: key);
final Color primaryColor;
@override
Widget build(BuildContext context) {
return Positioned(
top: 5,
right: 20,
child: Container(
alignment: Alignment.topRight,
child: Container(
padding: const EdgeInsets.all(4.0),
decoration: likedWidgetDecoration,
child: Icon(
Icons.favorite,
size: 15.0,
color: primaryColor,
),
),
),
);
}
}
class NavBar extends StatefulWidget {
@override
_NavBarState createState() => _NavBarState();
}
class _NavBarState extends State<NavBar> {
int selectedPage = 0;
final List<Widget> _list = <Widget>[
HomePage(),
ExplorePage(),
NearMe(),
AccountPage(),
];
void navTappedChange(int index) {
setState(() {
selectedPage = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _list[selectedPage],
bottomNavigationBar: BottomNavigationBar(
elevation: 0,
unselectedItemColor: Colors.black,
selectedItemColor: Colors.redAccent,
type: BottomNavigationBarType.shifting,
currentIndex: selectedPage,
onTap: navTappedChange,
items: [
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.house_alt,
),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.compass,
),
label: 'Explore',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.location,
),
label: 'Near Me',
),
BottomNavigationBarItem(
icon: Icon(
CupertinoIcons.person,
),
label: 'Account',
),
],
),
);
}
}
类导航栏扩展StatefulWidget{
@凌驾
_NavBarState createState();
}
类_NavBarState扩展状态{
int selectedPage=0;
最终列表_列表=[
首页(),
ExplorePage(),
NearMe(),
AccountPage(),
];
无效navTappedChange(整数索引){
设置状态(){
selectedPage=索引;
});
}
@凌驾