在 Flutter 中访问另一个小部件的构建方法中的 State Class 字段
Access to State Class field in another widget's build method in Flutter
有这么一个class
class ProgressBar extends StatefulWidget {
final double size;
final String text;
final String textSize;
ProgressBar({Key? key, required this.size, required this.text, required this.textSize}) : super(key: key);
@override
_ProgressBarState createState() => _ProgressBarState();
}
class _ProgressBarState extends State<ProgressBar> with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(
seconds: 5,
),
);
animation = Tween<double>(
begin: 0,
end: widget.size,
).animate(CurvedAnimation(parent: _controller, curve: Curves.easeInOut))
..addListener(() {
setState(() {});
});
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
return Padding(
padding: const EdgeInsets.only(left: 20, right: 20, top: 12, bottom: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.text, style: theme.textTheme.bodyText1!.copyWith(fontSize: 14, color: theme.colorScheme.primary)),
Padding(
padding: const EdgeInsets.only(top: 15, bottom: 6),
child: LinearProgressIndicator(
value: _controller.value,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('${animation.value.toStringAsFixed(2)} Мб из ${widget.textSize} Мб', style: theme.textTheme.bodyText2!.copyWith(color: Color(0xff8F92A1), fontSize: 12)),
Text('Загрузка...', style: theme.textTheme.bodyText2!.copyWith(color: Color(0xff8F92A1), fontSize: 12)),
],)
],
),
);
}
}
我需要在另一个 class 中使用 'animation'。
child: ListView.separated(
padding: EdgeInsets.zero,
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: NeverScrollableScrollPhysics(),
separatorBuilder: (BuildContext context, int index) => Divider(color: Color(0xffF3F6F8)),
itemCount: items.length + 1,
itemBuilder: (context, index) {
if (index == 0) {
return _rowAdd();
}
return animation.isCompleted ? ProgressBar(size: items[index - 1].sizeFile, text: items[index - 1].nameFile, textSize: items[index - 1].sizeFile.toStringAsFixed(2),) :
Padding(
...
);
如何正确操作?这是一个临时拐杖。一般来说,我的 file_picker 向 Item class 添加数据,我需要一个空的 CallBack 来每个文件,并临时在里面放一个动画超时,但我不知道该怎么做然而...
我不明白你的意思,但我会尽力帮助你:
你想从另一个 class 回电话吗?使用这个:
假设我想将回调数据作为字符串:
import 'package:flutter/material.dart';
typedef void StringCallback(String value);
class Example extends StatefulWidget {
final StringCallback callback;
const Example({Key key, this.callback}) : super(key: key);
@override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
getString();
},
);
}
getString(){
// This is the Data i need from this class:
String data = "Data I need";
setState(() {
widget.callback(data);
});
}
}
在另一个 class 中,您可以像这样使用它:
Widget testTheCallbackInAnotherClass(){
return Example(
callback: (value){
setState(() {
final dataIgetFromAnotherClass = value;
print(dataIgetFromAnotherClass);
});
},
);
}
您甚至可以像这样调用另一个 class 中的所有函数:
先做一个key例如class状态
GlobalKey<_ExampleState> _key = GlobalKey<_ExampleState>();
然后调用函数:
String dataIneedFromExampleClass = _key.currentState.getString();
希望对您有所帮助
有这么一个class
class ProgressBar extends StatefulWidget {
final double size;
final String text;
final String textSize;
ProgressBar({Key? key, required this.size, required this.text, required this.textSize}) : super(key: key);
@override
_ProgressBarState createState() => _ProgressBarState();
}
class _ProgressBarState extends State<ProgressBar> with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(
seconds: 5,
),
);
animation = Tween<double>(
begin: 0,
end: widget.size,
).animate(CurvedAnimation(parent: _controller, curve: Curves.easeInOut))
..addListener(() {
setState(() {});
});
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
return Padding(
padding: const EdgeInsets.only(left: 20, right: 20, top: 12, bottom: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(widget.text, style: theme.textTheme.bodyText1!.copyWith(fontSize: 14, color: theme.colorScheme.primary)),
Padding(
padding: const EdgeInsets.only(top: 15, bottom: 6),
child: LinearProgressIndicator(
value: _controller.value,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('${animation.value.toStringAsFixed(2)} Мб из ${widget.textSize} Мб', style: theme.textTheme.bodyText2!.copyWith(color: Color(0xff8F92A1), fontSize: 12)),
Text('Загрузка...', style: theme.textTheme.bodyText2!.copyWith(color: Color(0xff8F92A1), fontSize: 12)),
],)
],
),
);
}
}
我需要在另一个 class 中使用 'animation'。
child: ListView.separated(
padding: EdgeInsets.zero,
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: NeverScrollableScrollPhysics(),
separatorBuilder: (BuildContext context, int index) => Divider(color: Color(0xffF3F6F8)),
itemCount: items.length + 1,
itemBuilder: (context, index) {
if (index == 0) {
return _rowAdd();
}
return animation.isCompleted ? ProgressBar(size: items[index - 1].sizeFile, text: items[index - 1].nameFile, textSize: items[index - 1].sizeFile.toStringAsFixed(2),) :
Padding(
...
);
如何正确操作?这是一个临时拐杖。一般来说,我的 file_picker 向 Item class 添加数据,我需要一个空的 CallBack 来每个文件,并临时在里面放一个动画超时,但我不知道该怎么做然而...
我不明白你的意思,但我会尽力帮助你:
你想从另一个 class 回电话吗?使用这个:
假设我想将回调数据作为字符串:
import 'package:flutter/material.dart';
typedef void StringCallback(String value);
class Example extends StatefulWidget {
final StringCallback callback;
const Example({Key key, this.callback}) : super(key: key);
@override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
getString();
},
);
}
getString(){
// This is the Data i need from this class:
String data = "Data I need";
setState(() {
widget.callback(data);
});
}
}
在另一个 class 中,您可以像这样使用它:
Widget testTheCallbackInAnotherClass(){
return Example(
callback: (value){
setState(() {
final dataIgetFromAnotherClass = value;
print(dataIgetFromAnotherClass);
});
},
);
}
您甚至可以像这样调用另一个 class 中的所有函数:
先做一个key例如class状态
GlobalKey<_ExampleState> _key = GlobalKey<_ExampleState>();
然后调用函数:
String dataIneedFromExampleClass = _key.currentState.getString();
希望对您有所帮助