2021年5月7日金曜日

tuyano flutter 326 page(file access) and 316 page (routing)

ファイルIOだから、Webでは実現できない(ファイル書き込みできないから)

よってアンドロイドスタヂオでエミュレータ実施で成功した


 iimport 'package:flutter/material.dart';


import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';

void main() {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
},
);
}
}

class FirstScreen extends StatefulWidget {
FirstScreen({Key key}) : super(key: key);

@override
_FirstScreenState createState() => new _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
final _controller = TextEditingController();
final _fname = 'mydata.txt';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Column(
children: <Widget>[
Text(
'Home Screen',
style: TextStyle(fontSize: 32.0),
),
Padding(
padding: EdgeInsets.all(20.0),
),
TextField(
controller: _controller,
style: TextStyle(fontSize: 32.0),
)
],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 0,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('Save'),
icon: Icon(Icons.save),
),
BottomNavigationBarItem(
title: Text('Load'),
icon: Icon(Icons.open_in_new),
),
],
onTap: (int value) {
switch (value) {
case 0:
saveIt(_controller.text);
setState(() {
_controller.text = '';
});
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("saved!"),
content: Text("save message to file."),
));
break;
case 1:
setState(() {
loadIt().then((String value) {
setState(() {
_controller.text = value;
});
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("loaded!"),
content: Text("load message from file."),
));
});
});
break;
default:
print('no defalut.');
}
},
),
);
}

Future<File> getDataFile(String filename) async {
final directory = await getApplicationDocumentsDirectory();
return File(directory.path + '/' + filename);
}

void saveIt(String value) async {
getDataFile(_fname).then((File file) {
file.writeAsString(value);
});
}

Future<String> loadIt() async {
try {
final file = await getDataFile(_fname);
return file.readAsString();
} catch (e) {
return null;
}
}
}


----------------------------------------------------------

mport 'package:flutter/material.dart';


void main() {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new 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: new FirstScreen(),
);
}
}

class FirstScreen extends StatefulWidget {
FirstScreen({Key key}) : super(key: key); // コンストラクタ。今回は特に処理はない

@override
_FirstScreenState createState() => new _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
final _controller = TextEditingController();
String _input;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Home Screen', style: TextStyle(fontSize: 32.0)),
Padding(
padding: EdgeInsets.all(10.0),
),
TextField(
controller: _controller,
style: TextStyle(fontSize: 32.0),
onChanged: changeField,
),
],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 1,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('Home'),
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
title: Text('next'),
icon: Icon(Icons.navigate_next),
),
],
onTap: (int value) {
if (value == 1)
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen(_input)),
);
},
),
);
}

void changeField(String val) => _input = val;
}

class SecondScreen extends StatelessWidget {
final String _value;

SecondScreen(this._value);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Next"),
),
body: Center(
child: Text('you typed: "$_value".', style: TextStyle(fontSize: 32.0)),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 0,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text('prev'),
icon: Icon(Icons.navigate_before),
),
BottomNavigationBarItem(
title: Text('?'),
icon: Icon(Icons.android),
),
],
onTap: (int value) {
if (value == 0) Navigator.pop(context);
},
),
);
}
}

0 件のコメント:

コメントを投稿