Created
August 5, 2018 21:42
-
-
Save p69/a2723c6bdd64763cf874cef5ce8d6ed0 to your computer and use it in GitHub Desktop.
Stateful widget with globalkey
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 'dart:math'; | |
import '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: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _currentPosition = 0; | |
static const _maxPosition = 2; | |
bool _removed = false; | |
void _moveDown() { | |
setState(() { | |
_currentPosition++; | |
}); | |
} | |
void _moveUp() { | |
setState(() { | |
_currentPosition--; | |
}); | |
} | |
void _removeBox() { | |
setState(() { | |
_removed = true; | |
}); | |
} | |
void _createBox() { | |
setState(() { | |
_removed = false; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text(widget.title), | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
BoxContainer(_currentPosition == 0 && !_removed), | |
BoxContainer(_currentPosition == 1 && !_removed), | |
BoxContainer(_currentPosition == 2 && !_removed), | |
ButtonsContainer( | |
_currentPosition < _maxPosition && !_removed ? _moveDown : null, | |
_currentPosition > 0 && !_removed ? _moveUp : null, | |
!_removed ? _removeBox : null, | |
_removed ? _createBox : null), | |
], | |
)); | |
} | |
} | |
class BoxContainer extends StatelessWidget { | |
final bool _withChild; | |
BoxContainer(this._withChild); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: 100.0, | |
child: _withChild | |
? ColoredBox( | |
key: boxKey, | |
) | |
: null, | |
); | |
} | |
} | |
class ButtonsContainer extends StatelessWidget { | |
final VoidCallback _downAction; | |
final VoidCallback _upAction; | |
final VoidCallback _removeAction; | |
final VoidCallback _createAction; | |
ButtonsContainer( | |
this._downAction, this._upAction, this._removeAction, this._createAction); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
alignment: Alignment.bottomCenter, | |
color: Colors.yellow.withAlpha(80), | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
RaisedButton( | |
child: Text('Down'), | |
onPressed: _downAction, | |
), | |
RaisedButton( | |
child: Text('Up'), | |
onPressed: _upAction, | |
), | |
RaisedButton( | |
child: Text(_createAction == null ? 'Remove' : 'Create'), | |
onPressed: _createAction == null ? _removeAction : _createAction, | |
), | |
], | |
), | |
); | |
} | |
} | |
final Key boxKey = GlobalKey(debugLabel: 'Colored box key'); | |
class ColoredBox extends StatefulWidget { | |
ColoredBox({Key key}) : super(key: key); | |
@override | |
State createState() => _ColoredBoxState(); | |
} | |
class _ColoredBoxState extends State { | |
int _number; | |
Color _color; | |
@override | |
void initState() { | |
_number = Random().nextInt(50); | |
_color = Color.fromARGB(255, Random().nextInt(255), Random().nextInt(255), | |
Random().nextInt(255)); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: _color, | |
child: Center( | |
child: Text( | |
_number.toString(), | |
style: Theme.of(context).textTheme.display1, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment