隐式动画在Scaffold持久`bottomSheet`字段中不起作用

时间:2019-08-04 07:24:44

标签: flutter

我一直在尝试在StatefulWidgets Material的持久Scaffold字段中获取适用于自定义bottomSheet的隐式动画,但它们将不起作用。

但是,如果我将自定义StatefulWidgets移到Scaffold的{​​{1}}字段中,它们就可以正常工作。

我已确定问题是由于body总是在每次调用Scaffold时为State bottomSheet创建新的Widget。 br /> 但是,对于setStatebody可以匹配并维护现有的Scaffold

我创建了以下最小测试应用程序来证明这一点:

State

上述应用会产生以下日志输出:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(
              onPressed: _incrementCounter,
              child: Text('Increment: $_counter'),
              color: Colors.amber,
            ),
            CounterText(
              key: Key('body'),
              counter: _counter,
            ),
          ],
        ),
      ),
      bottomSheet: CounterText(
        key: Key('bottomSheet'),
        counter: _counter,
      ),
    );
  }
}

class CounterText extends StatefulWidget {
  const CounterText({Key key, this.counter}) : super(key: key);

  final int counter;

  @override
  _CounterTextState createState() => _CounterTextState();
}

class _CounterTextState extends State<CounterText> {
  @override
  void initState() {
    print('initState ${widget.key} called: counter = ${widget.counter}');
    super.initState();
  }

  @override
  void didUpdateWidget(CounterText oldWidget) {
    print('didUpdateWidget ${widget.key} called: counter = ${widget.counter}');
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return Text('Counter: ${widget.counter}');
  }
}

如您所见,首先初始化I/flutter (14567): initState [<'body'>] called: counter = 0 I/flutter (14567): initState [<'bottomSheet'>] called: counter = 0 I/flutter (14567): didUpdateWidget [<'body'>] called: counter = 1 I/flutter (14567): initState [<'bottomSheet'>] called: counter = 1 I/flutter (14567): didUpdateWidget [<'body'>] called: counter = 2 I/flutter (14567): initState [<'bottomSheet'>] called: counter = 2 小部件的两个实例之后,随后对CounterText的调用将导致setState的{​​{1}}实例调用{ {1}},而body实例再次调用CounterText

在我将此问题提交给Flutter团队之前,我想知道我是否忽略了任何内容?

这真的是应该解决的错误还是这是意向的行为,我只是不正确地理解事情?

0 个答案:

没有答案