【问题标题】:How to control the height and width of widget during flutter hero transition?flutter hero 过渡期间如何控制小部件的高度和宽度?
【发布时间】:2021-10-24 15:58:47
【问题描述】:

我想在颤动英雄转换中指定flightShuttleBuilder 的高度和宽度。

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    timeDilation = 5;
    return MaterialApp(
      home: Screen1(),
    );
  }
}

class Screen1 extends StatelessWidget {
  const Screen1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Hero(
          tag: 'icon',
          flightShuttleBuilder: (
            BuildContext flightContext,
            Animation<double> animation,
            HeroFlightDirection flightDirection,
            BuildContext fromHeroContext,
            BuildContext toHeroContext,
          ) {
            return AnimatedBuilder(
              animation: animation,
              builder: (context, child) {
                return Container(
                  height: 200, // not working
                  width: 200,
                  decoration: BoxDecoration(
                    color: Colors.green,
                  ),
                );
              },
            );
          },
          transitionOnUserGestures: true,
          child: IconButton(
            icon: Icon(
              Icons.ad_units,
              size: 50,
            ),
            onPressed: () {
              Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => Screen2()));
            },
          ),
        ),
      ),
    );
  }
}

class Screen2 extends StatelessWidget {
  const Screen2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        alignment: Alignment.bottomCenter,
        child: Hero(
          child: Icon(
            Icons.ad_units,
            size: 100,
          ),
          tag: 'icon',
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout flutter-animation


    【解决方案1】:

    UnconstrainedBox 小部件包裹您的Container。 于是就变成了:

    return UnconstrainedBox( // <--- add this widget here.
      child: Container(
        height: 200,
        width: 200,
        decoration: BoxDecoration(
          color: Colors.green,
        ),
      ),
    );
    

    【讨论】:

    • 它有溢出问题
    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 2019-08-25
    • 2017-04-09
    • 2016-04-28
    • 2010-10-14
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多