From 69d005156bb2335ed41852e7935b6e8c4bc09fff Mon Sep 17 00:00:00 2001 From: omartinma Date: Thu, 8 Feb 2024 10:40:59 +0100 Subject: [PATCH] feat: complete code for training --- analysis_options.yaml | 9 ++- devtools_options.yaml | 1 + lib/basics/basics_screen.dart | 84 +++++++++++++++++++++++++++ lib/main.dart | 12 ++-- lib/sample_app/bloc/sample_bloc.dart | 30 ++++++++++ lib/sample_app/bloc/sample_event.dart | 11 ++++ lib/sample_app/bloc/sample_state.dart | 14 +++++ lib/sample_app/sample_app_screen.dart | 75 ++++++++++++++++++++++++ pubspec.lock | 61 ++++++++++++++----- pubspec.yaml | 6 +- 10 files changed, 282 insertions(+), 21 deletions(-) create mode 100644 devtools_options.yaml create mode 100644 lib/basics/basics_screen.dart create mode 100644 lib/sample_app/bloc/sample_bloc.dart create mode 100644 lib/sample_app/bloc/sample_event.dart create mode 100644 lib/sample_app/bloc/sample_state.dart create mode 100644 lib/sample_app/sample_app_screen.dart diff --git a/analysis_options.yaml b/analysis_options.yaml index f9b3034..8d829b6 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1 +1,8 @@ -include: package:flutter_lints/flutter.yaml +include: package:very_good_analysis/analysis_options.5.1.0.yaml +analyzer: + exclude: + - lib/l10n/* + - lib/generated/* +linter: + rules: + public_member_api_docs: false diff --git a/devtools_options.yaml b/devtools_options.yaml new file mode 100644 index 0000000..7e7e7f6 --- /dev/null +++ b/devtools_options.yaml @@ -0,0 +1 @@ +extensions: diff --git a/lib/basics/basics_screen.dart b/lib/basics/basics_screen.dart new file mode 100644 index 0000000..c6456a2 --- /dev/null +++ b/lib/basics/basics_screen.dart @@ -0,0 +1,84 @@ +// ignore_for_file: avoid_print + +import 'package:flutter/material.dart'; + +class BasicsScreen extends StatelessWidget { + const BasicsScreen({super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('IE Workshop'), + backgroundColor: Colors.red, + actions: [ + IconButton( + onPressed: () {}, + icon: const Icon(Icons.settings), + ), + ], + ), + backgroundColor: Colors.green, + body: SingleChildScrollView( + child: Column( + children: [ + const Text('Hello world!'), + const Text( + 'Hello world with style', + style: TextStyle( + fontSize: 34, + color: Colors.blue, + fontWeight: FontWeight.bold, + ), + ), + const Text( + 'Tooooooooooooooo looooooooooooooooong text', + style: TextStyle( + fontSize: 34, + color: Colors.pink, + fontWeight: FontWeight.bold, + ), + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + Container( + height: 300, + width: 300, + color: Colors.amber, + child: const Text('Hello in a colored box'), + ), + const SizedBox( + height: 15, + ), + Container( + height: 300, + width: 300, + color: Colors.amber, + padding: const EdgeInsets.all(30), + child: const Text('Hello in a colored box'), + ), + Row( + children: [ + TextButton( + onPressed: () { + print('Button 1 pressed'); + }, + child: const Text('Button 1'), + ), + ElevatedButton( + onPressed: () { + print('Button 2 pressed'); + }, + child: const Text('Button 2'), + ), + ], + ), + const SizedBox( + height: 100, + ), + ], + ), + ), + ); + } +} diff --git a/lib/main.dart b/lib/main.dart index a725658..450785b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:ie_workshop/sample_app/bloc/sample_bloc.dart'; +import 'package:ie_workshop/sample_app/sample_app_screen.dart'; void main() { runApp(const MainApp()); @@ -9,11 +12,10 @@ class MainApp extends StatelessWidget { @override Widget build(BuildContext context) { - return const MaterialApp( - home: Scaffold( - body: Center( - child: Text('Hello World!'), - ), + return MaterialApp( + home: BlocProvider( + create: (context) => SampleBloc()..add(const FetchData()), + child: const SampleAppScreen(), ), ); } diff --git a/lib/sample_app/bloc/sample_bloc.dart b/lib/sample_app/bloc/sample_bloc.dart new file mode 100644 index 0000000..7a3a7cd --- /dev/null +++ b/lib/sample_app/bloc/sample_bloc.dart @@ -0,0 +1,30 @@ +import 'dart:async'; + +import 'package:bloc/bloc.dart'; +import 'package:equatable/equatable.dart'; +import 'package:ie_workshop/sample_app/sample_app_screen.dart'; +import 'package:meta/meta.dart'; + +part 'sample_event.dart'; +part 'sample_state.dart'; + +class SampleBloc extends Bloc { + SampleBloc() : super(SampleInitial()) { + on(_fetchData); + } + + FutureOr _fetchData(FetchData event, Emitter emit) async { + await Future.delayed(const Duration(seconds: 1)); + final list = []; + for (var i = 0; i < 5; i++) { + list.add( + MyItem( + name: 'Name $i', + description: 'Description $i', + url: 'https://picsum.photos/id/$i/200', + ), + ); + } + emit(SampleSucces(items: list)); + } +} diff --git a/lib/sample_app/bloc/sample_event.dart b/lib/sample_app/bloc/sample_event.dart new file mode 100644 index 0000000..b0cc7bf --- /dev/null +++ b/lib/sample_app/bloc/sample_event.dart @@ -0,0 +1,11 @@ +part of 'sample_bloc.dart'; + +class SampleEvent extends Equatable { + const SampleEvent(); + @override + List get props => []; +} + +class FetchData extends SampleEvent { + const FetchData(); +} diff --git a/lib/sample_app/bloc/sample_state.dart b/lib/sample_app/bloc/sample_state.dart new file mode 100644 index 0000000..15d276f --- /dev/null +++ b/lib/sample_app/bloc/sample_state.dart @@ -0,0 +1,14 @@ +part of 'sample_bloc.dart'; + +@immutable +sealed class SampleState {} + +final class SampleInitial extends SampleState {} + +final class SampleLoading extends SampleState {} + +final class SampleSucces extends SampleState { + SampleSucces({required this.items}); + + final List items; +} diff --git a/lib/sample_app/sample_app_screen.dart b/lib/sample_app/sample_app_screen.dart new file mode 100644 index 0000000..20a3783 --- /dev/null +++ b/lib/sample_app/sample_app_screen.dart @@ -0,0 +1,75 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:ie_workshop/sample_app/bloc/sample_bloc.dart'; + +class MyItem { + MyItem({ + required this.name, + required this.description, + required this.url, + }); + + final String name; + final String description; + final String url; +} + +class SampleAppScreen extends StatelessWidget { + const SampleAppScreen({super.key}); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('My first app'), + ), + body: BlocBuilder( + builder: (context, state) { + if (state is SampleSucces) { + return ListView.builder( + itemBuilder: (context, index) { + final item = state.items[index]; + return ListTile( + title: Text(item.name, style: const TextStyle(fontSize: 30)), + subtitle: Text( + item.description, + style: const TextStyle(fontSize: 24), + ), + leading: Image.network(item.url), + trailing: const Icon(Icons.account_tree_sharp), + onTap: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) { + return DetailsScreen(item: item); + }, + ), + ); + }, + ); + }, + itemCount: state.items.length, + ); + } + return const Center(child: CircularProgressIndicator()); + }, + ), + ); + } +} + +class DetailsScreen extends StatelessWidget { + const DetailsScreen({required this.item, super.key}); + + final MyItem item; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(item.name), + ), + body: Text(item.description, style: const TextStyle(fontSize: 30)), + ); + } +} diff --git a/pubspec.lock b/pubspec.lock index dae426a..af0c47e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -9,6 +9,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.11.0" + bloc: + dependency: "direct main" + description: + name: bloc + sha256: "3820f15f502372d979121de1f6b97bfcf1630ebff8fe1d52fb2b0bfa49be5b49" + url: "https://pub.dev" + source: hosted + version: "8.1.2" boolean_selector: dependency: transitive description: @@ -41,6 +49,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.18.0" + equatable: + dependency: "direct main" + description: + name: equatable + sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2 + url: "https://pub.dev" + source: hosted + version: "2.0.5" fake_async: dependency: transitive description: @@ -54,27 +70,19 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_lints: - dependency: "direct dev" + flutter_bloc: + dependency: "direct main" description: - name: flutter_lints - sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04 + name: flutter_bloc + sha256: e74efb89ee6945bcbce74a5b3a5a3376b088e5f21f55c263fc38cbdc6237faae url: "https://pub.dev" source: hosted - version: "2.0.3" + version: "8.1.3" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" - lints: - dependency: transitive - description: - name: lints - sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452" - url: "https://pub.dev" - source: hosted - version: "2.1.1" matcher: dependency: transitive description: @@ -92,13 +100,21 @@ packages: source: hosted version: "0.5.0" meta: - dependency: transitive + dependency: "direct main" description: name: meta sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e url: "https://pub.dev" source: hosted version: "1.10.0" + nested: + dependency: transitive + description: + name: nested + sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20" + url: "https://pub.dev" + source: hosted + version: "1.0.0" path: dependency: transitive description: @@ -107,6 +123,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" + provider: + dependency: transitive + description: + name: provider + sha256: "9a96a0a19b594dbc5bf0f1f27d2bc67d5f95957359b461cd9feb44ed6ae75096" + url: "https://pub.dev" + source: hosted + version: "6.1.1" sky_engine: dependency: transitive description: flutter @@ -168,6 +192,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + very_good_analysis: + dependency: "direct dev" + description: + name: very_good_analysis + sha256: "9ae7f3a3bd5764fb021b335ca28a34f040cd0ab6eec00a1b213b445dae58a4b8" + url: "https://pub.dev" + source: hosted + version: "5.1.0" web: dependency: transitive description: @@ -178,3 +210,4 @@ packages: version: "0.3.0" sdks: dart: ">=3.2.4 <4.0.0" + flutter: ">=1.16.0" diff --git a/pubspec.yaml b/pubspec.yaml index 62938e1..94d7b10 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,13 +7,17 @@ environment: sdk: '>=3.2.4 <4.0.0' dependencies: + bloc: ^8.1.2 + equatable: flutter: sdk: flutter + flutter_bloc: ^8.1.3 + meta: ^1.10.0 dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^2.0.0 + very_good_analysis: ^5.1.0 flutter: uses-material-design: true