Skip to content

Instantly share code, notes, and snippets.

@felangel felangel/main.dart
Created May 28, 2019

Embed
What would you like to do?
[flutter_timer] completed main
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_timer/bloc/bloc.dart';
import 'package:flutter_timer/ticker.dart';
import 'package:wave/wave.dart';
import 'package:wave/config.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final TimerBloc _timerBloc = TimerBloc(ticker: Ticker());
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primaryColor: Color.fromRGBO(109, 234, 255, 1),
accentColor: Color.fromRGBO(72, 74, 126, 1),
brightness: Brightness.dark,
),
title: 'Flutter Timer',
home: BlocProvider(
bloc: _timerBloc,
child: Timer(),
),
);
}
@override
void dispose() {
_timerBloc.dispose();
super.dispose();
}
}
class Timer extends StatelessWidget {
static const TextStyle timerTextStyle = TextStyle(
fontSize: 60,
fontWeight: FontWeight.bold,
);
@override
Widget build(BuildContext context) {
final TimerBloc _timerBloc = BlocProvider.of<TimerBloc>(context);
return Scaffold(
appBar: AppBar(title: Text('Flutter Timer')),
body: Stack(
children: [
Background(),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 100.0),
child: Center(
child: BlocBuilder(
bloc: _timerBloc,
builder: (context, state) {
final String minutesStr = ((state.duration / 60) % 60)
.floor()
.toString()
.padLeft(2, '0');
final String secondsStr = (state.duration % 60)
.floor()
.toString()
.padLeft(2, '0');
return Text(
'$minutesStr:$secondsStr',
style: Timer.timerTextStyle,
);
},
),
),
),
BlocBuilder(
condition: (previousState, currentState) =>
currentState.runtimeType != previousState.runtimeType,
bloc: _timerBloc,
builder: (context, state) => Actions(),
),
],
),
],
),
);
}
}
class Actions extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: _mapStateToActionButtons(
timerBloc: BlocProvider.of<TimerBloc>(context),
),
);
}
List<Widget> _mapStateToActionButtons({
TimerBloc timerBloc,
}) {
final TimerState state = timerBloc.currentState;
if (state is Ready) {
return [
FloatingActionButton(
child: Icon(Icons.play_arrow),
onPressed: () => timerBloc.dispatch(Start(duration: state.duration)),
),
];
}
if (state is Running) {
return [
FloatingActionButton(
child: Icon(Icons.pause),
onPressed: () => timerBloc.dispatch(Pause()),
),
FloatingActionButton(
child: Icon(Icons.replay),
onPressed: () => timerBloc.dispatch(Reset()),
),
];
}
if (state is Paused) {
return [
FloatingActionButton(
child: Icon(Icons.play_arrow),
onPressed: () => timerBloc.dispatch(Resume()),
),
FloatingActionButton(
child: Icon(Icons.replay),
onPressed: () => timerBloc.dispatch(Reset()),
),
];
}
if (state is Finished) {
return [
FloatingActionButton(
child: Icon(Icons.replay),
onPressed: () => timerBloc.dispatch(Reset()),
),
];
}
return [];
}
}
class Background extends StatelessWidget {
@override
Widget build(BuildContext context) {
return WaveWidget(
config: CustomConfig(
gradients: [
[
Color.fromRGBO(72, 74, 126, 1),
Color.fromRGBO(125, 170, 206, 1),
Color.fromRGBO(184, 189, 245, 0.7)
],
[
Color.fromRGBO(72, 74, 126, 1),
Color.fromRGBO(125, 170, 206, 1),
Color.fromRGBO(172, 182, 219, 0.7)
],
[
Color.fromRGBO(72, 73, 126, 1),
Color.fromRGBO(125, 170, 206, 1),
Color.fromRGBO(190, 238, 246, 0.7)
],
],
durations: [19440, 10800, 6000],
heightPercentages: [0.03, 0.01, 0.02],
gradientBegin: Alignment.bottomCenter,
gradientEnd: Alignment.topCenter,
),
size: Size(double.infinity, double.infinity),
waveAmplitude: 25,
backgroundColor: Colors.blue[50],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.