Flutter:移动小部件到另一个位置

3

enter image description here

当按下按钮时,我希望文本(Text to move up)到达屏幕顶部并在中心显示新文本。我认为stack和positioned widget可能有效。我希望拥有一个响应式的应用程序。
import 'package:flutter/material.dart';

class MoveTest extends StatefulWidget {
  @override
  _MoveTestState createState() => _MoveTestState();
}

class _MoveTestState extends State<MoveTest> {
  bool moveIt = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Expanded(
            child: Center(
              child: Text(
                'Text to move up',
                style: TextStyle(fontSize: 30),
              ),
            ),
          ),
          (moveIt)
              ? Container(
                  child: Text(
                    'New Text in the center',
                    style: TextStyle(fontSize: 30),
                  ),
                )
              : Container(),
          Container(
            child: RaisedButton(
              onPressed: () {
                setState(() {
                  moveIt = true;
                });
              },
              child: Text('Move'),
            ),
          )
        ],
      ),
    );
  }
}

如何用少量动画移动文本并显示新的小部件和一些文本?
我应该使用具有定位小部件的堆栈吗?
如何使用堆栈/定位小部件和响应式应用程序?

编辑:

你通过不提供任何帮助而给予负面投票来获得快乐吗?残忍的心灵

1个回答

3
我会选择 SlideTransition (https://api.flutter.dev/flutter/widgets/SlideTransition-class.html),通过 Animation 对象控制 SlideTranslation 的动画完成后,您可以在中间显示任何文本。
以下是一个小的代码示例,可以帮助您启动工作。
import 'dart:async';

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter SlideTransition',
      theme: ThemeData.dark(),
      home: MyHomePage()
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  MyHomePageState createState() => MyHomePageState();

}


class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {

  AnimationController _animationController;
  Animation _animation;

  @override
  void initState() {
    _animationController =
        AnimationController(vsync: this, duration: Duration(seconds: 2));
    _animation = Tween<Offset>(begin: Offset(0, 0), end: Offset(0, -0.4)).animate(
        _animationController);

    _animationController.forward().whenComplete(() {
      // put here the stuff you wanna do when animation completed!
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: SlideTransition(
          position: _animation,
          child: Center(child: Text("My Text")),
        )
    );
  }
}

我使用了 stack 和 positioned widget 完成了,但是你的看起来很棒。谢谢! - Mahesh
3
@Mahesh,你能分享一下你使用堆栈和定位小部件实现的代码吗?我也在尝试解决同样的问题。或许可以在这里添加另一个答案。 - MobileMon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接