我已经创建了我的自定义有状态小部件,并且我使用一个布尔值来检查AnimatedContainer的状态。此外,在createState中有一个函数可以检查AnimatedContainer的状态并更改容器的宽度。我的问题是,我试图在AnimatedContainer中使用函数_handleTap()作为我的孩子,但是它给了我一个错误,告诉我表达式的类型是空的,因此,不能使用。
class SectionTaps extends StatefulWidget {
SectionTaps(this.isActive);
bool isActive = false;
_SectionTapsState createState() => _SectionTapsState();
}
class _SectionTapsState extends State<SectionTaps> {
bool _isActive = false;
double _width = 255.0;
void _handleTap(){
setState(() {
_isActive = widget.isActive;
_isActive == true ? _width = 55.0 : _width = 255.0;
//change container width
});
}
final leftButton = new AnimatedContainer(
duration: Duration(seconds: 1),
height: 88.0,
width: 255.0,
decoration: new BoxDecoration(
color: new Color(0xFF376480),
shape: BoxShape.rectangle,
borderRadius: new BorderRadius.only(
topRight: Radius.circular(80.0),
bottomRight: Radius.circular(80.0),
),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Colors.black12,
blurRadius: 10.0,
offset: new Offset(0.0, .0),
),
],
),
child: _handleTap(),
);发布于 2019-01-17 23:02:09
您需要用GestureDetector包装您的- AnimatedContainer,并使用onTap:调用setSate()。
代码:
class SectionTaps extends StatefulWidget {
SectionTaps(this.isActive);
bool isActive = false;
_SectionTapsState createState() => _SectionTapsState();
}
class _SectionTapsState extends State<SectionTaps> {
bool _isActive = false;
double _width = 255.0;
void _handleTap() {
setState(() {
print ('Set State Called.');
_isActive = widget.isActive;
_isActive == true ? _width = 55.0 : _width = 255.0;
//change container width
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: GestureDetector(
onTap: () {
_handleTap();
},
child: new AnimatedContainer(
duration: Duration(seconds: 1),
height: 88.0,
width: 255.0,
decoration: new BoxDecoration(
color: new Color(0xFF376480),
shape: BoxShape.rectangle,
borderRadius: new BorderRadius.only(
topRight: Radius.circular(80.0),
bottomRight: Radius.circular(80.0),
),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Colors.black12,
blurRadius: 10.0,
offset: new Offset(0.0, .0),
),
],
),
),
),
),
);
}
}https://stackoverflow.com/questions/54238294
复制相似问题