From a4e6e9dcf9daa353bb713fd5fce739b826a7979b Mon Sep 17 00:00:00 2001 From: ValAnn Date: Tue, 22 Oct 2024 14:53:02 +0400 Subject: [PATCH] process 3 --- lib/domain/models/card.dart | 0 lib/main.dart | 160 +--------------------- lib/presentation/home_page/card.dart | 30 ++++ lib/presentation/home_page/home_page.dart | 160 ++++++++++++++++++++++ 4 files changed, 191 insertions(+), 159 deletions(-) create mode 100644 lib/domain/models/card.dart create mode 100644 lib/presentation/home_page/card.dart create mode 100644 lib/presentation/home_page/home_page.dart diff --git a/lib/domain/models/card.dart b/lib/domain/models/card.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/main.dart b/lib/main.dart index c5f9b63..0981ad9 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,6 @@ +import 'package:card_app/presentation/home_page/home_page.dart'; import 'package:flutter/material.dart'; -// Класс приложения void main() { runApp(const MyApp()); } @@ -21,162 +21,4 @@ class MyApp extends StatelessWidget { home: const MyHomePage(title: 'Валова Анна Дмитриевна'), ); } -} - -// Класс домашней страницы -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - // Список слов и их переводов - final List 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}); } \ No newline at end of file diff --git a/lib/presentation/home_page/card.dart b/lib/presentation/home_page/card.dart new file mode 100644 index 0000000..018a8ad --- /dev/null +++ b/lib/presentation/home_page/card.dart @@ -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(); + +} + diff --git a/lib/presentation/home_page/home_page.dart b/lib/presentation/home_page/home_page.dart new file mode 100644 index 0000000..72eb21f --- /dev/null +++ b/lib/presentation/home_page/home_page.dart @@ -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 createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + @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), + ), + ) + ]), + ) + ], + )); + } +}