From 6aac11934f1b094db083aa37deb329ba5e6ab105 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Sat, 8 Oct 2022 18:52:10 -0700 Subject: [PATCH] Open Files / Create New File --- lib/editor.dart | 1 + lib/main.dart | 6 ++-- lib/view.dart | 82 +++++++++++++++++++++++++++++++++++++------- linux/CMakeLists.txt | 4 +-- pubspec.lock | 47 +++++++++++++++++++++++++ pubspec.yaml | 1 + 6 files changed, 123 insertions(+), 18 deletions(-) diff --git a/lib/editor.dart b/lib/editor.dart index 6c455fa..999f096 100644 --- a/lib/editor.dart +++ b/lib/editor.dart @@ -13,6 +13,7 @@ class Editor extends StatefulWidget { class _Editor extends State { @override Widget build(BuildContext context) { + return InputListener(LayoutBuilder(builder: (BuildContext context, BoxConstraints viewportConstraints) { //print("opening document " + path + " " + startLine.toString()); return View(); diff --git a/lib/main.dart b/lib/main.dart index 7e73a0e..3d09704 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,9 +8,9 @@ import 'outline.dart'; void main(List args) { var docfile = LineFile(); - //if (args.length > 1) { - docfile.openFile("./example.md", 0); - //} + if (args.length > 0) { + docfile.openFile(args[0], 0); + } var doc = ChangeNotifierProvider.value(value: docfile); runApp(MultiProvider( diff --git a/lib/view.dart b/lib/view.dart index d43cc49..2d03864 100644 --- a/lib/view.dart +++ b/lib/view.dart @@ -1,6 +1,10 @@ +import 'dart:io'; + +import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:scrollable_positioned_list/scrollable_positioned_list.dart'; +import 'package:subwrite/theme.dart'; import 'file.dart'; import 'line_info.dart'; @@ -34,18 +38,70 @@ class _View extends State with SingleTickerProviderStateMixin { Widget build(BuildContext context) { LineFile doc = Provider.of(context); - return ScrollablePositionedList.builder( - physics: const AlwaysScrollableScrollPhysics(), - itemScrollController: doc.scrollController, - itemCount: doc.lines(), - padding: EdgeInsets.zero, - shrinkWrap: true, - itemBuilder: (BuildContext bcontext, int index) { - return ChangeNotifierProvider.value( - value: doc.backingLines[index], - builder: (lcontext, lineinfo) { - return Provider.of(lcontext).build(context,_animationController); - }); - }); + if (doc.backingLines.isEmpty) { + return Center( + child: Row ( + mainAxisAlignment: MainAxisAlignment.center, + mainAxisSize: MainAxisSize.min, + children: [ + + + ElevatedButton.icon(onPressed: () async { + String? result = await FilePicker.platform.saveFile( + + ); + + if (result != null) { + File(result).createSync(recursive: false); + doc.openFile(result, 0); + doc.focus.requestFocus(); + } else { + // User canceled the picker + } + + + }, icon: Icon(Icons.create, color: foreground), label: const Text("New Document"), + style: ButtonStyle(backgroundColor: MaterialStateProperty.all(tabs), + foregroundColor: MaterialStateProperty.all(foreground))), + + SizedBox(width: 20,), + + ElevatedButton.icon(onPressed: () async { + FilePickerResult? result = await FilePicker.platform.pickFiles( + allowMultiple: false, + type: FileType.custom, + allowedExtensions: ['md','txt'], + ); + if (result != null) { + doc.openFile(result.files.single.path!, 0); + } else { + // User canceled the picker + } + + + }, icon: Icon(Icons.file_open, color: foreground), label: const Text("Open Document"), + style: ButtonStyle(backgroundColor: MaterialStateProperty.all(tabs), + foregroundColor: MaterialStateProperty.all(foreground))), + + ], + ), + ); + + } else { + return ScrollablePositionedList.builder( + physics: const AlwaysScrollableScrollPhysics(), + itemScrollController: doc.scrollController, + itemCount: doc.lines(), + padding: EdgeInsets.zero, + shrinkWrap: true, + itemBuilder: (BuildContext bcontext, int index) { + return ChangeNotifierProvider.value( + value: doc.backingLines[index], + builder: (lcontext, lineinfo) { + return Provider.of(lcontext).build( + context, _animationController); + }); + }); + } } } diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt index 724236f..a8b1427 100644 --- a/linux/CMakeLists.txt +++ b/linux/CMakeLists.txt @@ -4,10 +4,10 @@ project(runner LANGUAGES CXX) # The name of the executable created for the application. Change this to change # the on-disk name of your application. -set(BINARY_NAME "sarahdown") +set(BINARY_NAME "subwrite") # The unique GTK application identifier for this application. See: # https://wiki.gnome.org/HowDoI/ChooseApplicationID -set(APPLICATION_ID "com.example.sarahdown") +set(APPLICATION_ID "tech.resistant.subwrite") # Explicitly opt in to modern CMake behaviors to avoid warnings with recent # versions of CMake. diff --git a/pubspec.lock b/pubspec.lock index 7e313d6..f3918fe 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -50,6 +50,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.1" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + file_picker: + dependency: "direct main" + description: + name: file_picker + url: "https://pub.dartlang.org" + source: hosted + version: "5.2.0+1" flutter: dependency: "direct main" description: flutter @@ -62,11 +76,30 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + flutter_plugin_android_lifecycle: + dependency: transitive + description: + name: flutter_plugin_android_lifecycle + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.7" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.4" lints: dependency: transitive description: @@ -109,6 +142,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.2" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.3" provider: dependency: "direct main" description: @@ -177,6 +217,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" sdks: dart: ">=2.18.0 <3.0.0" flutter: ">=2.12.0" diff --git a/pubspec.yaml b/pubspec.yaml index 537af2a..d9210d3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: cupertino_icons: ^1.0.2 provider: ^6.0.2 scrollable_positioned_list: ^0.3.2 + file_picker: 5.2.0+1 dev_dependencies: flutter_test: