1、AnimatedOpacity

控制widget淡入或淡出效果  

  

栗子是点击MaterialButton后,通过更新opacity变量,控制details的opacity;

import 'package:flutter/material.dart';

const owl_url =
    'https://raw.githubusercontent.com/flutter/website/master/src/images/owl.jpg';

class FadeInDemo extends StatefulWidget {
  _FadeInDemoState createState() => _FadeInDemoState();
}

class _FadeInDemoState extends State<FadeInDemo> {
  double opacity = 0.0;

  @override
  Widget build(BuildContext context) {
    return Column(children: <Widget>[
      Image.network(owl_url),
      MaterialButton(
        child: Text(
          'Show details',
          style: TextStyle(color: Colors.blueAccent),
        ),
        onPressed: () => setState(() {
          opacity = opacity == 1 ? 0.0 : 1;
        }),
      ),
      AnimatedOpacity(
        opacity: opacity,
        duration: Duration(seconds: 2),
        child: Column(
          children: <Widget>[
            Text('Type: Owl'),
            Text('Age: 39'),
            Text('Employment: None'),
          ],
        ),
      )
    ]);
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: FadeInDemo(),
        ),
      ),
    );
  }
}

void main() {
  runApp(
    MyApp(),
  );
}
View Code

相关文章:

  • 2021-12-03
  • 2021-09-07
  • 2021-10-20
  • 2021-07-04
  • 2023-04-04
  • 2021-04-29
猜你喜欢
  • 2021-07-30
  • 2021-08-12
  • 2021-07-18
  • 2021-07-11
  • 2021-08-02
  • 2022-03-02
  • 2021-07-13
相关资源
相似解决方案