diff --git a/calorie_go_flutter/lib/pages/home/home_page.dart b/calorie_go_flutter/lib/pages/home/home_page.dart index c6075f2..1f6ca5c 100644 --- a/calorie_go_flutter/lib/pages/home/home_page.dart +++ b/calorie_go_flutter/lib/pages/home/home_page.dart @@ -1,5 +1,5 @@ import 'package:calorie_go_flutter/components/bottom_app_bar.dart'; -import 'package:calorie_go_flutter/gen/assets.gen.dart'; +import 'package:calorie_go_flutter/pages/home/home_page_controller.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; @@ -9,6 +9,17 @@ class HomePage extends HookConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { + final state = ref.watch(homePageControllerProvider); + final monster = state.monster; + + // return monster.when(data: (data){ + // return Text(data.monsterImageId.imageUrl); + // }, error: (err, stackTrace){ + // return Text("error"); + // }, loading: (){ + // return Text("loading"); + // }); + return Scaffold( appBar: AppBar( title: const Text('ホーム'), @@ -29,7 +40,14 @@ class HomePage extends HookConsumerWidget { ), ClipRRect( borderRadius: BorderRadius.circular(40.0), - child: Assets.images.sample.sampleImage.image(), + // child: Assets.images.sample.sampleImage.image(), + child: monster.when(data: (data){ + return Image.network(data.monsterImageId?.imageUrl ?? ''); + }, error: (err, stackTrace){ + return const Text("error"); + }, loading: (){ + return const Text("loading"); + }), ), const SizedBox( height: 30, diff --git a/calorie_go_flutter/lib/pages/home/home_page_controller.dart b/calorie_go_flutter/lib/pages/home/home_page_controller.dart index e69de29..9c54471 100644 --- a/calorie_go_flutter/lib/pages/home/home_page_controller.dart +++ b/calorie_go_flutter/lib/pages/home/home_page_controller.dart @@ -0,0 +1,27 @@ +import 'package:calorie_go_flutter/pages/home/home_page_state.dart'; +import 'package:calorie_go_flutter/repositories/monster_repository.dart'; +import 'package:flutter/material.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; + +part 'home_page_controller.g.dart'; + +@riverpod +class HomePageController extends _$HomePageController { + @override + HomePageState build() { + WidgetsBinding.instance.addPostFrameCallback((_) { + getMonster(); + }); + return const HomePageState(); + } + + Future getMonster() async { + try { + final monster = await MonsterRepository().getMonster(); + if(monster == null) throw Exception('モンスターが見つかりませんでした'); + state = state.copyWith(monster: AsyncValue.data(monster)); + } catch (e) { + state = state.copyWith(errorMessage: '$e'); + } + } +} diff --git a/calorie_go_flutter/lib/pages/home/home_page_controller.g.dart b/calorie_go_flutter/lib/pages/home/home_page_controller.g.dart new file mode 100644 index 0000000..45ceaec --- /dev/null +++ b/calorie_go_flutter/lib/pages/home/home_page_controller.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'home_page_controller.dart'; + +// ************************************************************************** +// RiverpodGenerator +// ************************************************************************** + +String _$homePageControllerHash() => + r'7787a70eaaafe53eda5361b8cb8c582378e1f76e'; + +/// See also [HomePageController]. +@ProviderFor(HomePageController) +final homePageControllerProvider = + AutoDisposeNotifierProvider.internal( + HomePageController.new, + name: r'homePageControllerProvider', + debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') + ? null + : _$homePageControllerHash, + dependencies: null, + allTransitiveDependencies: null, +); + +typedef _$HomePageController = AutoDisposeNotifier; +// ignore_for_file: type=lint +// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member diff --git a/calorie_go_flutter/lib/pages/home/home_page_state.dart b/calorie_go_flutter/lib/pages/home/home_page_state.dart index e69de29..cce5ad9 100644 --- a/calorie_go_flutter/lib/pages/home/home_page_state.dart +++ b/calorie_go_flutter/lib/pages/home/home_page_state.dart @@ -0,0 +1,13 @@ +import 'package:calorie_go_client/calorie_go_client.dart'; +import 'package:freezed_annotation/freezed_annotation.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; + +part 'home_page_state.freezed.dart'; + +@freezed +class HomePageState with _$HomePageState { + const factory HomePageState({ + @Default(AsyncValue.loading()) AsyncValue monster, + String? errorMessage, + }) = _HomePageState; +} diff --git a/calorie_go_flutter/lib/pages/home/home_page_state.freezed.dart b/calorie_go_flutter/lib/pages/home/home_page_state.freezed.dart new file mode 100644 index 0000000..7a66544 --- /dev/null +++ b/calorie_go_flutter/lib/pages/home/home_page_state.freezed.dart @@ -0,0 +1,153 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint +// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark + +part of 'home_page_state.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +T _$identity(T value) => value; + +final _privateConstructorUsedError = UnsupportedError( + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models'); + +/// @nodoc +mixin _$HomePageState { + AsyncValue get monster => throw _privateConstructorUsedError; + String? get errorMessage => throw _privateConstructorUsedError; + + @JsonKey(ignore: true) + $HomePageStateCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $HomePageStateCopyWith<$Res> { + factory $HomePageStateCopyWith( + HomePageState value, $Res Function(HomePageState) then) = + _$HomePageStateCopyWithImpl<$Res, HomePageState>; + @useResult + $Res call({AsyncValue monster, String? errorMessage}); +} + +/// @nodoc +class _$HomePageStateCopyWithImpl<$Res, $Val extends HomePageState> + implements $HomePageStateCopyWith<$Res> { + _$HomePageStateCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? monster = null, + Object? errorMessage = freezed, + }) { + return _then(_value.copyWith( + monster: null == monster + ? _value.monster + : monster // ignore: cast_nullable_to_non_nullable + as AsyncValue, + errorMessage: freezed == errorMessage + ? _value.errorMessage + : errorMessage // ignore: cast_nullable_to_non_nullable + as String?, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$HomePageStateImplCopyWith<$Res> + implements $HomePageStateCopyWith<$Res> { + factory _$$HomePageStateImplCopyWith( + _$HomePageStateImpl value, $Res Function(_$HomePageStateImpl) then) = + __$$HomePageStateImplCopyWithImpl<$Res>; + @override + @useResult + $Res call({AsyncValue monster, String? errorMessage}); +} + +/// @nodoc +class __$$HomePageStateImplCopyWithImpl<$Res> + extends _$HomePageStateCopyWithImpl<$Res, _$HomePageStateImpl> + implements _$$HomePageStateImplCopyWith<$Res> { + __$$HomePageStateImplCopyWithImpl( + _$HomePageStateImpl _value, $Res Function(_$HomePageStateImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? monster = null, + Object? errorMessage = freezed, + }) { + return _then(_$HomePageStateImpl( + monster: null == monster + ? _value.monster + : monster // ignore: cast_nullable_to_non_nullable + as AsyncValue, + errorMessage: freezed == errorMessage + ? _value.errorMessage + : errorMessage // ignore: cast_nullable_to_non_nullable + as String?, + )); + } +} + +/// @nodoc + +class _$HomePageStateImpl implements _HomePageState { + const _$HomePageStateImpl( + {this.monster = const AsyncValue.loading(), this.errorMessage}); + + @override + @JsonKey() + final AsyncValue monster; + @override + final String? errorMessage; + + @override + String toString() { + return 'HomePageState(monster: $monster, errorMessage: $errorMessage)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$HomePageStateImpl && + (identical(other.monster, monster) || other.monster == monster) && + (identical(other.errorMessage, errorMessage) || + other.errorMessage == errorMessage)); + } + + @override + int get hashCode => Object.hash(runtimeType, monster, errorMessage); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$HomePageStateImplCopyWith<_$HomePageStateImpl> get copyWith => + __$$HomePageStateImplCopyWithImpl<_$HomePageStateImpl>(this, _$identity); +} + +abstract class _HomePageState implements HomePageState { + const factory _HomePageState( + {final AsyncValue monster, + final String? errorMessage}) = _$HomePageStateImpl; + + @override + AsyncValue get monster; + @override + String? get errorMessage; + @override + @JsonKey(ignore: true) + _$$HomePageStateImplCopyWith<_$HomePageStateImpl> get copyWith => + throw _privateConstructorUsedError; +}