flutter_carousel/example/lib/main.dart

190 lines
4.7 KiB
Dart
Raw Normal View History

2022-11-01 08:20:01 +01:00
// SPDX-FileCopyrightText: 2022 Iconica
//
// SPDX-License-Identifier: BSD-3-Clause
2022-08-24 16:38:36 +02:00
import 'package:carousel/carousel.dart';
2022-09-02 14:00:16 +02:00
2022-08-24 16:38:36 +02:00
import 'package:carousel_example/pokemon.dart';
import 'package:carousel_example/pokemon_card.dart';
import 'package:carousel_example/pokemon_types.dart';
2022-08-24 16:38:36 +02:00
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() {
runApp(const CarouselExampleApp());
}
class CarouselExampleApp extends StatefulWidget {
const CarouselExampleApp({Key? key}) : super(key: key);
@override
State<CarouselExampleApp> createState() => _CarouselExampleAppState();
}
class _CarouselExampleAppState extends State<CarouselExampleApp> {
Pokemon? selected;
final List<Pokemon> pokemons = [
Pokemon(
name: 'Bulbasaur',
nr: 1,
types: [
PokemonType.grass,
PokemonType.poison,
],
),
Pokemon(
name: 'Charmander',
nr: 4,
types: [
PokemonType.fire,
],
),
Pokemon(
name: 'Squirtle',
nr: 7,
types: [
PokemonType.water,
],
),
Pokemon(
name: 'Caterpie',
nr: 10,
types: [
PokemonType.bug,
],
),
Pokemon(
name: 'Pidgey',
nr: 16,
types: [
PokemonType.normal,
PokemonType.flying,
],
),
Pokemon(
name: 'Pikachu',
nr: 25,
types: [
PokemonType.electric,
],
),
Pokemon(
name: 'Machop',
nr: 66,
types: [
PokemonType.fighting,
],
),
Pokemon(
name: 'Geodude',
nr: 74,
types: [
PokemonType.rock,
PokemonType.ground,
],
),
2022-08-24 16:38:36 +02:00
];
Widget _buildCard(BuildContext context, int index) {
return PokemonCard(
pokemon: pokemons[index % pokemons.length],
);
}
void _onClick(int index) {
setState(() {
selected = pokemons[index % pokemons.length];
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
alignment: Alignment.bottomCenter,
children: [
Padding(
padding: const EdgeInsets.all(16.0),
2022-08-24 16:38:36 +02:00
child: ListView(
children: [
const SizedBox(height: 50),
Carousel(
pageViewHeight: 220,
selectableCardId: 4,
onCardClick: _onClick,
transforms: [
CardTransform(
2022-09-26 14:52:20 +02:00
x: 220,
y: -85,
angle: -math.pi / 12,
scale: 0.3,
opacity: 0.2,
),
2022-08-24 16:38:36 +02:00
CardTransform(
2022-09-26 14:52:20 +02:00
x: 230,
y: -80,
angle: math.pi / 12,
scale: 0.5,
opacity: 0.4,
),
2022-08-24 16:38:36 +02:00
CardTransform(
2022-09-26 14:52:20 +02:00
x: 200,
y: -75,
angle: -math.pi / 12,
scale: 0.6,
opacity: 0.6,
),
2022-08-24 16:38:36 +02:00
CardTransform(
2022-09-26 14:52:20 +02:00
x: 110,
y: -60,
angle: math.pi / 12,
scale: 0.7,
opacity: 0.8,
),
CardTransform(
x: 0,
y: 0,
angle: 0,
scale: 1,
opacity: 1.0,
),
2022-08-24 16:38:36 +02:00
CardTransform(
2022-09-26 14:52:20 +02:00
x: -270,
y: 70,
angle: -math.pi / 50,
scale: 1,
opacity: 0.5,
),
2022-08-24 16:38:36 +02:00
],
builder: _buildCard,
),
Carousel(
selectableCardId: 1,
onCardClick: _onClick,
alignment: Alignment.center,
transforms: [
CardTransform(x: 0, y: 0, angle: 0, scale: 0.5),
CardTransform(x: 0, y: 0, angle: 0, scale: 1),
CardTransform(
x: 300, y: 0, angle: math.pi / 20, scale: 1),
],
builder: _buildCard,
),
],
),
),
if (selected != null)
Padding(
padding: const EdgeInsets.only(bottom: 20),
2022-08-24 16:38:36 +02:00
child: Text('Clicked: ${selected!.name}'),
),
],
),
),
);
}
}