diff --git a/lib/StockCategory.dart b/lib/StockCategory.dart new file mode 100644 index 0000000..a74e74b --- /dev/null +++ b/lib/StockCategory.dart @@ -0,0 +1,19 @@ + +enum StockCategory { + oilgas, + mining, + finance +} + +extension StockCategoryExtension on StockCategory { + String get name { + switch (this) { + case StockCategory.oilgas: + return 'Нефтегазовая отрасль'; + case StockCategory.mining: + return 'Добыча полезных ископаемых'; + case StockCategory.finance: + return 'Банки'; + } + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 2cea2a1..3185b87 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,13 +1,6 @@ -import 'dart:math'; import 'package:flutter/material.dart'; -// Classes + -// Methods + -// Enums + -// Loops + -// Generics (List<>) + -// Anonymous functions + -// Future + -// Extension + +import 'package:fl_chart/fl_chart.dart'; +import 'StockCategory.dart'; void main() { runApp(const MyApp()); @@ -24,13 +17,14 @@ class MyApp extends StatelessWidget { colorScheme: ColorScheme.fromSeed(seedColor: Colors.red), useMaterial3: true, ), - home: const MyHomePage(title: 'Gerimovich I M'), + home: const MyHomePage(title: 'Gerimovich Ilya Maximovich'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); + final String title; @override @@ -55,39 +49,39 @@ class _MyHomePageState extends State { } } -enum StockCategory { oilgas, finance, mining } -extension StockCategoryExtension on StockCategory { - String get name { - switch (this) { - case StockCategory.oilgas: - return 'Нефтегазовая отрасль'; - case StockCategory.finance: - return 'Банки'; - case StockCategory.mining: - return 'Добыча полезных ископаемых'; - } - } -} - class Stock { - final String name; - final double price; - final String imageUrl; - final StockCategory category; + final String? name; + final double? price; + final String? imageUrl; + final StockCategory? category; Stock({ - required this.name, - required this.price, - required this.imageUrl, - required this.category, + this.name, this.price, this.imageUrl, this.category, }); } -class StocksWidget extends StatelessWidget { +class StocksWidget extends StatefulWidget { const StocksWidget({super.key}); + + @override + State createState() => _StocksWidgetState(); +} + +class _StocksWidgetState extends State { + final List prices = [52.90, 77, 89, 111, 135.43, 157.50, 111.11]; + + Future _fetchNewPrice() async { + await Future.delayed(const Duration(seconds: 1)); + setState(() { + prices.add(prices.last + (prices.last * 0.1)); + //if (prices.length > 5) { + // prices.removeAt(0); + //} + }); + } + @override Widget build(BuildContext context) { - final stocks = [ Stock( name: 'Алроса', @@ -97,21 +91,15 @@ class StocksWidget extends StatelessWidget { category: StockCategory.mining), Stock( name: 'Газпром', - price: 135.43, + price: 135.431111, imageUrl: 'https://data.cbonds.info/organisations_logos/21/21.png', category: StockCategory.oilgas), Stock( name: 'Сбербанк', - price: 257.50, - imageUrl: - 'https://data.cbonds.info/organisations_logos/313/1615279834sberbank__2-01.png', - category: StockCategory.finance), - Stock( - name: 'Сбербанк', - price: 111111111.111111111, imageUrl: 'https://data.cbonds.info/organisations_logos/313/1615279834sberbank__2-01.png', category: StockCategory.finance), + Stock(), ]; return Center( @@ -120,6 +108,32 @@ class StocksWidget extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ ...stocks.map((e) => StockCard(stock: e)), + const SizedBox(height: 20), + ElevatedButton( + onPressed: _fetchNewPrice, + child: const Text('Добавить новую цену'), + ), + const SizedBox(height: 20), + SizedBox( + height: 300, + child: LineChart( + LineChartData( + lineBarsData: [ + LineChartBarData( + spots: prices + .asMap() + .map((index, price) => MapEntry(index.toDouble(), FlSpot(index.toDouble(), price))) + .values + .toList(), + isCurved: true, + color: Colors.blue, + barWidth: 3, + dotData: const FlDotData(show: false), + ), + ], + ), + ), + ), ], ), ), @@ -129,35 +143,36 @@ class StocksWidget extends StatelessWidget { class StockCard extends StatelessWidget { final Stock stock; + const StockCard({super.key, required this.stock}); @override Widget build(BuildContext context) { return Card( - margin: const EdgeInsets.all(8.0), + margin: const EdgeInsets.all(8.5), child: Padding( - padding: const EdgeInsets.all(14.0), + padding: const EdgeInsets.all(20), child: Row( children: [ Image.network( - stock.imageUrl, + stock.imageUrl ?? 'https://img.freepik.com/premium-vector/vector-illustration-about-concept-of-no-result-data-or-document-or-file-not-found_675567-5734.jpg', width: 85, height: 85, ), - const SizedBox(width: 16.0), + const SizedBox(width: 20), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - stock.name, + stock.name ?? '', style: Theme.of(context).textTheme.headlineSmall, ), Text( - '${stock.price.toStringAsFixed(2)} р', + '${stock.price?.toStringAsFixed(2)} р', style: Theme.of(context).textTheme.bodyLarge, ), Text( - stock.category.name, + stock.category?.name ?? '', style: Theme.of(context).textTheme.bodySmall, ), ], @@ -167,4 +182,4 @@ class StockCard extends StatelessWidget { ), ); } -} +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 07514d0..1a77d25 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -49,6 +49,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.8" + equatable: + dependency: transitive + description: + name: equatable + sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2 + url: "https://pub.dev" + source: hosted + version: "2.0.5" fake_async: dependency: transitive description: @@ -57,6 +65,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" + fl_chart: + dependency: "direct main" + description: + name: fl_chart + sha256: "94307bef3a324a0d329d3ab77b2f0c6e5ed739185ffc029ed28c0f9b019ea7ef" + url: "https://pub.dev" + source: hosted + version: "0.69.0" flutter: dependency: "direct main" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index cb839ed..a8a4d82 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,7 +31,7 @@ dependencies: flutter: sdk: flutter - + fl_chart: ^0.69.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8