Pibd-33_Tikhonenkov_Alexey_PMU/lib/main.dart

295 lines
9.2 KiB
Dart
Raw Normal View History

2024-11-07 16:23:34 +04:00
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
2024-11-13 02:38:05 +04:00
@override
2024-11-07 16:23:34 +04:00
Widget build(BuildContext context) {
return MaterialApp(
2024-11-13 02:38:05 +04:00
title: 'Цитаты',
2024-11-07 16:23:34 +04:00
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigo),
useMaterial3: true,
),
2024-11-13 02:38:05 +04:00
home: const MyHomePage(title: 'Цитаты'),
2024-11-07 16:23:34 +04:00
);
}
}
2024-11-13 02:38:05 +04:00
class Quote {
final String text;
final String author;
2024-11-20 01:35:31 +04:00
final String imagePath;
2024-11-27 13:34:01 +04:00
bool isFavorite;
2024-11-20 01:35:31 +04:00
2024-11-27 13:34:01 +04:00
Quote(this.text, this.author, this.imagePath, [this.isFavorite = false]);
void toggleFavorite() {
isFavorite = !isFavorite;
}
2024-11-13 02:38:05 +04:00
}
2024-11-07 16:23:34 +04:00
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
2024-11-20 01:35:31 +04:00
final TextEditingController quoteTextController = TextEditingController();
final TextEditingController quoteAuthorController = TextEditingController();
final List<Quote> _quotes = [];
2024-11-20 01:35:31 +04:00
final String defaultImagePath =
'https://cdn-icons-png.flaticon.com/128/17818/17818874.png';
2024-11-13 02:38:05 +04:00
2024-11-20 01:35:31 +04:00
@override
void initState() {
super.initState();
_quotes.add(Quote(
'Сила воли — это ключ к успеху.',
'Аноним',
'https://cdn-icons-png.flaticon.com/128/17818/17818880.png',
));
_quotes.add(Quote(
'Вера в себя — это первый шаг к победе.',
'Аноним',
'https://cdn-icons-png.flaticon.com/128/17818/17818889.png',
));
_quotes.add(Quote(
'Не бойся быть отличным, бойся быть обычным.',
'Аноним',
'https://cdn-icons-png.flaticon.com/128/17818/17818899.png',
));
}
void _addQuote() {
setState(() {
final newQuote = Quote(
quoteTextController.text.addQuotesIfMissing().capitalize(),
quoteAuthorController.text.capitalize(),
2024-11-27 13:34:01 +04:00
defaultImagePath,
2024-11-20 01:35:31 +04:00
);
_quotes.add(newQuote);
quoteTextController.clear();
quoteAuthorController.clear();
});
}
2024-11-07 16:23:34 +04:00
2024-11-20 01:35:31 +04:00
void _removeQuote(int index) {
setState(() {
_quotes.removeAt(index);
});
2024-11-07 16:23:34 +04:00
}
@override
Widget build(BuildContext context) {
2024-11-13 02:38:05 +04:00
return Scaffold(
2024-11-07 16:23:34 +04:00
appBar: AppBar(
2024-11-20 01:35:31 +04:00
title: const Text('Цитаты'),
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
foregroundColor: Theme.of(context).colorScheme.onPrimaryContainer,
2024-11-07 16:23:34 +04:00
),
2024-11-27 13:34:01 +04:00
body: Column(
2024-11-13 02:38:05 +04:00
children: [
2024-11-27 13:34:01 +04:00
const Padding(
padding: EdgeInsets.all(16.0),
child: Text(
'Список цитат',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.normal),
),
),
Expanded(
child: _quotes.isEmpty
? const Center(
child: Text(
'Нет добавленных цитат. Добавьте первую!',
style: TextStyle(fontSize: 18, color: Colors.grey),
2024-11-20 01:35:31 +04:00
),
2024-11-27 13:34:01 +04:00
)
: ListView.builder(
itemCount: _quotes.length,
itemBuilder: (context, index) {
final quote = _quotes[index];
return Card(
margin: const EdgeInsets.symmetric(
vertical: 8.0,
horizontal: 10.0,
2024-11-20 01:35:31 +04:00
),
2024-11-27 13:34:01 +04:00
child: ListTile(
contentPadding: const EdgeInsets.all(8.0),
leading: SizedBox(
width: 50.0,
child: Image.network(
quote.imagePath,
fit: BoxFit.cover,
errorBuilder: (_, __, ___) =>
const Icon(Icons.error, color: Colors.red),
2024-11-20 01:35:31 +04:00
),
2024-11-27 13:34:01 +04:00
),
title: Text(
quote.text,
style: const TextStyle(
fontSize: 18, fontWeight: FontWeight.w500),
),
subtitle: Text('- ${quote.author}'),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(
quote.isFavorite
? Icons.favorite
: Icons.favorite_border,
color: quote.isFavorite ? Colors.red : null,
2024-11-20 01:35:31 +04:00
),
2024-11-27 13:34:01 +04:00
onPressed: () {
setState(() {
quote.toggleFavorite();
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(quote.isFavorite
? 'Цитата добавлена в избранное'
: 'Цитата удалена из избранного'),
duration: const Duration(seconds: 2),
),
);
},
2024-11-20 01:35:31 +04:00
),
2024-11-27 13:34:01 +04:00
IconButton(
2024-11-20 01:35:31 +04:00
icon: const Icon(Icons.delete),
2024-11-27 13:34:01 +04:00
onPressed: () {
_removeQuote(index);
},
2024-11-20 01:35:31 +04:00
),
2024-11-27 13:34:01 +04:00
],
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
QuoteDetailScreen(quote: quote),
),
);
},
),
);
},
),
2024-11-13 02:38:05 +04:00
),
],
2024-11-07 16:23:34 +04:00
),
floatingActionButton: FloatingActionButton(
2024-11-20 01:35:31 +04:00
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Новая цитата'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: quoteTextController,
decoration: const InputDecoration(
hintText: 'Введите текст цитаты',
),
),
const SizedBox(height: 8),
TextField(
controller: quoteAuthorController,
decoration: const InputDecoration(
hintText: 'Введите автора цитаты',
),
),
],
),
actions: [
TextButton(
onPressed: () {
_addQuote();
Navigator.of(context).pop();
},
child: const Text('Добавить'),
),
],
);
},
);
},
2024-11-13 02:38:05 +04:00
tooltip: 'Добавить цитату',
child: const Icon(Icons.format_quote),
),
2024-11-27 13:34:01 +04:00
//floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
2024-11-13 02:38:05 +04:00
);
}
}
2024-11-27 13:34:01 +04:00
class QuoteDetailScreen extends StatelessWidget {
final Quote quote;
const QuoteDetailScreen({super.key, required this.quote});
@override
Widget build(BuildContext context) {
2024-11-27 13:34:01 +04:00
return Scaffold(
appBar: AppBar(
title: const Text('Детали цитаты'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.network(
quote.imagePath,
height: 150,
errorBuilder: (_, __, ___) =>
const Icon(Icons.error, color: Colors.red),
),
const SizedBox(height: 20),
Text(
quote.text,
style:
const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
const SizedBox(height: 10),
Text(
'- ${quote.author}',
style: const TextStyle(fontSize: 18, color: Colors.grey),
),
],
),
),
),
);
}
}
2024-11-13 02:38:05 +04:00
extension StringExtension on String {
String capitalize() {
return split(' ').map((word) {
if (word.isNotEmpty) {
return '${word[0].toUpperCase()}${word.substring(1).toLowerCase()}';
}
return word;
}).join(' ');
}
2024-11-13 02:38:05 +04:00
String addQuotesIfMissing() {
if (startsWith('\"') && endsWith('\"')) return this;
if (startsWith('\"') && !endsWith('\"')) return '$this\"';
if (endsWith('\"') && !startsWith('\"')) return '\"$this';
2024-11-13 02:38:05 +04:00
return '\"$this\"';
2024-11-07 16:23:34 +04:00
}
}