左右摆动带角度 摆动时带有角度倾斜
- import 'dart:math';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- void main() {
- runApp(const MyApp());
- }
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- // This widget is the root of your application.
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- // This is the theme of your application.
- //
- // TRY THIS: Try running your application with "flutter run". You'll see
- // the application has a blue toolbar. Then, without quitting the app,
- // try changing the seedColor in the colorScheme below to Colors.green
- // and then invoke "hot reload" (save your changes or press the "hot
- // reload" button in a Flutter-supported IDE, or press "r" if you used
- // the command line to start the app).
- //
- // Notice that the counter didn't reset back to zero; the application
- // state is not lost during the reload. To reset the state, use hot
- // restart instead.
- //
- // This works for code too, not just values: Most code changes can be
- // tested with just a hot reload.
- colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
- useMaterial3: true,
- ),
- home: const MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- const MyHomePage({super.key, required this.title});
- final String title;
- @override
- State<MyHomePage> createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
- late final AnimationController _shakeController =
- AnimationController(vsync: this, duration: shakeDuration);
- shake() {
- _shakeController.forward();
- }
- @override
- void initState() {
- _shakeController.addListener(() {
- if (_shakeController.status == AnimationStatus.completed) {
- _shakeController.reset();
- }
- });
- super.initState();
- }
- var shakeCount = 2;
- var shakeDuration = Duration(milliseconds: 1000);
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text("晃动"),
- ),
- body: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- AnimatedBuilder(
- animation: _shakeController,
- builder: (context, child) {
- final sineValue = sin(shakeCount * 2 * pi * _shakeController.value);
- print(sineValue);
- return Transform.translate(
- offset: Offset(sineValue * 10, 0),
- child: Transform.rotate(angle: sineValue *10* (pi / 180),
- child: child),
- );
- },
- child: const Text(
- "我是摆动文字",
- textAlign: TextAlign.center,
- style: TextStyle(color: Colors.orange),
- ),
- ),
- CupertinoButton(child: const Text("抖动"), onPressed: shake),
- ],
- ),
- );
- }
- }
复制代码
|