process 3

This commit is contained in:
ValAnn 2024-10-22 14:53:02 +04:00
parent 491b5b3d26
commit a4e6e9dcf9
4 changed files with 191 additions and 159 deletions

View File

View File

@ -1,6 +1,6 @@
import 'package:card_app/presentation/home_page/home_page.dart';
import 'package:flutter/material.dart';
// Класс приложения
void main() {
runApp(const MyApp());
}
@ -22,161 +22,3 @@ class MyApp extends StatelessWidget {
);
}
}
// Класс домашней страницы
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 List<Word> words = [
Word(word: 'Hello', translation: 'Привет', image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS69a8OWnRueQNykBpuyfbGxl3gt5ct46NOHg&s'),
Word(word: 'Book', translation: 'Книга', image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSfbH7Ckkbzrr3KZlb144GTaAmJnz87U9opGw&s'),
Word(word: 'Cat', translation: 'Кот', image: 'https://upload.wikimedia.org/wikipedia/commons/0/0e/Felis_silvestris_silvestris.jpg'),
];
int currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var word in words)
_Card(
word: word.word,
translation: word.translation,
image: word.image,
),
// Кнопки "Знаю" и "Не знаю"
/* Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Кнопка "Знаю"
ElevatedButton(
onPressed: () {
setState(() {
currentIndex++;
if (currentIndex >= words.length) {
currentIndex = 0;
}
});
},
child: const Text('Знаю'),
),
const SizedBox(width: 20),
// Кнопка "Не знаю"
ElevatedButton(
onPressed: () {
// Переходим к предыдущей карточке
setState(() {
currentIndex--;
if (currentIndex < 0) {
currentIndex = words.length - 1;
}
});
},
child: const Text('Не знаю'),
),
],
),*/
],
),
),
),
);
}
}
// Класс карточки
class _Card extends StatefulWidget {
final String word;
final String translation;
final String image;
const _Card({
required this.word,
required this.translation,
required this.image,
});
@override
State<_Card> createState() => _CardState();
}
class _CardState extends State<_Card> {
bool isKnow = false;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(20),
margin: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.black12,
borderRadius: BorderRadius.circular(10),
),
child: IntrinsicHeight(
child: Column(
children: [
Image.network(widget.image, width: 300),
Text(
widget.word,
style: Theme.of(context).textTheme.headlineLarge,
),
// Перевод слова
Text(
widget.translation,
style: Theme.of(context).textTheme.bodyMedium,
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: GestureDetector(
onTap: () {
setState(() {
isKnow = !isKnow;
});
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
child: isKnow
? const Icon(
Icons.check_circle,
color: Colors.green,
key: ValueKey(0))
: const Icon(
Icons.check_circle_outline,
key: ValueKey(1)),
),
),
),
],
)
],
),
));
}
}
// Класс для слов
class Word {
final String word;
final String translation;
final String image;
Word({required this.word, required this.translation, required this.image});
}

View File

@ -0,0 +1,30 @@
part of 'home_page.dart';
typedef OnKnowCallBack = void Function(String title, bool isKnow)?;
// Класс карточки
class _Card extends StatefulWidget {
final String word;
final String translation;
final String image;
final int mark;
final OnKnowCallBack onKnow;
const _Card({
required this.word,
required this.translation,
required this.image,
this.mark = 0,
this.onKnow,
});
factory _Card.fromData(
_Card data, {
OnKnowCallBack? onKnow,
}) =>
_Card(word: data.word, translation: data.translation, image: data.image, mark: data.mark, onKnow: onKnow);
@override
State<_Card> createState() => _CardState();
}

View File

@ -0,0 +1,160 @@
import 'package:flutter/material.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> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Body());
}
}
class Body extends StatelessWidget {
const Body({super.key});
@override
Widget build(BuildContext context) {
final List<_Card> words = [
_Card(
word: 'Hello',
translation: 'Привет',
image:
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS69a8OWnRueQNykBpuyfbGxl3gt5ct46NOHg&s',
mark: 5,
),
_Card(
word: 'Book',
translation: 'Книга',
image:
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSfbH7Ckkbzrr3KZlb144GTaAmJnz87U9opGw&s',
mark: 3),
_Card(
word: 'Cat',
translation: 'Кот',
image:
'https://upload.wikimedia.org/wikipedia/commons/0/0e/Felis_silvestris_silvestris.jpg'),
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: words
.map((e) => _Card.fromData(
e,
onKnow: (title, isKnow) {
_showSnackBar(context, title, isKnow);
},
)).toList(),
),
),
);
}
void _showSnackBar(BuildContext context, String title, bool isKnow){
WidgetsBinding.instance.addPostFrameCallback((_){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'Now you ${isKnow ? 'know' : 'don\'t know'} word $title',
style: Theme.of(context).textTheme.bodyLarge,),
backgroundColor: Colors.indigoAccent,
duration: const Duration(seconds: 2),
));
});
}
}
class _CardState extends State<_Card> {
bool isKnow = false;
@override
Widget build(BuildContext context) {
return Container(
constraints: const BoxConstraints(minHeight: 140, maxWidth: 240),
padding: const EdgeInsets.all(20),
margin: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.black12,
borderRadius: BorderRadius.circular(10),
),
child: Stack(
children: [
Column(
children: [
Column(
children: [
Image.network(widget.image, width: 300),
Text(
widget.word,
style: Theme.of(context).textTheme.headlineLarge,
),
Text(
widget.translation,
style: Theme.of(context).textTheme.bodyMedium,
),
],
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: GestureDetector(
onTap: () {
setState(() {
isKnow = !isKnow;
});
widget.onKnow?.call(widget.word, isKnow);
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
child: isKnow
? const Icon(Icons.check_circle,
color: Colors.green, key: ValueKey(0))
: const Icon(Icons.check_circle_outline,
key: ValueKey(1)),
),
),
),
),
],
),
Align(
alignment: Alignment.center,
child: Row(children: [
Container(
child: Container(
width: widget.mark * 10,
decoration: const BoxDecoration(
color: Colors.greenAccent,
),
padding: const EdgeInsets.fromLTRB(8, 2, 8, 2),
),
),
Expanded(
child: Container(
decoration: const BoxDecoration(
color: Colors.red,
),
padding: const EdgeInsets.fromLTRB(8, 2, 8, 2),
),
)
]),
)
],
));
}
}