From 2b5f8ca701ad6dbc8e57583914d75c8ae47304f6 Mon Sep 17 00:00:00 2001 From: Mrigank Anand Date: Sun, 31 Jan 2021 00:04:25 +0530 Subject: [PATCH 1/2] Adding code for -if username is not added, open username add sheet and then go to home. -If username is already added, then directly open home. --- lib/main.dart | 53 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 264e222a..1ec832de 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,6 +5,7 @@ import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:hive/hive.dart'; import 'package:flutter/services.dart'; import 'package:hive_flutter/hive_flutter.dart'; +import 'controllers/global_setting.dart'; // Files import './controllers/activity.dart'; @@ -44,21 +45,41 @@ Future hiveInitialize() async { class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return ValueListenableBuilder( - valueListenable: Hive.box(globalSettingBoxName).listenable(), - builder: (context, box, widget) { - return c.CupertinoApp( - initialRoute: '/username_add', - title: "StarBook", - theme: c.CupertinoThemeData(brightness: brightness), - localizationsDelegates: [ - DefaultMaterialLocalizations.delegate, - c.DefaultCupertinoLocalizations.delegate, - DefaultWidgetsLocalizations.delegate, - ], - onGenerateRoute: (settings) => RouteGenerator.mainRoute(settings), - ); - }, - ); + User user = GlobalSettingController.getuser(); + if (user != null && user.name.isNotEmpty) { + return ValueListenableBuilder( + valueListenable: Hive.box(globalSettingBoxName).listenable(), + builder: (context, box, widget) { + return c.CupertinoApp( + initialRoute: '/home', + title: "StarBook", + theme: c.CupertinoThemeData(brightness: brightness), + localizationsDelegates: [ + DefaultMaterialLocalizations.delegate, + c.DefaultCupertinoLocalizations.delegate, + DefaultWidgetsLocalizations.delegate, + ], + onGenerateRoute: (settings) => RouteGenerator.mainRoute(settings), + ); + }, + ); + } else { + return ValueListenableBuilder( + valueListenable: Hive.box(globalSettingBoxName).listenable(), + builder: (context, box, widget) { + return c.CupertinoApp( + initialRoute: '/username_add', + title: "StarBook", + theme: c.CupertinoThemeData(brightness: brightness), + localizationsDelegates: [ + DefaultMaterialLocalizations.delegate, + c.DefaultCupertinoLocalizations.delegate, + DefaultWidgetsLocalizations.delegate, + ], + onGenerateRoute: (settings) => RouteGenerator.mainRoute(settings), + ); + }, + ); + } } } From b94000fe7c1ce0d3fc181f2649f9e9574af1667f Mon Sep 17 00:00:00 2001 From: Mrigank Anand Date: Sun, 31 Jan 2021 00:57:28 +0530 Subject: [PATCH 2/2] Added feature to share daily activity. --- lib/screens/activity_page.dart | 157 ++++++++++++++++++++------------- lib/utils/share.dart | 22 +++++ pubspec.lock | 7 ++ pubspec.yaml | 4 + 4 files changed, 130 insertions(+), 60 deletions(-) create mode 100644 lib/utils/share.dart diff --git a/lib/screens/activity_page.dart b/lib/screens/activity_page.dart index 7a1ef819..ce322300 100644 --- a/lib/screens/activity_page.dart +++ b/lib/screens/activity_page.dart @@ -7,6 +7,7 @@ import '../models/activity.dart'; import '../models/mood.dart'; import '../styles/style.dart'; import '../utils/date.dart'; +import '../utils/share.dart'; /// Activity Page Screen widget displays [Activity] class ActivityPage extends StatelessWidget { @@ -14,8 +15,9 @@ class ActivityPage extends StatelessWidget { // will get the mood from list on the basis of moodId. ActivityPage(this.activity) : mood = mMoodList[activity.moodId]; - final Mood mood; final Activity activity; + final Mood mood; + GlobalKey globalKey = GlobalKey(); void onEdit(BuildContext context) async { await Navigator.of(context).popAndPushNamed("/edit", arguments: activity); @@ -23,68 +25,103 @@ class ActivityPage extends StatelessWidget { @override Widget build(BuildContext context) { - return c.CupertinoPageScaffold( - backgroundColor: c.CupertinoDynamicColor.resolve( - c.CupertinoColors.tertiarySystemBackground, - context, - ), - navigationBar: c.CupertinoNavigationBar( - backgroundColor: c.CupertinoDynamicColor.resolve( - c.CupertinoColors.tertiarySystemBackground, context), - middle: Text('Activity'), - trailing: GestureDetector( - onTap: () => onEdit(context), - child: Text( - "Edit", - style: c.CupertinoTheme.of(context).textTheme.navActionTextStyle, + return Builder( + builder: (ctx) => c.RepaintBoundary( + key: globalKey, + child: c.CupertinoPageScaffold( + backgroundColor: c.CupertinoDynamicColor.resolve( + c.CupertinoColors.tertiarySystemBackground, + context, ), - ), - border: null, - ), - child: SingleChildScrollView( - child: SafeArea( - child: Column( - crossAxisAlignment: c.CrossAxisAlignment.start, - children: [ - Container( - padding: EdgeInsets.all(16), - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - "${getMonthTitle(activity.month)} ${activity.day}, ${activity.year}", - style: Style.bodySecondary(context), - ), - ], - ), - ), - Container( - padding: EdgeInsets.all(16), - child: Text( - mood.label, - style: Style.body(context), - ), + navigationBar: c.CupertinoNavigationBar( + backgroundColor: c.CupertinoDynamicColor.resolve( + c.CupertinoColors.tertiarySystemBackground, context), + middle: Text('Activity'), + trailing: GestureDetector( + onTap: () => onEdit(context), + child: Text( + "Edit", + style: + c.CupertinoTheme.of(context).textTheme.navActionTextStyle, ), - Container( - padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16), - child: SelectableText(activity.title, - showCursor: true, - toolbarOptions: - c.ToolbarOptions(copy: true, selectAll: true), - style: Style.largeTitle(context), - enableInteractiveSelection: true), - ), - Container( - padding: EdgeInsets.all(16), - child: SelectableText( - activity.note, - showCursor: true, - toolbarOptions: c.ToolbarOptions(copy: true, selectAll: true), - style: Style.body(context), - enableInteractiveSelection: true, - ), + ), + border: null, + ), + child: SingleChildScrollView( + child: SafeArea( + child: Column( + crossAxisAlignment: c.CrossAxisAlignment.start, + children: [ + Container( + padding: EdgeInsets.all(16), + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + "${getMonthTitle(activity.month)} ${activity.day}, ${activity.year}", + style: Style.bodySecondary(context), + ), + ], + ), + ), + Container( + padding: EdgeInsets.all(16), + child: Text( + mood.label, + style: Style.body(context), + ), + ), + Container( + padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16), + child: SelectableText(activity.title, + showCursor: true, + toolbarOptions: + c.ToolbarOptions(copy: true, selectAll: true), + style: Style.largeTitle(context), + enableInteractiveSelection: true), + ), + Container( + padding: EdgeInsets.all(16), + child: SelectableText( + activity.note, + showCursor: true, + toolbarOptions: + c.ToolbarOptions(copy: true, selectAll: true), + style: Style.body(context), + enableInteractiveSelection: true, + ), + ), + c.Center( + child: c.Padding( + padding: const EdgeInsets.only(left: 30, right: 30), + child: c.CupertinoButton( + onPressed: () => + {convertWidgetToImageAndShare(ctx, globalKey)}, + color: Colors.blue, + borderRadius: new BorderRadius.circular(10.0), + child: c.Row( + mainAxisAlignment: c.MainAxisAlignment.center, + children: [ + Icon( + Icons.share, + color: Colors.white, + ), + c.SizedBox( + width: 20, + ), + new Text( + "Share", + textAlign: TextAlign.center, + style: new TextStyle(color: Colors.white), + ), + ], + ), + ), + ), + ) + ], ), - ], + ), ), ), ), diff --git a/lib/utils/share.dart b/lib/utils/share.dart new file mode 100644 index 00000000..ae1462cb --- /dev/null +++ b/lib/utils/share.dart @@ -0,0 +1,22 @@ +import 'dart:typed_data'; +import 'dart:ui' as ui; + +import 'package:esys_flutter_share/esys_flutter_share.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; + +void convertWidgetToImageAndShare(BuildContext context, containerKey) async { + RenderRepaintBoundary renderRepaintBoundary = + containerKey.currentContext.findRenderObject(); + ui.Image boxImage = await renderRepaintBoundary.toImage(pixelRatio: 1); + ByteData byteData = await boxImage.toByteData(format: ui.ImageByteFormat.png); + Uint8List uInt8List = byteData.buffer.asUint8List(); + try { + await Share.file('Star Book', 'activity.png', uInt8List, 'image/png', + text: + 'See My Diary. I made it with Star Book App. Check it out at Github https://github.com/hashirshoaeb/star_book'); + } catch (e) { + print('error: $e'); + } +} diff --git a/pubspec.lock b/pubspec.lock index fd752916..fcf1737d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -183,6 +183,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.5.0" + esys_flutter_share: + dependency: "direct main" + description: + name: esys_flutter_share + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" fake_async: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index d6bfd7f3..85a8271b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -43,6 +43,10 @@ dependencies: fluttertoast: ^7.1.6 # Blur Hash Functions for Network Images flutter_blurhash: ^0.5.0 + # For sharing + esys_flutter_share: ^1.0.2 + + dev_dependencies: flutter_test: