Skip to content

Instantly share code, notes, and snippets.

@tolo
Last active January 18, 2023 14:39
Show Gist options
  • Save tolo/839aebabfca0b0215f52780621963ff8 to your computer and use it in GitHub Desktop.
Save tolo/839aebabfca0b0215f52780621963ff8 to your computer and use it in GitHub Desktop.
Flutter Labinar - Lab 1 - Counter (complete)

Flutter Labinar - Lab 1 - Counter (complete)

Created with <3 with dartpad.dev.

// ignore_for_file: prefer_const_declarations
// ignore_for_file: prefer_const_constructors
// ignore_for_file: prefer_final_fields
// ignore_for_file: use_key_in_widget_constructors
// ignore_for_file: prefer_const_literals_to_create_immutables
// ignore_for_file: prefer_const_constructors_in_immutables
// ignore_for_file: avoid_print
import 'package:flutter/material.dart';
void main() {
runApp(const CounterCompleteApp());
}
class CounterCompleteApp extends StatelessWidget {
const CounterCompleteApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CounterApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CounterPage(title: 'CounterApp'),
);
}
}
class CounterPage extends StatefulWidget {
CounterPage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State createState() => _CounterPageState();
}
class _CounterPageState extends State<CounterPage> {
late int _counter;
@override
void initState() {
_counter = 0;
super.initState();
}
void _incrementCounter() {
setState(() {
_counter++;
});
if (_counter == 1) {
_showToast();
}
}
void _showToast() {
final snackBar = SnackBar(backgroundColor: Colors.purple, content: Text('Incremented! 🚀'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
margin: EdgeInsets.symmetric(vertical: 24, horizontal: 8),
alignment: Alignment.center,
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [Colors.blue, Colors.red],
),
border: Border.all(
color: Colors.black38,
width: 4,
),
borderRadius: BorderRadius.circular(20),
),
child: DefaultTextStyle(
style: Theme.of(context).textTheme.bodyText1!.copyWith(color: Colors.white),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Offstage(child: Text('🚀')), // Preload icon
Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: TextStyle(fontSize: 44),
),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment