mirror of
https://github.com/AyanDas-99/Flutter-animations.git
synced 2025-05-17 13:55:57 +08:00
lecture1
This commit is contained in:
81
lib/Screens/lecture1.dart
Normal file
81
lib/Screens/lecture1.dart
Normal file
@ -0,0 +1,81 @@
|
||||
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:math' as math;
|
||||
|
||||
class Lecture1 extends StatefulWidget {
|
||||
final String title;
|
||||
const Lecture1({
|
||||
Key? key,
|
||||
required this.title,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<Lecture1> createState() => _Lecture1State();
|
||||
}
|
||||
|
||||
class _Lecture1State extends State<Lecture1>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late AnimationController _controller;
|
||||
late Animation _animation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_controller =
|
||||
AnimationController(vsync: this, duration: Duration(seconds: 2));
|
||||
_animation =
|
||||
Tween<double>(begin: 0.0, end: math.pi * 2).animate(_controller);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: Center(
|
||||
child: AnimatedBuilder(
|
||||
animation: _controller,
|
||||
builder: (BuildContext context, Widget? child) {
|
||||
return Transform(
|
||||
alignment: Alignment.center,
|
||||
transform: Matrix4.identity()
|
||||
..rotateY(_animation.value)
|
||||
..rotateZ(_animation.value)
|
||||
..rotateX(_animation.value),
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
if (_controller.isAnimating) {
|
||||
_controller.stop();
|
||||
} else {
|
||||
_controller.repeat();
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
height: 100,
|
||||
width: 100,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Colors.black,
|
||||
blurRadius: 5,
|
||||
spreadRadius: 5,
|
||||
)
|
||||
],
|
||||
borderRadius: BorderRadius.circular(10)),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
100
lib/main.dart
100
lib/main.dart
@ -1,5 +1,10 @@
|
||||
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'Screens/lecture1.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
@ -12,13 +17,17 @@ class MyApp extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: ThemeData(
|
||||
brightness: Brightness.dark,
|
||||
useMaterial3: true,
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
initialRoute: '/',
|
||||
routes: {
|
||||
'/':(context) => const MyHomePage(title: 'AnimatedSwitcher'),
|
||||
|
||||
'/': (context) => const MyHomePage(title: 'Flutter Animations'),
|
||||
'lec1/': (context) =>
|
||||
const Lecture1(title: 'AnimateBuilder and Transform'),
|
||||
},
|
||||
);
|
||||
}
|
||||
@ -34,56 +43,55 @@ class MyHomePage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
final allAssets = [
|
||||
'assets/1677751434999.jpeg',
|
||||
'assets/1681746373201.jpeg',
|
||||
'assets/1677749490747.jpeg',
|
||||
'assets/11.jpg'
|
||||
];
|
||||
int current = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
AnimatedSwitcher(
|
||||
duration: Duration(milliseconds: 500),
|
||||
transitionBuilder: (child, Animation<double> animation) =>
|
||||
FadeTransition(
|
||||
opacity: animation,
|
||||
child: child,
|
||||
),
|
||||
child: Container(
|
||||
key: UniqueKey(),
|
||||
width: 300,
|
||||
height: 300,
|
||||
decoration: BoxDecoration(
|
||||
image:
|
||||
DecorationImage(image: AssetImage(allAssets[current]))),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
if (current == 3) return null;
|
||||
setState(() {
|
||||
current++;
|
||||
});
|
||||
},
|
||||
icon: Icon(Icons.forward)),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
if (current == 0) return null;
|
||||
setState(() {
|
||||
current--;
|
||||
});
|
||||
},
|
||||
icon: Icon(Icons.arrow_back)),
|
||||
],
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: GridView(
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 3, mainAxisSpacing: 10, crossAxisSpacing: 10),
|
||||
children: [
|
||||
Link(link: "lec1/", title: "AnimatedBuilder and Transform"),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Link extends StatelessWidget {
|
||||
final String link;
|
||||
final String title;
|
||||
Link({
|
||||
Key? key,
|
||||
required this.link,
|
||||
required this.title,
|
||||
}) : super(key: key);
|
||||
|
||||
Color background =
|
||||
Colors.primaries[Random().nextInt(Colors.primaries.length)];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return InkWell(
|
||||
onTap: () => Navigator.pushNamed(context, link),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: background, borderRadius: BorderRadius.circular(20)),
|
||||
child: Center(
|
||||
child: Text(
|
||||
title,
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: background.computeLuminance() > 0.5
|
||||
? Colors.black
|
||||
: Colors.white),
|
||||
)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user