flutter - 代码是正确的,但容器没有移动

标签 flutter containers offset

Link编码。

enter image description here

    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: Text('Moving Box')),
            body: Stack(
                children: <Widget>[
            Align(
              alignment: FractionalOffset(1, 0),
              child: Container(
                // Use the properties stored in the State class.
                width: _width,
                height: _height,
                color: Color(0xffe5ee22),
              ),
            ),
          ],
        )
//      body: _buildBody(context)
        );
  }

最佳答案

根据您的要点,您计划使用 AnimatedContainer

因此,我尝试以尽可能少的修改为您修复代码

  1. 必须将 AnimatedContainerApp 包装到 MaterialApp 小部件中

更改此代码:

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

class AnimatedContainerApp extends StatefulWidget {
  @override
  _AnimatedContainerAppState createState() => _AnimatedContainerAppState();
}

进入这个:


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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: AnimatedContainerApp(),
    );
  }
}

class AnimatedContainerApp extends StatefulWidget {
  @override
  _AnimatedContainerAppState createState() => _AnimatedContainerAppState();
}
  1. 将容器更改为动画容器

改变这部分:

child: Container(
  // Use the properties stored in the State class.
  width: _width,
  height: _height,
  color: Color(0xffe5ee22),
),

进入这个:

child: AnimatedContainer( // Changed to AnimatedContainer
  duration: Duration(seconds: 1), // Duration is mandatory
  width: _width,
  height: _height,
  color: Color(0xffe5ee22),
),
  1. 调用setState()创建触发动画的按钮
class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
  bool isSmall = true;
  double _width = 50;
  double _height = 50;

  void changeSize() {
    // Business Logic
    if (isSmall) {
      setState(() {
        _width = 100;
        _height = 100;
        isSmall = false;
      });
    } else {
      setState(() {
        _width = 50;
        _height = 50;
        isSmall = true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Moving Box'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add_to_home_screen),
            onPressed: changeSize, // this will toggle size of container
          )
        ],
      ),

演示

Demo

完整代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: AnimatedContainerApp(),
    );
  }
}

class AnimatedContainerApp extends StatefulWidget {
  @override
  _AnimatedContainerAppState createState() => _AnimatedContainerAppState();
}

class _AnimatedContainerAppState extends State<AnimatedContainerApp> {
  bool isSmall = true;
  double _width = 50;
  double _height = 50;

  void changeSize() {
    // Toggle State
    if (isSmall) {
      setState(() {
        _width = 100;
        _height = 100;
        isSmall = false;
      });
    } else {
      setState(() {
        _width = 50;
        _height = 50;
        isSmall = true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Moving Box'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add_to_home_screen),
            onPressed: changeSize,
          )
        ],
      ),
      body: Stack(
        children: <Widget>[
          Align(
            alignment: FractionalOffset(1, 0),
            child: AnimatedContainer( // Changed to AnimatedContainer
              duration: Duration(seconds: 1), // Add Duration
              width: _width,
              height: _height,
              color: Color(0xffe5ee22),
            ),
          ),
        ],
      ),
    );
  }
}

关于flutter - 代码是正确的,但容器没有移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57561849/

相关文章:

firebase - 无法将参数类型 'Type'分配给参数类型 'FirebaseUser'

javascript - 如何从 websocket 连接读取 header

node.js - 如何删除Azure NodeJS的blob存储容器

c# - LayoutKind.explicit 对于本身就是结构的字段的 .NET 行为

无法理解读取系统调用的行为

firebase - 如何比较来自 firebase 的两个不同集合的两个值?

flutter - 通过ChangeNotifierProvider的加载状态未显示

html - 悬停时的下拉菜单超出容器宽度

Python dict 像满射多键→值容器

javascript - 菜单从左侧滑入 - 添加上边距偏移