Created
January 17, 2020 22:10
-
-
Save frank06/a9f55b6de67133a9d686b389ad07da22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
List<String> homePageList = List(); | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
homePageList.add('A' + _counter.toString()); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Flutter Demo Home Page'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'homePageList', | |
), | |
Text( | |
'$homePageList', | |
style: Theme.of(context).textTheme.display1, | |
), | |
FlatButton( | |
color: Colors.blue, | |
child: Text('Next Screen'), | |
onPressed: () { | |
Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => OtherScreen( | |
passedInList: homePageList, | |
), | |
), | |
); | |
}, | |
) | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class OtherScreen extends StatefulWidget { | |
OtherScreen({Key key, this.passedInList}) : super(key: key); | |
final List<String> passedInList; | |
@override | |
_OtherScreenState createState() => _OtherScreenState(); | |
} | |
class _OtherScreenState extends State<OtherScreen> { | |
List<String> _otherScreenList; | |
@override | |
void initState() { | |
super.initState(); | |
_otherScreenList = List<String>.from(widget.passedInList); | |
} | |
void _removeLast() { | |
setState(() { | |
// only remove from _otherScreenList, but it still removes from the widget.passedInList | |
_otherScreenList.removeLast(); | |
print('widget.passedInList: ' + widget.passedInList.toString()); | |
print('_otherScreenList: ' + _otherScreenList.toString()); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Other Screen'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'widget.passedInList', | |
), | |
Text( | |
'${widget.passedInList}', | |
style: Theme.of(context).textTheme.display1, | |
), | |
Text( | |
'otherScreenList', | |
), | |
Text( | |
'$_otherScreenList', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _removeLast, | |
tooltip: 'Remove last', | |
child: Icon(Icons.minimize), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment