Refactor home to use constructor injection
[skip ci]
This commit is contained in:
parent
d01289c9c6
commit
0366b087e8
4 changed files with 53 additions and 42 deletions
|
|
@ -1,6 +1,8 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:frosty/screens/home.dart';
|
||||
import 'package:frosty/stores/auth_store.dart';
|
||||
import 'package:frosty/stores/channel_list_store.dart';
|
||||
import 'package:frosty/stores/home_store.dart';
|
||||
import 'package:frosty/stores/settings_store.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
|
|
@ -33,7 +35,10 @@ class MyApp extends StatelessWidget {
|
|||
future: context.read<AuthStore>().init(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.done) {
|
||||
return const Home();
|
||||
return Home(
|
||||
channelListStore: ChannelListStore(authStore: context.read<AuthStore>()),
|
||||
homeStore: HomeStore(),
|
||||
);
|
||||
}
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,21 +1,30 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_mobx/flutter_mobx.dart';
|
||||
import 'package:frosty/screens/settings.dart';
|
||||
import 'package:frosty/stores/auth_store.dart';
|
||||
import 'package:frosty/stores/channel_list_store.dart';
|
||||
import 'package:frosty/stores/home_store.dart';
|
||||
import 'package:frosty/widgets/drawer_menu.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'channel_list.dart';
|
||||
|
||||
class Home extends StatelessWidget {
|
||||
const Home({Key? key}) : super(key: key);
|
||||
class Home extends StatefulWidget {
|
||||
final HomeStore homeStore;
|
||||
final ChannelListStore channelListStore;
|
||||
|
||||
const Home({
|
||||
Key? key,
|
||||
required this.homeStore,
|
||||
required this.channelListStore,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
_HomeState createState() => _HomeState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final homeStore = HomeStore();
|
||||
final auth = context.read<AuthStore>();
|
||||
final channelListStore = ChannelListStore(auth: auth);
|
||||
final titles = [if (auth.isLoggedIn) 'Followed Channels', 'Top Channels', 'Categories'];
|
||||
final titles = [if (context.read<AuthStore>().isLoggedIn) 'Followed Channels', 'Top Channels', 'Categories'];
|
||||
|
||||
debugPrint('build home');
|
||||
return Observer(
|
||||
|
|
@ -23,50 +32,28 @@ class Home extends StatelessWidget {
|
|||
debugPrint('rebuild tab controller');
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(titles[homeStore.selectedIndex]),
|
||||
title: widget.homeStore.search ? const TextField() : Text(titles[widget.homeStore.selectedIndex]),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.search),
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
widget.homeStore.search = !widget.homeStore.search;
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
drawer: Drawer(
|
||||
child: ListView(
|
||||
children: [
|
||||
const DrawerHeader(
|
||||
child: Center(
|
||||
child: Text('Frosty for Twitch'),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.settings),
|
||||
title: const Text('Settings'),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) {
|
||||
return const Settings();
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
drawer: const DrawerMenu(),
|
||||
body: IndexedStack(
|
||||
index: homeStore.selectedIndex,
|
||||
index: widget.homeStore.selectedIndex,
|
||||
children: [
|
||||
if (auth.isLoggedIn)
|
||||
if (context.read<AuthStore>().isLoggedIn)
|
||||
ChannelList(
|
||||
category: ChannelCategory.followed,
|
||||
channelListStore: channelListStore,
|
||||
channelListStore: widget.channelListStore,
|
||||
),
|
||||
ChannelList(
|
||||
category: ChannelCategory.top,
|
||||
channelListStore: channelListStore,
|
||||
channelListStore: widget.channelListStore,
|
||||
),
|
||||
const Center(
|
||||
child: Text('Games'),
|
||||
|
|
@ -76,7 +63,7 @@ class Home extends StatelessWidget {
|
|||
bottomNavigationBar: SafeArea(
|
||||
child: BottomNavigationBar(
|
||||
items: [
|
||||
if (auth.isLoggedIn)
|
||||
if (context.read<AuthStore>().isLoggedIn)
|
||||
const BottomNavigationBarItem(
|
||||
icon: Icon(Icons.favorite),
|
||||
label: 'Followed',
|
||||
|
|
@ -90,8 +77,8 @@ class Home extends StatelessWidget {
|
|||
label: 'Categories',
|
||||
),
|
||||
],
|
||||
currentIndex: homeStore.selectedIndex,
|
||||
onTap: homeStore.handleTap,
|
||||
currentIndex: widget.homeStore.selectedIndex,
|
||||
onTap: widget.homeStore.handleTap,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ part 'home_store.g.dart';
|
|||
class HomeStore = _HomeStoreBase with _$HomeStore;
|
||||
|
||||
abstract class _HomeStoreBase with Store {
|
||||
@observable
|
||||
bool search = false;
|
||||
|
||||
@observable
|
||||
int selectedIndex = 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,21 @@ part of 'home_store.dart';
|
|||
// ignore_for_file: non_constant_identifier_names, unnecessary_brace_in_string_interps, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic
|
||||
|
||||
mixin _$HomeStore on _HomeStoreBase, Store {
|
||||
final _$searchAtom = Atom(name: '_HomeStoreBase.search');
|
||||
|
||||
@override
|
||||
bool get search {
|
||||
_$searchAtom.reportRead();
|
||||
return super.search;
|
||||
}
|
||||
|
||||
@override
|
||||
set search(bool value) {
|
||||
_$searchAtom.reportWrite(value, super.search, () {
|
||||
super.search = value;
|
||||
});
|
||||
}
|
||||
|
||||
final _$selectedIndexAtom = Atom(name: '_HomeStoreBase.selectedIndex');
|
||||
|
||||
@override
|
||||
|
|
@ -41,6 +56,7 @@ mixin _$HomeStore on _HomeStoreBase, Store {
|
|||
@override
|
||||
String toString() {
|
||||
return '''
|
||||
search: ${search},
|
||||
selectedIndex: ${selectedIndex}
|
||||
''';
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue