Skip to content

Commit

Permalink
feat: add carousel to display qrcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuel-braz committed Apr 16, 2024
1 parent 9751a7b commit 2f252f7
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/core/utils/layout_util.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';

class LayoutUtil {
static bool isMobileLayout(BuildContext context) => MediaQuery.of(context).size.width < 600;
static bool isMobileLayout(BuildContext context) => MediaQuery.of(context).size.width < 700;
}
29 changes: 24 additions & 5 deletions lib/data/event_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ class EventModel {
final String eventDate;
final String? color;
final List<SocialQrCode> socialQrCodes;
final bool isCarouselView;

const EventModel(
{required this.eventName,
required this.eventDate,
this.color,
this.socialQrCodes = const []});
const EventModel({
required this.eventName,
required this.eventDate,
this.color,
this.socialQrCodes = const [],
this.isCarouselView = false,
});

factory EventModel.fromMap(Map<String, dynamic> data) => EventModel(
eventName: data['eventName'] as String,
Expand Down Expand Up @@ -45,4 +48,20 @@ class EventModel {
///
/// Converts [EventModel] to a JSON string.
String toJson() => json.encode(toMap());

EventModel copyWith({
String? eventName,
String? eventDate,
String? color,
List<SocialQrCode>? socialQrCodes,
bool? isCarouselView,
}) {
return EventModel(
eventName: eventName ?? this.eventName,
eventDate: eventDate ?? this.eventDate,
color: color ?? this.color,
socialQrCodes: socialQrCodes ?? this.socialQrCodes,
isCarouselView: isCarouselView ?? this.isCarouselView,
);
}
}
4 changes: 4 additions & 0 deletions lib/presentation/home/event_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ class EventStore extends ValueNotifier<EventModel?> {

return jsonDecode(data);
}

void toggleView() {
value = value!.copyWith(isCarouselView: !value!.isCarouselView);
}
}
33 changes: 23 additions & 10 deletions lib/presentation/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,30 @@ class _HomePageState extends State<HomePage> {
),
actions: [
if (!LayoutUtil.isMobileLayout(context))
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, 'sorteio');
},
child: const Text(
'Sorteio',
Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, 'sorteio');
},
child: const Text(
'Sorteio',
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
_store.toggleView();
},
child: Icon(_store.value!.isCarouselView ? Icons.grid_view_rounded : Icons.view_carousel),
),
),
],
)
],
centerTitle: true,
backgroundColor: _store.value!.color != null
Expand Down
51 changes: 51 additions & 0 deletions lib/presentation/home/home_view/carousel_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'dart:math' as math;

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_sp_social/presentation/home/home_view/desktop_view.dart';

import '../event_store.dart';

class CarouselView extends StatefulWidget {
final EventStore store;

const CarouselView({
required this.store,
super.key,
});

@override
State<CarouselView> createState() => _CarouselViewState();
}

class _CarouselViewState extends State<CarouselView> {
final carouselController = CarouselController();

@override
Widget build(BuildContext context) {
final items = widget.store.value!.socialQrCodes;
final screenHeight = MediaQuery.of(context).size.height;
final screenWidth = MediaQuery.of(context).size.width;
final double maxWidth = math.min(screenWidth * 0.4, 500);
final double maxHeight = math.min(screenHeight * 0.7, maxWidth * 1.2);

return Container(
alignment: Alignment.center,
width: screenWidth,
child: SizedBox(
height: maxHeight,
child: CarouselSlider(
items: items.map((e) => QRCode(socialQrCode: e, width: maxWidth)).toList(),
carouselController: carouselController,
options: CarouselOptions(
clipBehavior: Clip.none,
autoPlay: true,
viewportFraction: 1,
aspectRatio: screenWidth / screenHeight,
initialPage: 0,
),
),
),
);
}
}
11 changes: 5 additions & 6 deletions lib/presentation/home/home_view/desktop_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ class _DesktopViewState extends State<DesktopView> {
spacing: 16,
runSpacing: 16,
children:
widget.store.value!.socialQrCodes.map((e) => _QRCode(socialQrCode: e, width: _cardMaxWidth)).toList()),
widget.store.value!.socialQrCodes.map((e) => QRCode(socialQrCode: e, width: _cardMaxWidth)).toList()),
),
);
}
}

class _QRCode extends StatelessWidget {
class QRCode extends StatelessWidget {
final SocialQrCode socialQrCode;
final double width;

const _QRCode({required this.socialQrCode, required this.width});
const QRCode({super.key, required this.socialQrCode, required this.width});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -104,9 +104,8 @@ class _QRCode extends StatelessWidget {
padding: const EdgeInsets.all(8.0),
child: Text(
socialQrCode.title,
style: Theme.of(context).textTheme.headlineSmall!.copyWith(
color: Theme.of(context).colorScheme.background,
),
style:
Theme.of(context).textTheme.headlineSmall!.copyWith(color: Theme.of(context).colorScheme.surface),
textAlign: TextAlign.center,
maxLines: 2,
overflow: TextOverflow.ellipsis,
Expand Down
4 changes: 4 additions & 0 deletions lib/presentation/home/home_view/home_page_factory.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_sp_social/core/utils/layout_util.dart';
import 'package:flutter_sp_social/presentation/home/event_store.dart';
import 'package:flutter_sp_social/presentation/home/home_view/carousel_view.dart';
import 'package:flutter_sp_social/presentation/home/home_view/desktop_view.dart';
import 'package:flutter_sp_social/presentation/home/home_view/mobile_view.dart';

Expand All @@ -9,6 +10,9 @@ class HomePageViewFactory {
if (LayoutUtil.isMobileLayout(context)) {
return MobileView(store: store);
} else {
if (store.value?.isCarouselView ?? false) {
return CarouselView(store: store);
}
return DesktopView(store: store);
}
}
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
carousel_slider:
dependency: "direct main"
description:
name: carousel_slider
sha256: "9c695cc963bf1d04a47bd6021f68befce8970bcd61d24938e1fb0918cf5d9c42"
url: "https://pub.dev"
source: hosted
version: "4.2.1"
characters:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
crypto: ^3.0.3
google_fonts: any
file_picker: ^6.1.1
carousel_slider: ^4.2.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 2f252f7

Please sign in to comment.