PIbd-31_Belianin_N._N._PMD/lib/home_page/home_page.dart

107 lines
3.5 KiB
Dart
Raw Normal View History

2024-09-20 16:16:06 +04:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pmd/models/card_data.dart';
import 'package:pmd/details_page/details_page.dart';
import 'package:pmd/repositories/api_interface.dart';
2024-09-20 16:48:02 +04:00
import 'package:pmd/repositories/mock_repository.dart';
2024-09-20 16:16:06 +04:00
import '../repositories/potter_repository.dart';
part 'card.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _searchController = TextEditingController();
2024-09-29 17:00:08 +04:00
// final ApiInterface repository = PotterRepository();
final ApiInterface repository = MockRepository();
2024-09-20 16:16:06 +04:00
late Future<List<CardData>?> _data;
@override
void initState() {
super.initState();
_data = repository.loadData(null);
}
void _showSnackBar(BuildContext context, String text) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(text),
duration: const Duration(seconds: 2),
backgroundColor: Colors.black54,
));
});
}
void _navigateToDetailsPage(BuildContext context, CardData data) {
Navigator.push(
context,
CupertinoPageRoute(builder: (context) => DetailsPage(data: data)),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: ListView(
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.only(right: 30, left: 30, top: 20),
child: CupertinoSearchTextField(
controller: _searchController,
onChanged: (search) {
setState(() {
_data = repository.loadData(search);
});
}),
),
FutureBuilder<List<CardData>?>(
future: _data,
builder: (context, snapshot) => SingleChildScrollView(
padding: const EdgeInsets.symmetric(
horizontal: 30,
vertical: 15,
),
child: snapshot.hasData
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: snapshot.data?.map((cardData) {
return Column(
children: [
const SizedBox(height: 20),
_Card.fromData(
cardData,
(String text) =>
_showSnackBar(context, text),
() => _navigateToDetailsPage(
context, cardData),
),
const SizedBox(height: 20)
],
);
}).toList() ??
[])
: const CircularProgressIndicator(),
),
),
],
)
],
),
);
}
}