Skip to content

Instantly share code, notes, and snippets.

@m24te28
Last active October 22, 2019 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m24te28/0999ac4b5eb38bcb59b4f92aa0a7b560 to your computer and use it in GitHub Desktop.
Save m24te28/0999ac4b5eb38bcb59b4f92aa0a7b560 to your computer and use it in GitHub Desktop.
simple push and pop demo with return
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Push / Pop demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _navigateToSecond() async {
int ret = await Navigator.of(context)
.push(MaterialPageRoute<int>(builder: (BuildContext context) {
return SecondPage(counter: _counter);
}));
setState(() {
// null が返された場合は、元の値を表示
_counter = ret == null ? _counter : ret;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button on Second Page:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _navigateToSecond,
tooltip: 'To Second Page',
child: Icon(Icons.forward),
),
);
}
}
class SecondPage extends StatefulWidget {
SecondPage({Key key, this.counter}) : super(key: key);
final int counter;
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
int _counter;
@override
void initState() {
super.initState();
_counter = widget.counter;
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
Row(
children: <Widget>[
Expanded(
flex: 1,
child: RaisedButton(
// counter をリセット
onPressed: () => Navigator.of(context).pop(0),
child: Icon(Icons.cancel),
color: Colors.red,
),
),
Expanded(
flex: 1,
child: RaisedButton(
onPressed: () => Navigator.of(context).pop(_counter),
child: Icon(Icons.check_circle),
color: Colors.green,
),
),
],
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment