Skip to content

Commit 457891f

Browse files
author
Isaias Nascimento
committed
save image to devide
1 parent e62bf06 commit 457891f

File tree

8 files changed

+315
-10
lines changed

8 files changed

+315
-10
lines changed

lib/main.dart

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import 'package:features_flutter/provider/greate_places.dart';
2+
import 'package:features_flutter/screen/add_place_screen.dart';
3+
import 'package:features_flutter/screen/place_list_screen.dart';
14
import 'package:flutter/material.dart';
5+
import 'package:provider/provider.dart';
26

37
void main() {
48
runApp(MyApp());
@@ -7,13 +11,19 @@ void main() {
711
class MyApp extends StatelessWidget {
812
@override
913
Widget build(BuildContext context) {
10-
return MaterialApp(
11-
title: 'Features Flutter',
12-
theme: ThemeData(
13-
primarySwatch: Colors.indigo,
14-
accentColor: Colors.amber,
14+
return ChangeNotifierProvider.value(
15+
value: GreatPlaces(),
16+
child: MaterialApp(
17+
title: 'Features Flutter',
18+
theme: ThemeData(
19+
primarySwatch: Colors.indigo,
20+
accentColor: Colors.amber,
21+
),
22+
home: PlaceListScreen(),
23+
routes: {
24+
AddPlaceScreen.routeName: (ctx) => AddPlaceScreen(),
25+
},
1526
),
16-
home: Text('teste'),
1727
);
1828
}
1929
}

lib/models/place.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import 'dart:io';
2+
3+
import 'package:flutter/foundation.dart';
4+
5+
class PlaceLocation {
6+
final double latitude;
7+
final double longitude;
8+
final double address;
9+
10+
PlaceLocation({
11+
@required this.latitude,
12+
@required this.longitude,
13+
this.address,
14+
});
15+
}
16+
17+
class Place {
18+
final String id;
19+
final String title;
20+
final PlaceLocation location;
21+
final File image;
22+
23+
Place({
24+
@required this.id,
25+
@required this.title,
26+
@required this.location,
27+
@required this.image,
28+
});
29+
}

lib/provider/greate_places.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'dart:io';
2+
3+
import 'package:features_flutter/models/place.dart';
4+
import 'package:flutter/foundation.dart';
5+
6+
class GreatPlaces with ChangeNotifier {
7+
List<Place> _item = [];
8+
List<Place> get items => [..._item];
9+
10+
void addPlace(
11+
String title,
12+
File image,
13+
) {
14+
final newPlace = Place(
15+
id: DateTime.now().toString(),
16+
image: image,
17+
title: title,
18+
location: null,
19+
);
20+
_item.add(newPlace);
21+
notifyListeners();
22+
}
23+
}

lib/screen/add_place_screen.dart

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,76 @@
1+
import 'dart:io';
2+
3+
import 'package:features_flutter/provider/greate_places.dart';
4+
import 'package:features_flutter/widgets/image_input.dart';
15
import 'package:flutter/material.dart';
6+
import 'package:provider/provider.dart';
7+
8+
class AddPlaceScreen extends StatefulWidget {
9+
static const routeName = "/add-place";
10+
@override
11+
_AddPlaceScreenState createState() => _AddPlaceScreenState();
12+
}
13+
14+
class _AddPlaceScreenState extends State<AddPlaceScreen> {
15+
final _titleControler = TextEditingController();
16+
File _pickedImage;
17+
void _selectImage(File pickedImage) {
18+
_pickedImage = pickedImage;
19+
}
20+
21+
void _savePlace() {
22+
if (_titleControler.text.isEmpty || _pickedImage == null) {
23+
//show a dialog
24+
return;
25+
}
26+
Provider.of<GreatPlaces>(context, listen: false).addPlace(
27+
_titleControler.text,
28+
_pickedImage,
29+
);
30+
31+
Navigator.of(context).pop();
32+
}
233

3-
class AddPlaceScreen extends StatelessWidget {
434
@override
535
Widget build(BuildContext context) {
6-
return Container();
36+
return Scaffold(
37+
appBar: AppBar(
38+
title: Text('Add a new place'),
39+
actions: <Widget>[],
40+
),
41+
body: Column(
42+
crossAxisAlignment: CrossAxisAlignment.stretch,
43+
children: <Widget>[
44+
Expanded(
45+
child: SingleChildScrollView(
46+
child: Padding(
47+
padding: const EdgeInsets.all(12),
48+
child: Column(
49+
children: <Widget>[
50+
TextField(
51+
decoration: InputDecoration(labelText: 'Title'),
52+
controller: _titleControler,
53+
),
54+
SizedBox(
55+
height: 10,
56+
),
57+
Container(),
58+
ImageInput(_selectImage)
59+
],
60+
),
61+
),
62+
),
63+
),
64+
RaisedButton.icon(
65+
icon: Icon(Icons.adb),
66+
label: Text('Add place'),
67+
onPressed: _savePlace,
68+
elevation: 0,
69+
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
70+
color: Colors.amber,
71+
)
72+
],
73+
),
74+
);
775
}
876
}

lib/screen/place_list_screen.dart

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
1+
import 'package:features_flutter/provider/greate_places.dart';
2+
import 'package:features_flutter/screen/add_place_screen.dart';
13
import 'package:flutter/material.dart';
4+
import 'package:provider/provider.dart';
25

36
class PlaceListScreen extends StatelessWidget {
47
@override
58
Widget build(BuildContext context) {
6-
return Container();
9+
return Scaffold(
10+
appBar: AppBar(
11+
title: Text('Yor places'),
12+
actions: <Widget>[
13+
IconButton(
14+
icon: Icon(Icons.add),
15+
onPressed: () {
16+
Navigator.of(context).pushNamed(AddPlaceScreen.routeName);
17+
},
18+
)
19+
],
20+
),
21+
body: Consumer<GreatPlaces>(
22+
child: Center(
23+
child: Text('start adding some!'),
24+
),
25+
builder: (ctx, value, ch) => value.items.length <= 0
26+
? ch
27+
: ListView.builder(
28+
itemCount: value.items.length,
29+
itemBuilder: (context, index) => ListTile(
30+
leading: CircleAvatar(
31+
backgroundImage: FileImage(value.items[index].image),
32+
),
33+
title: Text(value.items[index].title),
34+
onTap: () {},
35+
),
36+
),
37+
),
38+
);
739
}
840
}

lib/widgets/image_input.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'dart:io';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:image_picker/image_picker.dart';
5+
import 'package:path/path.dart' as path;
6+
import 'package:path_provider/path_provider.dart' as syspasths;
7+
8+
class ImageInput extends StatefulWidget {
9+
final Function onSelectImage;
10+
11+
const ImageInput(this.onSelectImage);
12+
@override
13+
_ImageInputState createState() => _ImageInputState();
14+
}
15+
16+
class _ImageInputState extends State<ImageInput> {
17+
File _storeImage;
18+
Future<void> _takePicture() async {
19+
final imageFile = await ImagePicker.pickImage(
20+
source: ImageSource.camera,
21+
maxWidth: 600,
22+
);
23+
24+
if (imageFile == null) return;
25+
26+
setState(() {
27+
_storeImage = imageFile;
28+
});
29+
final appDir = await syspasths.getApplicationDocumentsDirectory();
30+
final fileName = path.basename(imageFile.path);
31+
final saveImage = await imageFile.copy('${appDir.path}/$fileName');
32+
widget.onSelectImage(saveImage);
33+
}
34+
35+
@override
36+
Widget build(BuildContext context) {
37+
return Row(
38+
children: <Widget>[
39+
Container(
40+
width: 150,
41+
height: 100,
42+
decoration: BoxDecoration(
43+
border: Border.all(width: 1, color: Colors.grey),
44+
),
45+
child: _storeImage != null
46+
? Image.file(
47+
_storeImage,
48+
fit: BoxFit.cover,
49+
width: double.infinity,
50+
)
51+
: Text('No image taken', textAlign: TextAlign.center),
52+
alignment: Alignment.center,
53+
),
54+
SizedBox(width: 10),
55+
Expanded(
56+
child: FlatButton.icon(
57+
icon: Icon(Icons.camera),
58+
label: Text(
59+
'Take Picture',
60+
),
61+
textColor: Theme.of(context).primaryColor,
62+
onPressed: _takePicture,
63+
),
64+
),
65+
],
66+
);
67+
}
68+
}

pubspec.lock

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ packages:
6969
description: flutter
7070
source: sdk
7171
version: "0.0.0"
72+
flutter_plugin_android_lifecycle:
73+
dependency: transitive
74+
description:
75+
name: flutter_plugin_android_lifecycle
76+
url: "https://pub.dartlang.org"
77+
source: hosted
78+
version: "1.0.8"
7279
flutter_test:
7380
dependency: "direct dev"
7481
description: flutter
@@ -81,6 +88,20 @@ packages:
8188
url: "https://pub.dartlang.org"
8289
source: hosted
8390
version: "2.1.12"
91+
image_picker:
92+
dependency: "direct main"
93+
description:
94+
name: image_picker
95+
url: "https://pub.dartlang.org"
96+
source: hosted
97+
version: "0.6.6+4"
98+
image_picker_platform_interface:
99+
dependency: transitive
100+
description:
101+
name: image_picker_platform_interface
102+
url: "https://pub.dartlang.org"
103+
source: hosted
104+
version: "1.0.0"
84105
matcher:
85106
dependency: transitive
86107
description:
@@ -95,20 +116,69 @@ packages:
95116
url: "https://pub.dartlang.org"
96117
source: hosted
97118
version: "1.1.8"
98-
path:
119+
nested:
99120
dependency: transitive
121+
description:
122+
name: nested
123+
url: "https://pub.dartlang.org"
124+
source: hosted
125+
version: "0.0.4"
126+
path:
127+
dependency: "direct main"
100128
description:
101129
name: path
102130
url: "https://pub.dartlang.org"
103131
source: hosted
104132
version: "1.6.4"
133+
path_provider:
134+
dependency: "direct main"
135+
description:
136+
name: path_provider
137+
url: "https://pub.dartlang.org"
138+
source: hosted
139+
version: "1.6.9"
140+
path_provider_macos:
141+
dependency: transitive
142+
description:
143+
name: path_provider_macos
144+
url: "https://pub.dartlang.org"
145+
source: hosted
146+
version: "0.0.4+3"
147+
path_provider_platform_interface:
148+
dependency: transitive
149+
description:
150+
name: path_provider_platform_interface
151+
url: "https://pub.dartlang.org"
152+
source: hosted
153+
version: "1.0.2"
105154
petitparser:
106155
dependency: transitive
107156
description:
108157
name: petitparser
109158
url: "https://pub.dartlang.org"
110159
source: hosted
111160
version: "2.4.0"
161+
platform:
162+
dependency: transitive
163+
description:
164+
name: platform
165+
url: "https://pub.dartlang.org"
166+
source: hosted
167+
version: "2.2.1"
168+
plugin_platform_interface:
169+
dependency: transitive
170+
description:
171+
name: plugin_platform_interface
172+
url: "https://pub.dartlang.org"
173+
source: hosted
174+
version: "1.0.2"
175+
provider:
176+
dependency: "direct main"
177+
description:
178+
name: provider
179+
url: "https://pub.dartlang.org"
180+
source: hosted
181+
version: "4.1.2"
112182
quiver:
113183
dependency: transitive
114184
description:
@@ -186,3 +256,4 @@ packages:
186256
version: "3.6.1"
187257
sdks:
188258
dart: ">=2.7.0 <3.0.0"
259+
flutter: ">=1.16.0 <2.0.0"

0 commit comments

Comments
 (0)