【发布时间】:2020-08-25 02:59:26
【问题描述】:
我试图让 AnimatedSwitcher 在 ReorderableListView 中工作,它在普通 ListView 中工作。我认为这与钥匙有关,但我现在确定了。
Flutter 1.17.0 • 频道测试版 • https://github.com/flutter/flutter.git 框架 • 修订版 e6b34c2b5c(7 天前) • 2020-05-02 11:39:18 -0700 引擎 • 修订版 540786dd51 工具• Dart 2.8.1
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => ChangeNumber(),
child: MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: SafeArea(
child: Consumer<ChangeNumber>(
builder: (context, value, child) {
return Column(
children: <Widget>[
Container(
height: 100,
child: ReorderableListView(
onReorder: (oldIndex, newIndex) {},
children: <Widget>[
AnimatedSwitcher(
key: ValueKey(value.i),
duration: Duration(seconds: 1),
child: NumberTile(
number: value.i,
key: ValueKey(value.i),
),
),
],
),
),
RaisedButton(
child: Text('Increase'),
onPressed: () => value.i = value.i + 1,
)
],
);
},
),
),
),
),
);
}
}
class NumberTile extends StatelessWidget {
final int number;
NumberTile({this.number, key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text('$number'),
);
}
}
class ChangeNumber extends ChangeNotifier {
int _i = 0;
get i => _i;
set i(int value) {
_i = value;
notifyListeners();
}
}
【问题讨论】:
-
看起来您只是在更新一个数字,而不是为另一个小部件切换一个小部件。我不相信动画切换器会起作用,除非你用一个新的孩子替换
AnimatedSwitcher的孩子。将您的ReorderableListView更改为ListView似乎不适用于上面的代码示例。 -
如果您使用 ListView 更改 ReorderedListView 并删除 AnimatedSwitcher 上的键,它将起作用。孩子:ListView(孩子:
[AnimatedSwitcher(持续时间:持续时间(秒:1),孩子:NumberTile(数字:value.i,键:ValueKey(value.i),),),],),跨度>
标签: flutter dart flutter-animation