Skip to content

Instantly share code, notes, and snippets.

@leechy
Created July 5, 2019 19:53
Show Gist options
  • Save leechy/b4151e24c8935d60133c0e35519c4e0d to your computer and use it in GitHub Desktop.
Save leechy/b4151e24c8935d60133c0e35519c4e0d to your computer and use it in GitHub Desktop.
Assignment 1: Time to Practice: Flutter Basics
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutterass1/textcontrol.dart';
import 'package:flutterass1/wisdom.dart';
void main() => runApp(AssignmentApp());
class AssignmentApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _AssignmentAppState();
}
}
class _AssignmentAppState extends State<AssignmentApp> {
var _textIndex = 0;
final texts = [
'To enjoy good health, to bring true happiness to one\'s family, to bring peace to all, one must first discipline and control one\'s own mind. If a man can control his mind he can find the way to Enlightenment, and all wisdom and virtue will naturally come to him.',
'We are shaped by our thoughts; we become what we think. When the mind is pure, joy follows like a shadow that never leaves.',
'There are only two mistakes one can make along the road to truth; not going all the way, and not starting.',
'No one saves us but ourselves. No one can and no one may. We ourselves must walk the path.',
'Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.',
'In a controversy the instant we feel anger we have already ceased striving for the truth, and have begun striving for ourselves.'
];
void _getRandomText() {
int newTextIndex = new Random().nextInt(texts.length);
if (newTextIndex == _textIndex) {
_getRandomText();
} else {
print(_textIndex);
print(newTextIndex);
setState(() => _textIndex = newTextIndex);
}
}
@override
Widget build(BuildContext context) {
// get random thought first
_getRandomText();
return MaterialApp(home: Scaffold(
appBar: AppBar(title: Text('Buddha\'s Thoughts'), backgroundColor: Colors.deepPurple),
body: Wisdom(texts[_textIndex]),
floatingActionButton: TextControl('Rethink', onPressed: _getRandomText),
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat
));
}
}
import 'package:flutter/material.dart';
class TextControl extends StatelessWidget {
final String buttonText;
final Function onPressed;
TextControl(this.buttonText, { this.onPressed });
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: onPressed,
tooltip: buttonText,
child: Icon(Icons.rotate_left),
backgroundColor: Colors.amber,
foregroundColor: Colors.black,
);
}
}
import 'package:flutter/material.dart';
class Wisdom extends StatelessWidget {
final String text;
Wisdom(this.text);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
margin: EdgeInsets.all(30),
child: Text(
text,
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold
),
textAlign: TextAlign.center
)
)
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment