mirror of
https://github.com/git-touch/git-touch
synced 2025-01-31 08:04:51 +01:00
refactor: use provider
This commit is contained in:
parent
49db168f6d
commit
6c6233cbcb
@ -2,7 +2,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:git_touch/screens/repo.dart';
|
||||
import 'package:primer/primer.dart';
|
||||
import 'providers/notification.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:git_touch/models/notification.dart';
|
||||
import 'providers/settings.dart';
|
||||
import 'screens/news.dart';
|
||||
import 'screens/notifications.dart';
|
||||
@ -25,7 +26,7 @@ class _HomeState extends State<Home> {
|
||||
// String login;
|
||||
|
||||
Widget _buildNotificationIcon(BuildContext context) {
|
||||
int count = NotificationProvider.of(context).count;
|
||||
int count = Provider.of<NotificationModel>(context).count;
|
||||
if (count == 0) {
|
||||
return Icon(Icons.notifications_none);
|
||||
}
|
||||
@ -158,19 +159,12 @@ class _HomeState extends State<Home> {
|
||||
|
||||
class App extends StatelessWidget {
|
||||
@override
|
||||
build(context) {
|
||||
return NotificationProvider(
|
||||
child: SettingsProvider(
|
||||
child: DefaultTextStyle(
|
||||
style: TextStyle(color: Colors.black),
|
||||
child: Home(),
|
||||
// theme: ThemeData(
|
||||
// textTheme: TextTheme(
|
||||
// title: TextStyle(color: Colors.red),
|
||||
// ),
|
||||
// ),
|
||||
),
|
||||
),
|
||||
Widget build(BuildContext context) {
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider(builder: (context) => NotificationModel()),
|
||||
],
|
||||
child: SettingsProvider(child: Home()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
11
lib/models/notification.dart
Normal file
11
lib/models/notification.dart
Normal file
@ -0,0 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class NotificationModel with ChangeNotifier {
|
||||
int _count = 0;
|
||||
int get count => _count;
|
||||
|
||||
setCount(int v) {
|
||||
_count = v;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class NotificationProvider extends StatefulWidget {
|
||||
final Widget child;
|
||||
|
||||
NotificationProvider({@required this.child});
|
||||
|
||||
static _NotificationProviderState of(BuildContext context) {
|
||||
return (context.inheritFromWidgetOfExactType(_InheritedNotificationProvider)
|
||||
as _InheritedNotificationProvider)
|
||||
.data;
|
||||
}
|
||||
|
||||
@override
|
||||
_NotificationProviderState createState() => new _NotificationProviderState();
|
||||
}
|
||||
|
||||
class _NotificationProviderState extends State<NotificationProvider> {
|
||||
int count = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void setCount(int value) {
|
||||
setState(() {
|
||||
count = value;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new _InheritedNotificationProvider(
|
||||
data: this,
|
||||
child: widget.child,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _InheritedNotificationProvider extends InheritedWidget {
|
||||
final _NotificationProviderState data;
|
||||
|
||||
_InheritedNotificationProvider({
|
||||
Key key,
|
||||
@required this.data,
|
||||
@required Widget child,
|
||||
}) : super(key: key, child: child);
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(_InheritedNotificationProvider old) => true;
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../scaffolds/refresh_stateless.dart';
|
||||
import '../providers/notification.dart';
|
||||
import 'package:git_touch/models/notification.dart';
|
||||
import '../providers/settings.dart';
|
||||
import '../widgets/notification_item.dart';
|
||||
import '../widgets/list_group.dart';
|
||||
@ -54,7 +55,7 @@ class NotificationScreenState extends State<NotificationScreen> {
|
||||
var ns = items.map((item) => NotificationPayload.fromJson(item)).toList();
|
||||
|
||||
if (index == 0) {
|
||||
NotificationProvider.of(context).setCount(ns.length);
|
||||
Provider.of<NotificationModel>(context).setCount(ns.length);
|
||||
}
|
||||
|
||||
Map<String, NotificationGroup> _groupMap = {};
|
||||
|
@ -31,6 +31,7 @@ dependencies:
|
||||
launch_review: ^2.0.0
|
||||
flutter_highlight: ^0.3.0
|
||||
timeago: ^2.0.18
|
||||
provider: ^3.1.0
|
||||
primer:
|
||||
git:
|
||||
url: git://github.com/pd4d10/primer-flutter
|
||||
|
Loading…
x
Reference in New Issue
Block a user