progress on home page
This commit is contained in:
75
lib/screens/home/home_controller.dart
Normal file
75
lib/screens/home/home_controller.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:qpay/http/http.dart';
|
||||
|
||||
part 'home_controller.freezed.dart';
|
||||
part 'home_controller.g.dart';
|
||||
|
||||
class HomeScreenModel {
|
||||
List<dynamic> transactions = [];
|
||||
List<Category> categories = [];
|
||||
dynamic account = {};
|
||||
dynamic profile = {};
|
||||
bool isLoading = false;
|
||||
bool isLoggedIn = false;
|
||||
String? errorMessage;
|
||||
}
|
||||
|
||||
@freezed
|
||||
abstract class Category with _$Category {
|
||||
const factory Category({
|
||||
required String name,
|
||||
required String description,
|
||||
required String? image,
|
||||
required String label,
|
||||
}) = _Category;
|
||||
|
||||
factory Category.fromJson(Map<String, dynamic> json) =>
|
||||
_$CategoryFromJson(json);
|
||||
}
|
||||
|
||||
var logger = Logger(printer: PrettyPrinter());
|
||||
|
||||
class HomeController extends ChangeNotifier {
|
||||
|
||||
final HomeScreenModel model = HomeScreenModel();
|
||||
final Http http = Http();
|
||||
|
||||
Future<void> getCategories() async {
|
||||
// activate skeletonizer
|
||||
model.categories = getFakeCategoryData();
|
||||
// Simulate fetching categories
|
||||
try {
|
||||
model.isLoading = true;
|
||||
List<dynamic> response = await http.get('/categories');
|
||||
model.categories = response.map((e) => Category.fromJson(e)).toList();
|
||||
} on DioException catch (e) {
|
||||
logger.e(e);
|
||||
model.errorMessage = e.type.name;
|
||||
model.categories = [];
|
||||
}
|
||||
|
||||
model.isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// required by skeletonizer
|
||||
List<Category> getFakeCategoryData() {
|
||||
return [
|
||||
Category(
|
||||
name: 'Electricity',
|
||||
description: 'Pay your electricity bills easily',
|
||||
image: 'assets/images/electricity.png',
|
||||
label: 'Electricity',
|
||||
),
|
||||
Category(
|
||||
name: 'Internet',
|
||||
description: 'Pay your internet bills easily',
|
||||
image: 'assets/images/internet.png',
|
||||
label: 'Internet',
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
157
lib/screens/home/home_controller.freezed.dart
Normal file
157
lib/screens/home/home_controller.freezed.dart
Normal file
@@ -0,0 +1,157 @@
|
||||
// dart format width=80
|
||||
// coverage:ignore-file
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
|
||||
|
||||
part of 'home_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// FreezedGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// dart format off
|
||||
T _$identity<T>(T value) => value;
|
||||
|
||||
/// @nodoc
|
||||
mixin _$Category {
|
||||
|
||||
String get name; String get description; String? get image; String get label;
|
||||
/// Create a copy of Category
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@pragma('vm:prefer-inline')
|
||||
$CategoryCopyWith<Category> get copyWith => _$CategoryCopyWithImpl<Category>(this as Category, _$identity);
|
||||
|
||||
/// Serializes this Category to a JSON map.
|
||||
Map<String, dynamic> toJson();
|
||||
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is Category&&(identical(other.name, name) || other.name == name)&&(identical(other.description, description) || other.description == description)&&(identical(other.image, image) || other.image == image)&&(identical(other.label, label) || other.label == label));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType,name,description,image,label);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Category(name: $name, description: $description, image: $image, label: $label)';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract mixin class $CategoryCopyWith<$Res> {
|
||||
factory $CategoryCopyWith(Category value, $Res Function(Category) _then) = _$CategoryCopyWithImpl;
|
||||
@useResult
|
||||
$Res call({
|
||||
String name, String description, String? image, String label
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
/// @nodoc
|
||||
class _$CategoryCopyWithImpl<$Res>
|
||||
implements $CategoryCopyWith<$Res> {
|
||||
_$CategoryCopyWithImpl(this._self, this._then);
|
||||
|
||||
final Category _self;
|
||||
final $Res Function(Category) _then;
|
||||
|
||||
/// Create a copy of Category
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline') @override $Res call({Object? name = null,Object? description = null,Object? image = freezed,Object? label = null,}) {
|
||||
return _then(_self.copyWith(
|
||||
name: null == name ? _self.name : name // ignore: cast_nullable_to_non_nullable
|
||||
as String,description: null == description ? _self.description : description // ignore: cast_nullable_to_non_nullable
|
||||
as String,image: freezed == image ? _self.image : image // ignore: cast_nullable_to_non_nullable
|
||||
as String?,label: null == label ? _self.label : label // ignore: cast_nullable_to_non_nullable
|
||||
as String,
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// @nodoc
|
||||
@JsonSerializable()
|
||||
|
||||
class _Category implements Category {
|
||||
const _Category({required this.name, required this.description, required this.image, required this.label});
|
||||
factory _Category.fromJson(Map<String, dynamic> json) => _$CategoryFromJson(json);
|
||||
|
||||
@override final String name;
|
||||
@override final String description;
|
||||
@override final String? image;
|
||||
@override final String label;
|
||||
|
||||
/// Create a copy of Category
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override @JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@pragma('vm:prefer-inline')
|
||||
_$CategoryCopyWith<_Category> get copyWith => __$CategoryCopyWithImpl<_Category>(this, _$identity);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return _$CategoryToJson(this, );
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is _Category&&(identical(other.name, name) || other.name == name)&&(identical(other.description, description) || other.description == description)&&(identical(other.image, image) || other.image == image)&&(identical(other.label, label) || other.label == label));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType,name,description,image,label);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Category(name: $name, description: $description, image: $image, label: $label)';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract mixin class _$CategoryCopyWith<$Res> implements $CategoryCopyWith<$Res> {
|
||||
factory _$CategoryCopyWith(_Category value, $Res Function(_Category) _then) = __$CategoryCopyWithImpl;
|
||||
@override @useResult
|
||||
$Res call({
|
||||
String name, String description, String? image, String label
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
/// @nodoc
|
||||
class __$CategoryCopyWithImpl<$Res>
|
||||
implements _$CategoryCopyWith<$Res> {
|
||||
__$CategoryCopyWithImpl(this._self, this._then);
|
||||
|
||||
final _Category _self;
|
||||
final $Res Function(_Category) _then;
|
||||
|
||||
/// Create a copy of Category
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override @pragma('vm:prefer-inline') $Res call({Object? name = null,Object? description = null,Object? image = freezed,Object? label = null,}) {
|
||||
return _then(_Category(
|
||||
name: null == name ? _self.name : name // ignore: cast_nullable_to_non_nullable
|
||||
as String,description: null == description ? _self.description : description // ignore: cast_nullable_to_non_nullable
|
||||
as String,image: freezed == image ? _self.image : image // ignore: cast_nullable_to_non_nullable
|
||||
as String?,label: null == label ? _self.label : label // ignore: cast_nullable_to_non_nullable
|
||||
as String,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// dart format on
|
||||
21
lib/screens/home/home_controller.g.dart
Normal file
21
lib/screens/home/home_controller.g.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'home_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
_Category _$CategoryFromJson(Map<String, dynamic> json) => _Category(
|
||||
name: json['name'] as String,
|
||||
description: json['description'] as String,
|
||||
image: json['image'] as String?,
|
||||
label: json['label'] as String,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$CategoryToJson(_Category instance) => <String, dynamic>{
|
||||
'name': instance.name,
|
||||
'description': instance.description,
|
||||
'image': instance.image,
|
||||
'label': instance.label,
|
||||
};
|
||||
328
lib/screens/home/home_screen.dart
Normal file
328
lib/screens/home/home_screen.dart
Normal file
@@ -0,0 +1,328 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:qpay/screens/home/home_controller.dart';
|
||||
import 'package:skeletonizer/skeletonizer.dart';
|
||||
import 'dart:async';
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
State<HomeScreen> createState() => _HomeScreenState();
|
||||
}
|
||||
|
||||
class _HomeScreenState extends State<HomeScreen>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late AnimationController _controller;
|
||||
late final Animation<AlignmentGeometry> _alignAnimation;
|
||||
final List<Animation<Offset>> _animations = [];
|
||||
final List<Animation<Offset>> _alignListAnimations = [];
|
||||
|
||||
late final bool _enabled = true;
|
||||
late bool _loading = true;
|
||||
Timer? _loadingTimer;
|
||||
|
||||
bool get _isLoggedIn => true;
|
||||
|
||||
final HomeController homeController = HomeController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
homeController.getCategories();
|
||||
_controller = AnimationController(
|
||||
duration: const Duration(milliseconds: 600),
|
||||
vsync: this,
|
||||
)..forward();
|
||||
|
||||
_alignAnimation = Tween<AlignmentGeometry>(
|
||||
begin: Alignment.centerLeft,
|
||||
end: Alignment.center,
|
||||
).animate(CurvedAnimation(parent: _controller, curve: Curves.easeOut));
|
||||
|
||||
List tranListIndices = [0, 1];
|
||||
for (int i = 0; i < tranListIndices.length; i++) {
|
||||
_alignListAnimations.add(
|
||||
Tween<Offset>(begin: Offset(0, -0.25), end: Offset.zero).animate(
|
||||
CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: Interval(0.075 * i, 1.0, curve: Curves.easeOut),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List buttonIndices = [0, 1, 2];
|
||||
for (int i = 0; i < buttonIndices.length; i++) {
|
||||
_animations.add(
|
||||
Tween<Offset>(begin: Offset(-0.15, 0), end: Offset.zero).animate(
|
||||
CurvedAnimation(
|
||||
parent: _controller,
|
||||
curve: Interval(0.075 * i, 1.0, curve: Curves.easeOut),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Simulate a delay to mimic loading state
|
||||
_loadingTimer = Timer(const Duration(seconds: 1), () {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _showErrorSnackBar(String? message) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(message.toString()),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
int _getCategoryIndex(String name) {
|
||||
return homeController.model.categories.indexWhere((e) => e.name == name);
|
||||
}
|
||||
|
||||
bool _isCategoryOdd(String name) {
|
||||
return _getCategoryIndex(name) % 2 != 0;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_loadingTimer?.cancel();
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final VoidCallback? onPressed = _enabled ? () {} : null;
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: CustomScrollView(
|
||||
slivers: [
|
||||
SliverAppBar(
|
||||
floating: true,
|
||||
title: Image(image: AssetImage('assets/icon.png'), width: 85),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.notifications_outlined),
|
||||
onPressed: () {},
|
||||
),
|
||||
],
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (_isLoggedIn)
|
||||
AlignTransition(
|
||||
alignment: _alignAnimation,
|
||||
child: Skeletonizer(
|
||||
enabled: _loading,
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
"Vusumuzi Khoza",
|
||||
style: TextStyle(fontSize: 15),
|
||||
),
|
||||
Text(
|
||||
"USD 4.02",
|
||||
style: TextStyle(
|
||||
fontSize: 30,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
ListenableBuilder(
|
||||
listenable: homeController,
|
||||
builder: (context, child) {
|
||||
if (homeController.model.errorMessage != null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_showErrorSnackBar(
|
||||
homeController.model.errorMessage,
|
||||
);
|
||||
});
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
...homeController.model.categories.map(
|
||||
(e) => Skeletonizer(
|
||||
enabled: homeController.model.isLoading,
|
||||
child: SlideTransition(
|
||||
position: _animations[0],
|
||||
child: FilledButton(
|
||||
style: ButtonStyle(
|
||||
backgroundColor:
|
||||
WidgetStateProperty.all(
|
||||
_isCategoryOdd(e.name)
|
||||
? Theme.of(context)
|
||||
.colorScheme
|
||||
.secondary
|
||||
.withValues(alpha: 0.9)
|
||||
: Theme.of(context)
|
||||
.colorScheme
|
||||
.primary
|
||||
.withValues(alpha: 0.9),
|
||||
),
|
||||
padding: WidgetStateProperty.all(
|
||||
const EdgeInsets.symmetric(
|
||||
vertical: 17.5,
|
||||
horizontal: 30,
|
||||
),
|
||||
),
|
||||
),
|
||||
onPressed: onPressed,
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.electric_bolt,
|
||||
color: Colors.white,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
e.name,
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
homeController.model.categories.isNotEmpty
|
||||
? const SizedBox(height: 30)
|
||||
: SizedBox(),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text(
|
||||
'Recent Activity',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
context.go('/history');
|
||||
},
|
||||
child: const Text(
|
||||
'View All',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.normal,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
Skeletonizer(
|
||||
enabled: _loading,
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
children: [
|
||||
SlideTransition(
|
||||
position: _alignListAnimations[0],
|
||||
child: ListTile(
|
||||
leading: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.primary.withValues(alpha: 0.1),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
Icons.electric_bolt,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
'Electricity Bill',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Text(
|
||||
'Paid USD 50.00',
|
||||
style: TextStyle(fontWeight: FontWeight.normal),
|
||||
),
|
||||
trailing: Text(
|
||||
'-USD 50.00',
|
||||
style: TextStyle(
|
||||
color: Colors.red,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SlideTransition(
|
||||
position: _alignListAnimations[1],
|
||||
child: ListTile(
|
||||
leading: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.primary.withValues(alpha: 0.1),
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
child: Icon(
|
||||
Icons.phone_android,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
title: Text(
|
||||
'Airtime Purchase',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Text(
|
||||
'Purchased USD 10.00',
|
||||
style: TextStyle(fontWeight: FontWeight.normal),
|
||||
),
|
||||
trailing: Text(
|
||||
'-USD 10.00',
|
||||
style: TextStyle(
|
||||
color: Colors.red,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user