end lab6, but not end lection 6
This commit is contained in:
parent
efbed7104a
commit
aa0c4a7f9d
18
lib/components/utils/debounce.dart
Normal file
18
lib/components/utils/debounce.dart
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
class Debounce {
|
||||||
|
factory Debounce() => _instance;
|
||||||
|
|
||||||
|
Debounce._();
|
||||||
|
static final Debounce _instance = Debounce._();
|
||||||
|
static Timer? _timer;
|
||||||
|
|
||||||
|
static void run(
|
||||||
|
VoidCallback action, {
|
||||||
|
Duration delay = const Duration(milliseconds: 500),
|
||||||
|
}) {
|
||||||
|
_timer?.cancel();
|
||||||
|
_timer = Timer(delay, action);
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmd_lab/presentation/home_page/bloc/bloc.dart';
|
||||||
import 'package:pmd_lab/presentation/home_page/home_page.dart';
|
import 'package:pmd_lab/presentation/home_page/home_page.dart';
|
||||||
|
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -16,7 +19,15 @@ class MyApp extends StatelessWidget {
|
|||||||
// colorScheme: ColorScheme.fromSeed(seedColor: Colors.black), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
// colorScheme: ColorScheme.fromSeed(seedColor: Colors.black), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'Flutter rocks!'),
|
home: RepositoryProvider<PotterRepository>(
|
||||||
|
lazy: true,
|
||||||
|
create: (_) => PotterRepository(),
|
||||||
|
child: BlocProvider<HomeBloc>(
|
||||||
|
lazy: true,
|
||||||
|
create: (context) => HomeBloc(context.read<PotterRepository>()),
|
||||||
|
child: const MyHomePage(title: 'Potter API'),
|
||||||
|
),
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
25
lib/presentation/home_page/bloc/bloc.dart
Normal file
25
lib/presentation/home_page/bloc/bloc.dart
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmd_lab/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:pmd_lab/presentation/home_page/bloc/state.dart';
|
||||||
|
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||||
|
|
||||||
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
|
final PotterRepository repo;
|
||||||
|
|
||||||
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||||||
|
on<HomeLoadDataEvent>(_onLoadData);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||||
|
emit(state.copyWith(isLoading: true));
|
||||||
|
|
||||||
|
final data = await repo.loadData(q: event.search);
|
||||||
|
|
||||||
|
emit(state.copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
data: data,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
9
lib/presentation/home_page/bloc/events.dart
Normal file
9
lib/presentation/home_page/bloc/events.dart
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
class HomeEvent {
|
||||||
|
const HomeEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class HomeLoadDataEvent extends HomeEvent{
|
||||||
|
final String? search;
|
||||||
|
const HomeLoadDataEvent({this.search});
|
||||||
|
}
|
30
lib/presentation/home_page/bloc/state.dart
Normal file
30
lib/presentation/home_page/bloc/state.dart
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:pmd_lab/domain/models/card.dart';
|
||||||
|
|
||||||
|
|
||||||
|
class HomeState extends Equatable {
|
||||||
|
final List<CardData>? data;
|
||||||
|
final bool isLoading;
|
||||||
|
|
||||||
|
const HomeState({
|
||||||
|
this.data,
|
||||||
|
this.isLoading = false,
|
||||||
|
});
|
||||||
|
|
||||||
|
HomeState copyWith({
|
||||||
|
List<CardData>? data,
|
||||||
|
bool? isLoading,
|
||||||
|
}) =>
|
||||||
|
HomeState(
|
||||||
|
data: data ?? this.data,
|
||||||
|
isLoading: isLoading ?? this.isLoading,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
data,
|
||||||
|
isLoading,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmd_lab/components/utils/debounce.dart';
|
||||||
import 'package:pmd_lab/domain/models/card.dart';
|
import 'package:pmd_lab/domain/models/card.dart';
|
||||||
import 'package:pmd_lab/presentation/details_page/details_page.dart';
|
import 'package:pmd_lab/presentation/details_page/details_page.dart';
|
||||||
|
import 'package:pmd_lab/presentation/home_page/bloc/bloc.dart';
|
||||||
|
import 'package:pmd_lab/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:pmd_lab/presentation/home_page/bloc/state.dart';
|
||||||
import 'package:pmd_lab/repositories/api_interface.dart';
|
import 'package:pmd_lab/repositories/api_interface.dart';
|
||||||
import 'package:pmd_lab/repositories/mock_repository.dart';
|
import 'package:pmd_lab/repositories/mock_repository.dart';
|
||||||
import 'package:pmd_lab/repositories/potter_repository.dart';
|
import 'package:pmd_lab/repositories/potter_repository.dart';
|
||||||
@ -80,8 +85,16 @@ class _BodyState extends State<Body> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
|
});
|
||||||
super.initState();
|
super.initState();
|
||||||
data = repo.loadData(q: null);
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose(){
|
||||||
|
searchController.dispose();
|
||||||
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -96,35 +109,35 @@ class _BodyState extends State<Body> {
|
|||||||
child: CupertinoSearchTextField(
|
child: CupertinoSearchTextField(
|
||||||
controller: searchController,
|
controller: searchController,
|
||||||
onChanged: (search) {
|
onChanged: (search) {
|
||||||
setState(() {
|
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
||||||
data = repo.loadData(q: search.isNotEmpty ? search : null);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Expanded(
|
|
||||||
child: FutureBuilder<List<CardData>?>(
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
future: data,
|
builder: (context, state) => state.isLoading
|
||||||
builder: (context, snapshot) =>
|
? const CircularProgressIndicator()
|
||||||
SingleChildScrollView(
|
: Expanded(
|
||||||
padding: const EdgeInsets.only(
|
child: RefreshIndicator(
|
||||||
left: 20, right: 20, top: 20),
|
onRefresh: _onRefresh,
|
||||||
child: snapshot.hasData
|
child: ListView.builder(
|
||||||
? Column(
|
padding: EdgeInsets.zero,
|
||||||
children: snapshot.data?.map((data) {
|
itemCount: state.data?.length ?? 0,
|
||||||
return _Card.fromData(
|
itemBuilder: (context, index) {
|
||||||
|
final data = state.data?[index];
|
||||||
|
return data != null
|
||||||
|
? _Card.fromData(
|
||||||
data,
|
data,
|
||||||
onLike: (text, isLiked) =>
|
onLike: (title, isLiked) => _showSnackBar(context, title, isLiked),
|
||||||
_showSnackBar(context, text, isLiked),
|
|
||||||
onTap: () => _navToDetails(context, data),
|
onTap: () => _navToDetails(context, data),
|
||||||
);
|
|
||||||
}).toList() ??
|
|
||||||
[],
|
|
||||||
)
|
|
||||||
: const CircularProgressIndicator()
|
|
||||||
),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
: const SizedBox.shrink();
|
||||||
|
},
|
||||||
|
), // ListView.builder
|
||||||
|
), // RefreshIndicator
|
||||||
|
), // Expanded
|
||||||
|
), // BlocBuilder
|
||||||
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -152,5 +165,9 @@ class _BodyState extends State<Body> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onRefresh() {
|
||||||
|
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
||||||
|
return Future.value(null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user