Created
June 10, 2020 10:14
-
-
Save yvonmanzi/ea555bcb1f9ad24625cf57e73868db83 to your computer and use it in GitHub Desktop.
some flutter code for imperative vs declarative article
This file contains hidden or 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'; | |
import 'dart:math'; | |
void main() => runApp(HomePage()); | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
final String _title = 'simple ui'; | |
final String _greetings = 'change my looks!'; | |
Color _currentColor; | |
final Color _initialColor = Colors.deepOrange; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: _title, | |
home: Scaffold( | |
body: Container( | |
alignment: Alignment.center, | |
decoration: BoxDecoration(color: _currentColor), | |
child: ButtonBar( | |
children: <Widget>[ | |
FlatButton( | |
child: Text(_greetings), | |
onPressed: () { | |
var rand = Random(); | |
var c = rand.nextInt(3); | |
setState(() { | |
if (c == 0) _currentColor = Colors.green; | |
if (c == 1) _currentColor = Colors.blue; | |
if (c == 2) _currentColor = Colors.red; | |
}); | |
}, | |
) | |
], | |
)), | |
), | |
); | |
} | |
@override | |
void initState() { | |
_currentColor = _initialColor; | |
super.initState(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment