AnimatedSwitcher не работает в ReorderableListView - PullRequest
0 голосов
/ 09 мая 2020

Я пытаюсь заставить AnimatedSwitcher работать в ReorderableListView, он работает в обычном ListView. Я думаю, это как-то связано с ключами, но теперь я уверен.

Flutter 1.17.0 • бета-канал • https://github.com/flutter/flutter.git Framework • версия e6b34c2b5 c (7 дней go) • 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();
  }
}

1 Ответ

0 голосов
/ 09 мая 2020

AnimatedSwitcher не будет влиять на виджет Text, так как ваш Text находится внутри ListTile внутри NumberTile. Вы должны поместить прямой виджет, для которого вы хотите анимировать переключатель, внутри AnimatedSwitcher. Отметьте это пример

...