在布局期间为renderanimatedopacity对象赋予了无限大小

时间:2020-10-29 11:06:56

标签: android ios flutter dart

在运行时使用此代码会给我这个警告:

  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color(0xffebebeb),
        body: AnimatedCrossFade(
        firstChild: SplashScreen1(),
        secondChild: SplashScreen2(),
        crossFadeState:
            !phaseTwo ? CrossFadeState.showFirst : CrossFadeState.showSecond,
        duration: Duration(seconds: 1),
      )
    );
  }

SplashScreen1()

  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffebebeb),
    );
  }

SplashScreen2()

  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffebebeb),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Image.asset('assets/images/splash_image_1.png'),
        ],
      ),
    );
  }

1 个答案:

答案 0 :(得分:1)

您可以在下面复制粘贴运行完整代码
您可以用Scaffold替换SplashScreen1SplashScreen2中的Container
代码段

class SplashScreen1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color(0xffebebeb),
    );
  }
}

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  bool phaseTwo = false;

  @override
  void initState() {
    super.initState();

    Future.delayed(const Duration(seconds: 3), () {
      setState(() {
        phaseTwo = true;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color(0xffebebeb),
        body: AnimatedCrossFade(
          firstChild: SplashScreen1(),
          secondChild: SplashScreen2(),
          crossFadeState:
              !phaseTwo ? CrossFadeState.showFirst : CrossFadeState.showSecond,
          duration: Duration(seconds: 1),
        ));
  }
}

class SplashScreen1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color(0xffebebeb),
    );
  }
}

class SplashScreen2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color(0xffebebeb),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Image.network('https://picsum.photos/250?image=9'),
        ],
      ),
    );
  }
}