From 493c89bb740fe18c8c058b5343f7ae74b97bd6d9 Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Mon, 15 Feb 2021 11:47:40 +0100 Subject: [PATCH] feat: add bottomnavigationbar --- lib/main.dart | 105 +++++++++++++++++++++++++------------ lib/views/unread_view.dart | 85 ++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 33 deletions(-) create mode 100644 lib/views/unread_view.dart diff --git a/lib/main.dart b/lib/main.dart index 0779dc2..2d77dfb 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,7 @@ import 'dart:html'; import 'package:flutter/material.dart'; import 'package:flutterflux/config.dart'; import 'package:flutterflux/miniflux.dart'; +import 'package:flutterflux/views/unread_view.dart'; import 'package:flutterflux/views/user_input_view.dart'; import 'package:localstorage/localstorage.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -17,7 +18,8 @@ class MyApp extends StatelessWidget { return MaterialApp( title: 'FlutterFlux', theme: ThemeData( - primarySwatch: Colors.blue, + primaryColor: Color(0xff004e98), + accentColor: Color(0xffff6700), ), initialRoute: "/", routes: { @@ -40,6 +42,7 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { + int _selectedNavIndex = 0; final LocalStorage storage = new LocalStorage(Config.IDENTIFIER_LOCALSTORAGE); List unreadPosts = []; @@ -56,6 +59,12 @@ class _MyHomePageState extends State { return true; } + void _onNavItemTapped(int index) { + setState(() { + _selectedNavIndex = index; + }); + } + void _initUnreadPosts() async { var posts = await MinifluxApi.instance.getUnreadPosts(); setState(() { @@ -63,7 +72,7 @@ class _MyHomePageState extends State { }); } - _readArticle(FeedEntry entry) async { + void _readArticle(FeedEntry entry) async { if (await canLaunch(entry.url)) { MinifluxApi.instance.markAsRead(entry.id); launch(entry.url); @@ -83,39 +92,69 @@ class _MyHomePageState extends State { super.initState(); } + static List _widgetOptions = [ + UnreadView( + key: UniqueKey(), + ), + Container( + color: Colors.blue, + ), + Container( + color: Colors.red, + ), + Container( + color: Colors.yellow, + ), + Container( + color: Colors.green, + ), + ]; + @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar( - actions: [ - IconButton( - icon: const Icon(Icons.settings), - tooltip: 'Settings', - onPressed: () { - Navigator.pushNamed(context, "/config"); - }, - ), - ], - title: Text(widget.title), - ), - body: Center( - key: UniqueKey(), - child: ListView.builder( - itemCount: unreadPosts.length, - key: UniqueKey(), - itemBuilder: (BuildContext context, int index) { - FeedEntry entry = unreadPosts[index]; - return InkWell( - child: Card( - child: ListTile( - key: UniqueKey(), - title: Text(entry.title), - ), - ), - onTap: () => _readArticle(entry), - ); - }, - )), - ); + appBar: AppBar( + foregroundColor: Theme.of(context).colorScheme.primary, + actions: [ + IconButton( + icon: const Icon(Icons.settings), + tooltip: 'Settings', + onPressed: () { + Navigator.pushNamed(context, "/config"); + }, + ), + ], + title: Text(widget.title), + ), + bottomNavigationBar: BottomNavigationBar( + items: [ + BottomNavigationBarItem( + icon: Icon(Icons.new_releases_outlined), + label: 'Unread', + ), + BottomNavigationBarItem( + icon: Icon(Icons.star), + label: 'Starred', + ), + BottomNavigationBarItem( + icon: Icon(Icons.history), + label: 'History', + ), + BottomNavigationBarItem( + icon: Icon(Icons.article), + label: 'Feeds', + ), + BottomNavigationBarItem( + icon: Icon(Icons.category), + label: 'Categories', + ), + ], + currentIndex: _selectedNavIndex, + onTap: _onNavItemTapped, + backgroundColor: Colors.black, + unselectedItemColor: Theme.of(context).colorScheme.secondaryVariant, + selectedItemColor: Theme.of(context).colorScheme.secondary, + ), + body: _widgetOptions[_selectedNavIndex]); } } diff --git a/lib/views/unread_view.dart b/lib/views/unread_view.dart new file mode 100644 index 0000000..cda6aaf --- /dev/null +++ b/lib/views/unread_view.dart @@ -0,0 +1,85 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; +import 'package:localstorage/localstorage.dart'; +import 'package:url_launcher/url_launcher.dart'; + +import '../config.dart'; +import '../miniflux.dart'; + +class UnreadView extends StatefulWidget { + UnreadView({Key key}) : super(key: key); + + @override + _UnreadViewState createState() => _UnreadViewState(); +} + +class _UnreadViewState extends State { + final LocalStorage storage = new LocalStorage(Config.IDENTIFIER_LOCALSTORAGE); + List unreadPosts = []; + + /// Check if the user token is set. + /// If that's not the case, redirect to the config page + Future checkConfig() async { + await storage.ready; + dynamic token = await storage.getItem(Config.IDENTIFIER_API_KEY); + + if (token == null || token.isEmpty) { + Navigator.pushNamed(context, "/config"); + return false; + } + return true; + } + + void _initUnreadPosts() async { + var posts = await MinifluxApi.instance.getUnreadPosts(); + setState(() { + unreadPosts = posts; + }); + } + + void _readArticle(FeedEntry entry) async { + if (await canLaunch(entry.url)) { + MinifluxApi.instance.markAsRead(entry.id); + launch(entry.url); + } else { + throw 'Could not launch ${entry.url}'; + } + } + + @override + void initState() { + checkConfig().then((success) { + if (success) { + _initUnreadPosts(); + } + }); + + super.initState(); + } + + static const List _widgetOptions = []; + + @override + Widget build(BuildContext context) { + return Container( + key: UniqueKey(), + child: Center( + child: ListView.builder( + itemCount: unreadPosts.length, + key: UniqueKey(), + itemBuilder: (BuildContext context, int index) { + FeedEntry entry = unreadPosts[index]; + return InkWell( + child: Card( + child: ListTile( + key: UniqueKey(), + title: Text(entry.title), + ), + ), + onTap: () => _readArticle(entry), + ); + }, + ), + )); + } +} -- 2.45.2