[英]Flutter can't access widget variable when its wrapped in ValueListenableBuilder widget
我刚刚用ValueListenableBuilder
包装了以下文本小部件,现在我无法访问我的widget.homeTween
变量
获取“无法无条件访问属性‘homeTween’,因为接收者可以为‘null’。”
ValueListenableBuilder<Box<String>>(
valueListenable: _localUser.listenable(),
builder: (context, box, widget) {
return Text(
'${getGreetings()!} ${_localUser.get("_firstName")}s',
style: TextStyle(
color: widget.homeTween!.value as Color,
fontSize: 16,
fontFamily: 'Nunito Regular',
),
);
},
),
像以前一样工作,没有问题。
这是整个小部件 class
// Imports:
import 'package:dogbase_app/theme/app_styles.dart';
import 'package:dogbase_app/utilities/helpers/greetings.dart';
import 'package:dogbase_app/utilities/hive/local_user.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gravatar/flutter_gravatar.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:namefully/namefully.dart';
class AnimatedAppBar extends StatefulWidget {
const AnimatedAppBar({
Key? key,
required this.colorAnimationController,
required this.onPressed,
required this.colorTween,
required this.homeTween,
required this.iconTween,
required this.drawerTween,
required this.workOutTween,
required this.loadpage,
}) : super(key: key);
final AnimationController colorAnimationController;
final Animation<dynamic>? colorTween;
final Animation<dynamic>? workOutTween;
final Animation<dynamic>? homeTween;
final Animation<dynamic>? iconTween;
final Animation<dynamic>? drawerTween;
final VoidCallback onPressed;
final Function loadpage;
@override
State<AnimatedAppBar> createState() => _AnimatedAppBarState();
}
class _AnimatedAppBarState extends State<AnimatedAppBar> {
late Gravatar? _gravatar;
@override
void initState() {
super.initState();
}
final _localUser = LocalUser().getUser();
@override
Widget build(BuildContext context) {
return SizedBox(
height: 65,
child: AnimatedBuilder(
animation: widget.colorAnimationController,
builder: (context, child) => AppBar(
toolbarHeight: 65,
leading: IconButton(
icon: Icon(
Icons.dehaze,
color: widget.drawerTween!.value as Color,
),
onPressed: widget.onPressed,
),
backgroundColor: widget.colorTween!.value as Color,
elevation: 0,
titleSpacing: 0,
title: Row(
children: <Widget>[
ValueListenableBuilder<Box<String>>(
valueListenable: _localUser.listenable(),
builder: (context, box, widget) {
return Text(
'''${getGreetings()!} ${Namefully(_localUser.get("_firstName").toString())}''',
style: TextStyle(
color: widget.homeTween!.value as Color,
fontSize: 16,
fontFamily: 'Nunito Regular',
),
);
},
),
],
),
actions: <Widget>[
Icon(
Icons.notifications,
color: widget.iconTween!.value as Color,
),
Padding(
padding: const EdgeInsets.all(10),
child: GestureDetector(
onTap: () {
widget.loadpage(8, accentColor);
},
child: CircleAvatar(
backgroundColor: whiteColor,
radius: 45,
child: ClipOval(
child: Image.network(
_gravatar!.imageUrl(),
),
),
),
),
),
],
),
),
);
}
}
因为widget
现在指的是构建器参数而不是StatefulWidget
小部件属性,请尝试以下操作:
builder: (context, box, Widget? child) {
// ...
和:
// Imports:
import 'package:dogbase_app/theme/app_styles.dart';
import 'package:dogbase_app/utilities/helpers/greetings.dart';
import 'package:dogbase_app/utilities/hive/local_user.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gravatar/flutter_gravatar.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:namefully/namefully.dart';
class AnimatedAppBar extends StatefulWidget {
const AnimatedAppBar({
Key? key,
required this.colorAnimationController,
required this.onPressed,
required this.colorTween,
required this.homeTween,
required this.iconTween,
required this.drawerTween,
required this.workOutTween,
required this.loadpage,
}) : super(key: key);
final AnimationController colorAnimationController;
final Animation<dynamic>? colorTween;
final Animation<dynamic>? workOutTween;
final Animation<dynamic>? homeTween;
final Animation<dynamic>? iconTween;
final Animation<dynamic>? drawerTween;
final VoidCallback onPressed;
final Function loadpage;
@override
State<AnimatedAppBar> createState() => _AnimatedAppBarState();
}
class _AnimatedAppBarState extends State<AnimatedAppBar> {
late Gravatar? _gravatar;
@override
void initState() {
super.initState();
}
final _localUser = LocalUser().getUser();
@override
Widget build(BuildContext context) {
return SizedBox(
height: 65,
child: AnimatedBuilder(
animation: widget.colorAnimationController,
builder: (context, child) => AppBar(
toolbarHeight: 65,
leading: IconButton(
icon: Icon(
Icons.dehaze,
color: widget.drawerTween!.value as Color,
),
onPressed: widget.onPressed,
),
backgroundColor: widget.colorTween!.value as Color,
elevation: 0,
titleSpacing: 0,
title: Row(
children: <Widget>[
ValueListenableBuilder<Box<String>>(
valueListenable: _localUser.listenable(),
builder: (context, box, _) {
return Text(
'''${getGreetings()!} ${Namefully(_localUser.get("_firstName").toString())}''',
style: TextStyle(
color: widget.homeTween!.value as Color,
fontSize: 16,
fontFamily: 'Nunito Regular',
),
);
},
),
],
),
actions: <Widget>[
Icon(
Icons.notifications,
color: widget.iconTween!.value as Color,
),
Padding(
padding: const EdgeInsets.all(10),
child: GestureDetector(
onTap: () {
widget.loadpage(8, accentColor);
},
child: CircleAvatar(
backgroundColor: whiteColor,
radius: 45,
child: ClipOval(
child: Image.network(
_gravatar!.imageUrl(),
),
),
),
),
),
],
),
),
);
}
}
能够解决这种color: this.widget.homeTween.,value as Color,
Gwhyyy 指出了这个问题,你可以直接使用 like
ValueListenableBuilder(
builder: (context, box, _) {
return Text(
'',
style: TextStyle(
color: widget.homeTween?.value,//null accepted
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.