- debugShowCheckedModeBanner: false デバッグモードでは指定しないとバナー非表示にできない(リリースモードではデフォルトで非表示)
- home: TodoListPage() StetelessWidget に StetefulWidget を載せる
import 'package:flutter/material.dart';
void main() => runApp(const MyTodoApp());
class MyTodoApp extends StatelessWidget
{
const MyTodoApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context)
{
return MaterialApp
(
debugShowCheckedModeBanner: false,
title: 'My Todo App',
theme: ThemeData(primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity),
home: TodoListPage(),
);
}
}
class TodoListPage extends StatefulWidget {
@override
_TodoListPageState createState() => _TodoListPageState();
}
class _TodoListPageState extends State<TodoListPage> {
List<String> todoList = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('リスト一覧'),
),
body: ListView.builder(
itemCount: todoList.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(todoList[index]),
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
final newListText = await Navigator.of(context).push(
MaterialPageRoute(builder: (context) {
return TodoAddPage();
}),
);
if (newListText != null) {
setState(() {
todoList.add(newListText);
});
}
},
child: Icon(Icons.add),
),
);
}
}
class TodoAddPage extends StatefulWidget {
@override
_TodoAddPageState createState() => _TodoAddPageState();
}
class _TodoAddPageState extends State<TodoAddPage> {
String _text = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('リスト追加'),
),
body: Container(
padding: EdgeInsets.all(64),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(_text, style: TextStyle(color: Colors.blue)),
const SizedBox(height: 8),
TextField(
onChanged: (String value) {
setState(() {
_text = value;
});
},
),
const SizedBox(height: 8),
Container(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
Navigator.of(context).pop(_text);
},
child: Text('リスト追加', style: TextStyle(color: Colors.white)),
),
),
const SizedBox(height: 8),
Container(
width: double.infinity,
child: TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('キャンセル'),
),
),
],
),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}