StatelessWidget dan StatefulWidget
Apa itu State?
class ContohWidget extends StatelessWidget{
final String _judul
...
}
StatelessWidget
- class Heading extends StatelessWidget {
- final String text;
- Heading({Key key, this.text});
- @override
- Widget build(BuildContext context){
- return Text(
- text,
- style: TextStyle(
- fontSize: 24.0,
- fontWeight: FontWeight.bold,
- ),
- );
- }
- }
Widget di atas merupakan sebuah widget untuk membuat Heading (sebuah text yang digunakan untuk judul). Kita akan panggil widget yang telah diubah ke code project pertama kita.
- import ‘package:flutter/material.dart’;
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: ‘Flutter Demo’,
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: Scaffold(
- body: Center(
- child: Text(“Hello world!”),
- ),
- ),
- );
- }
- }
- class Heading extends StatelessWidget {
- final String text;
- Heading({Key key, this.text});
- @override
- Widget build(BuildContext context){
- return Text(
- text,
- style: TextStyle(
- fontSize: 24.0,
- fontWeight: FontWeight.bold,
- ),
- );
- }
- }
Kita coba ubah widget Text yang menampilkan “Hello world!” dengan widget Heading yang kita buat.
- import ‘package:flutter/material.dart’;
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: ‘Flutter Demo’,
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: Scaffold(
- body: Center(
- child: Heading( // mengubah widget Text
- text:“Hello world!”,
- ),
- ),
- ),
- );
- }
- }
- class Heading extends StatelessWidget {
- final String text;
- Heading({Key key, this.text});
- @override
- Widget build(BuildContext context){
- return Text(
- text,
- style: TextStyle(
- fontSize: 24.0,
- fontWeight: FontWeight.bold,
- ),
- );
- }
- }
- final String text; // state text bersifat final
- Heading({this.text}); // lalu state text masuk ke constructor
StatefulWidget
- class ContohStateful extends StatefulWidget {
- final String parameterWidget; // ini parameter widget
- ContohStateful({this.parameterWidget});
- @override
- _ContohStatefulState createState() => _ContohStatefulState();
- }
- class _ContohStatefulState extends State<ContohStateful>{
- String _dataState; // ini state dari Widget ContohStateful
- @override
- Widget build(BuildContext context){
- // isi sebuah widget
- }
- }
- class PerubahanText extends StatefulWidget {
- final String text;
- const PerubahanText({Key key, this.text}) : super(key: key);
- @override
- _PerubahanTextState createState() => _PerubahanTextState();
- }
- class _PerubahanTextState extends State<PerubahanText> {
- double _ukuranText = 16.0;
- @override
- Widget build(BuildContext context) {
- return Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Text(widget.text, style: TextStyle(fontSize: _ukuranText)),
- RaisedButton(
- child: Text(“Perbesar”),
- onPressed: (){
- setState(() {
- _ukuranText = 32.0;
- });
- },
- )
- ],
- );
- }
- }
Letakkan kode di atas setelah StatelessWidget Heading yang telah kita buat sebelumnya lalu panggil StatefulWidget PerubahanText pada MyApp.
- import ‘package:flutter/material.dart’;
- void main() => runApp(MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: ‘Flutter Demo’,
- theme: ThemeData(
- primarySwatch: Colors.blue,
- ),
- home: Scaffold(
- body: Center(
- child: PerubahanText(text:“Hello world!”), // Ubah widget Heading ke PerubahanText
- ),
- ),
- );
- }
- }
- class Heading extends StatelessWidget {
- final String text;
- Heading({Key key, this.text});
- @override
- Widget build(BuildContext context){
- return Text(text, style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold));
- }
- }
- class PerubahanText extends StatefulWidget {
- final String text;
- const PerubahanText({Key key, this.text}) : super(key: key);
- @override
- _PerubahanTextState createState() => _PerubahanTextState();
- }
- class _PerubahanTextState extends State<PerubahanText> {
- double _ukuranText = 16.0;
- @override
- Widget build(BuildContext context) {
- return Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Text(widget.text, style: TextStyle(fontSize: _ukuranText)),
- RaisedButton(
- child: Text(“Perbesar”),
- onPressed: (){
- setState(() {
- _ukuranText = 32.0;
- });
- },
- )
- ],
- );
- }
- }
- setState(() {
- _ukuranText = 32.0; // ukuran text diubah menjadi 32
- });