Skip to content

Instantly share code, notes, and snippets.

@sulmanweb
Created June 21, 2022 09:16
Show Gist options
  • Save sulmanweb/c9269dafab36b5eafd6a03b43303704c to your computer and use it in GitHub Desktop.
Save sulmanweb/c9269dafab36b5eafd6a03b43303704c to your computer and use it in GitHub Desktop.
Flutter Assignment 1
import 'package:flutter/material.dart';
class ClickButton extends StatelessWidget {
final VoidCallback onPressedHandler;
const ClickButton({Key? key, required this.onPressedHandler})
: super(key: key);
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onPressedHandler,
child: const Text('Click Me'),
);
}
}
// 1) Create a new Flutter App (in this project) and output an AppBar and some text
// below it
// 2) Add a button which changes the text (to any other text of your choice)
// 3) Split the app into three widgets: App, TextControl & Text
import 'package:flutter/material.dart';
import './text_title.dart';
import './click_button.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const List<String> _titles = [
'First',
'Second',
'Third',
'Fourth',
'Fifth',
'Sixth',
];
int _titleIndex = 0;
void _changeText() {
setState(() {
_titleIndex += 1;
if (_titleIndex >= _titles.length) {
_titleIndex = 0;
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Assignment 1'),
),
body: Center(
child: Column(
children: [
TestTitle(title: _titles[_titleIndex]),
ClickButton(onPressedHandler: _changeText),
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
class TestTitle extends StatelessWidget {
final String title;
const TestTitle({Key? key, required this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(10),
child: Text(
title,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w400,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment