Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local files - reading and writing #6

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/Images/PMS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/energized.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/exhausted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/happy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/high.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertEnergized.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertExhausted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertHappy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertHigh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertLow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertNormal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertNormalEnergy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertPMS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertSad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertSensitive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertSpotting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/invertTired.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/low.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/normalEnergy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/sad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/sensitive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/spotting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Images/tired.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
1 change: 1 addition & 0 deletions ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
41 changes: 41 additions & 0 deletions ios/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}

def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end

File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
use_frameworks!
use_modular_headers!

flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
end
102 changes: 102 additions & 0 deletions lib/local_files.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//importing packages
import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

void main(List<String> args) {
runApp(
MaterialApp(
title: "Reading and writing files",
home: ReadAndWrite(storage: CounterStorage()),
),
);
}

class CounterStorage {
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();

return directory.path;
}

Future<File> get _localFile async {
final path = await _localPath;
return File('$path/output.txt');
}

Future<int> readData() async {
try {
final file = await _localFile;

// Read the file
final contents = await file.readAsString();

return int.parse(contents);
} catch (e) {
// If encountering an error, return 0
return 0;
}
}

Future<File> writeData(String data) async {
final file = await _localFile;
DateTime now = DateTime.now();
String date = DateTime(now.year, now.month, now.day).toString();

// Write the file
return file.writeAsString("$date: $data");
}
}

class ReadAndWrite extends StatefulWidget {
const ReadAndWrite({super.key, required this.storage});

final CounterStorage storage;

@override
State<ReadAndWrite> createState() => _ReadAndWriteState();
}

class _ReadAndWriteState extends State<ReadAndWrite> {
int _counter = 0;

@override
void initState() {
super.initState();
widget.storage.readData().then((value) {
setState(() {
_counter = value;
});
});
}

Future<File> _processData(String data) {
setState(() {
_counter++;
});

// Write the variable as a string to the file.
return widget.storage.writeData(data);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Reading and Writing Files'),
),
body: Center(
child: Text(
'Button tapped $_counter time${_counter == 1 ? '' : 's'}.',
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => _processData('button name'),
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
224 changes: 129 additions & 95 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,112 +4,146 @@ void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
State<MyApp> createState() => _MyApp();
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;

class _MyApp extends State<MyApp> {
@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Map<String, String> invertImage = {};
Widget build(BuildContext context) {
Color color = Theme.of(context).primaryColor;
Widget customRowOne = _buildCustomButtonRow(
'assets/images/low.png',
'assets/images/invertLow.png',
'assets/images/spotting.png',
'assets/images/invertSpotting.png',
'assets/images/normal.png',
'assets/images/invertNormal.png',
'assets/images/high.png',
'assets/images/invertHigh.png',
"Bleeding");
Widget customRowTwo = _buildCustomButtonRow(
'assets/images/exhausted.png',
'assets/images/invertExhausted.png',
'assets/images/tired.png',
'assets/images/invertTired.png',
'assets/images/normalEnergy.png',
'assets/images/invertNormalEnergy.png',
'assets/images/energized.png',
'assets/images/invertEnergized.png',
"Energy");
Widget customRowThree = _buildCustomButtonRow(
'assets/images/PMS.png',
'assets/images/invertPMS.png',
'assets/images/sensitive.png',
'assets/images/invertSensitive.png',
'assets/images/sad.png',
'assets/images/invertSad.png',
'assets/images/happy.png',
'assets/images/invertHappy.png',
"Mood");

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
return MaterialApp(
title: 'Flutter layout demo',
home: Container(
color: Color.fromARGB(255, 201, 90, 90),
alignment: Alignment.bottomCenter,
child: Container(
height: 615,
width: 600,
color: Colors.white,
child: Column(
children: [customRowOne, customRowTwo, customRowThree]))));
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
bool _isPressed = false;
Widget buildButton(String imageSrc, String invertedImageSrc) {
return Material(
color: Colors.blue,
elevation: 8,
//borderRadius: BorderRadius.circular(28),
shape: const CircleBorder(),
clipBehavior: Clip.antiAliasWithSaveLayer,
child: Container(
margin: const EdgeInsets.only(left: 10, right: 10),
decoration: BoxDecoration(
color: Colors.transparent,
shape: BoxShape.circle,
border: Border.all(
color: Color.fromARGB(255, 236, 139, 132), width: 3),
//borderRadius: BorderRadius.circular(10)
),
child: InkWell(
onTap: () => {
setState(() {
if (_isPressed) {
_isPressed = false;
} else {
_isPressed = true;
}
})
},
child: Ink.image(
image: (_isPressed
? AssetImage(invertedImageSrc)
: AssetImage(imageSrc)),
height: 83,
width: 83,
fit: BoxFit.cover,
))));
}

Container _buildCustomButtonRow(
String imageSrc1,
String invertImageSrc1,
String imageSrc2,
String invertImageSrc2,
String imageSrc3,
String invertImageSrc3,
String imageSrc4,
String invertImageSrc4,
String header) {
return Container(
height: 175,
margin: const EdgeInsets.only(top: 15, bottom: 15),
decoration: const BoxDecoration(
color: Colors.white,
boxShadow: <BoxShadow>[
BoxShadow(
color: Color.fromARGB(135, 77, 76, 76),
blurRadius: 15.0,
offset: Offset(0.0, 0.75))
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
child: Column(children: [
const SizedBox(height: 15),
Material(
child: Text(header,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.bold,
))),
const SizedBox(height: 15),
Container(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
padding:
const EdgeInsets.only(left: 12, right: 12, bottom: 12),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
buildButton(imageSrc1, invertImageSrc1),
buildButton(imageSrc2, invertImageSrc2),
buildButton(imageSrc3, invertImageSrc3),
buildButton(imageSrc4, invertImageSrc4)
],
)))
]));
}
}
1 change: 1 addition & 0 deletions macos/Flutter/Flutter-Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "ephemeral/Flutter-Generated.xcconfig"
1 change: 1 addition & 0 deletions macos/Flutter/Flutter-Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "ephemeral/Flutter-Generated.xcconfig"
Loading