如何State在Flutter中热重载子类的字段?
我知道在热重装期间不考虑修改字段的初始值,并且可以对它们使用热重装。但这非常缓慢。
有什么办法可以简化这个过程?
典型的用例是动画,尤其是AnimationController。由于它存储在State字段中,但我们通常希望在其持续时间内进行迭代。例:
class MyAnim extends StatefulWidget {
@override
_MyAnimState createState() => _MyAnimState();
}
class _MyAnimState extends State<MyAnim> with SingleTickerProviderStateMixin {
AnimationController animationController;
@override
void initState() {
animationController =
AnimationController(vsync: this, duration: const Duration(seconds: 1));
super.initState();
}
@override
void dispose() {
animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container();
}
}
Run Code Online (Sandbox Code Playgroud) 我ValueListenableBuilder用来观看一些动画自定义小部件中的一些值。那些基于子项大小使用多个值来对依赖的动作进行动画处理。
我的问题是倾听不止一个价值。我必须把它们嵌套。
下面是一个简化的示例来说明:
class MyWidget extends StatelessWidget {
final ValueNotifier<double> _height1 = ValueNotifier<double>(40);
final ValueNotifier<double> _height2 = ValueNotifier<double>(120);
final ValueNotifier<double> _height3 = ValueNotifier<double>(200);
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
builder: (BuildContext context, double h1, Widget child) {
return ValueListenableBuilder(
builder: (BuildContext context, double h2, Widget child) {
return ValueListenableBuilder(
builder: (BuildContext context, double h3, Widget child) {
return GestureDetector(
// i am using h1, h2 and h3 here ...
child: Stack(
children: <Widget>[
Positioned(
left: …Run Code Online (Sandbox Code Playgroud) 我正在尝试拍打动画:
https://material.io/guidelines/motion/choreography.html#choreography-creation
我的问题是我完全不知道该怎么做。据我所知,flutter中的小部件根本不知道它们的位置,也不知道其位置或其他小部件。而且您无法context.size在build方法内部访问。
我尝试使用Hero动画来做到这一点。或ScaleTransition。但这绝对是行不通的。
import 'package:flutter/material.dart';
main() {
runApp(MaterialApp(
home: HomeScreen(),
));
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Rite.ly'),
),
body: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage('assets/login_background.png'),
fit: BoxFit.fill,
)),
child: new ListView(
children: [
new Padding(
padding:
const EdgeInsets.only(top: 16.0, bottom: 8.0, left: 16.0),
child: new Text('Link : ',
style: new TextStyle(color: Colors.white)),
),
_takeUrl(context),
],
)),
);
}
}
Widget _takeUrl(BuildContext context) {
return …Run Code Online (Sandbox Code Playgroud) 我想要keepAlive我已经呈现的小部件ListView.我尝试了addAutomaticKeepAlives:true按ListView类提供的属性.
这是我使用的示例代码.SliverChildBuilderDelegate委托中提供的相同问题SliverList.
ListView.builder(
itemBuilder: (context,index){
return Card(
child: Container(
child: Image.asset("images/${index+1}.jpg",fit: BoxFit.cover,),
height: 250.0,
),
);
},
addAutomaticKeepAlives: true,
itemCount:40 ,
);
Run Code Online (Sandbox Code Playgroud) 我有一个向我发送值列表的服务,并在使用'dart:convert json.decode'进行解析后,得到了这样的List <dynamic> json:
(values) [ 0.23, 0.2, 0, 0.43 ]
Run Code Online (Sandbox Code Playgroud)
当我尝试将值解析为“双精度”时,出现解析错误
double value = values[2] // error
Run Code Online (Sandbox Code Playgroud)
因为如果我们检查每个元素的.runtimeType,我们可以看到我们具有以下内容
[double, double, int, double]
Run Code Online (Sandbox Code Playgroud)
因此,如何避免将“ 0”解释为int?我想避免使用这样的函数:
double jsonToDouble(dynamic value) {
if(value.runtimeType == int) {
return (value as int).toDouble();
}
return value;
}
Run Code Online (Sandbox Code Playgroud) import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: TestCode(),
);
}
}
class TestCode extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
width: 200,
height: 100,
color: Colors.red,
child: Container(
width: 100,
height: 100,
color: Colors.green,
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
其实这段代码很简单。我只想显示一个200 * 100的红色立方体和一个100 * 100的绿色立方体。
但运行效果是全屏绿?这是为什么?
接下来,我添加了一个Scaffoldto TestCode,如下
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(body: TestCode()), …Run Code Online (Sandbox Code Playgroud) 有时我想强制小部件采用特定大小,通常我使用SizedBox/ ConstrainedBox/来实现Container
但是我已经意识到,在某些情况下,小部件只会忽略它并采用不同的大小。这是为什么?
例:
预期行为:
实际行为:
我想制作一个包含许多高质量音频文件的有声读物应用程序,因此为了使应用程序尺寸更小,所以我将音频上传到了 firebase,但我不知道如何从我的 flutter 应用程序中播放它。