flutter 左右晃动摆动动画例子

siman 2024-6-5 18:22:17 355 0 来自 中国
左右摆动带角度  摆动时带有角度倾斜


  1. import 'dart:math';

  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';

  4. void main() {
  5.   runApp(const MyApp());
  6. }

  7. class MyApp extends StatelessWidget {
  8.   const MyApp({super.key});

  9.   // This widget is the root of your application.
  10.   @override
  11.   Widget build(BuildContext context) {
  12.     return MaterialApp(
  13.       title: 'Flutter Demo',
  14.       theme: ThemeData(
  15.         // This is the theme of your application.
  16.         //
  17.         // TRY THIS: Try running your application with "flutter run". You'll see
  18.         // the application has a blue toolbar. Then, without quitting the app,
  19.         // try changing the seedColor in the colorScheme below to Colors.green
  20.         // and then invoke "hot reload" (save your changes or press the "hot
  21.         // reload" button in a Flutter-supported IDE, or press "r" if you used
  22.         // the command line to start the app).
  23.         //
  24.         // Notice that the counter didn't reset back to zero; the application
  25.         // state is not lost during the reload. To reset the state, use hot
  26.         // restart instead.
  27.         //
  28.         // This works for code too, not just values: Most code changes can be
  29.         // tested with just a hot reload.
  30.         colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  31.         useMaterial3: true,
  32.       ),
  33.       home: const MyHomePage(title: 'Flutter Demo Home Page'),
  34.     );
  35.   }
  36. }

  37. class MyHomePage extends StatefulWidget {
  38.   const MyHomePage({super.key, required this.title});

  39.   final String title;

  40.   @override
  41.   State<MyHomePage> createState() => _MyHomePageState();
  42. }

  43. class _MyHomePageState extends State<MyHomePage>  with TickerProviderStateMixin {
  44.   late final AnimationController _shakeController =
  45.   AnimationController(vsync: this, duration: shakeDuration);

  46.   shake() {
  47.     _shakeController.forward();
  48.   }

  49.   @override
  50.   void initState() {
  51.     _shakeController.addListener(() {
  52.       if (_shakeController.status == AnimationStatus.completed) {
  53.         _shakeController.reset();
  54.       }
  55.     });
  56.     super.initState();
  57.   }

  58.   var shakeCount = 2;
  59.   var shakeDuration = Duration(milliseconds: 1000);
  60.   @override
  61.   Widget build(BuildContext context) {
  62.     return Scaffold(
  63.       appBar: AppBar(
  64.         title: const Text("晃动"),
  65.       ),
  66.       body: Column(
  67.         crossAxisAlignment: CrossAxisAlignment.stretch,
  68.         mainAxisAlignment: MainAxisAlignment.center,
  69.         children: [
  70.           AnimatedBuilder(
  71.             animation: _shakeController,
  72.             builder: (context, child) {
  73.               final sineValue = sin(shakeCount * 2 * pi * _shakeController.value);

  74.               print(sineValue);
  75.               return Transform.translate(
  76.                 offset: Offset(sineValue * 10, 0),
  77.                 child: Transform.rotate(angle: sineValue *10* (pi / 180),
  78.                 child: child),
  79.               );
  80.             },
  81.             child: const Text(
  82.               "我是摆动文字",
  83.               textAlign: TextAlign.center,
  84.               style: TextStyle(color: Colors.orange),
  85.             ),
  86.           ),
  87.           CupertinoButton(child: const Text("抖动"), onPressed: shake),
  88.         ],
  89.       ),
  90.     );
  91.   }
  92. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-9-8 08:17, Processed in 0.152249 second(s), 34 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表