0% found this document useful (0 votes)
78 views2 pages

Lab+State+Management Provider

The document describes using the provider package for state management in Flutter. It shows a main.dart file that uses ChangeNotifierProvider to provide an ApplicationColor model to descendant widgets. The model contains a boolean property to track if the color should be light blue or amber, and notifies listeners on change. Consumer widgets are used to rebuild when the state changes and display the current color.

Uploaded by

Silviana Widya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views2 pages

Lab+State+Management Provider

The document describes using the provider package for state management in Flutter. It shows a main.dart file that uses ChangeNotifierProvider to provide an ApplicationColor model to descendant widgets. The model contains a boolean property to track if the color should be light blue or amber, and notifies listeners on change. Consumer widgets are used to rebuild when the state changes and display the current color.

Uploaded by

Silviana Widya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab State Management

1. Provider state management


Main.dart
Main.dart
import'package:application_providerstatemanagement/application_color.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {


const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ChangeNotifierProvider<ApplicationColor>(
create: (context) => ApplicationColor(),
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Consumer<ApplicationColor>(
builder: (context, applicationcolor, _) => Text(
"Provider State Manageement",
style: TextStyle(color: applicationcolor.color),
),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Consumer<ApplicationColor>(
builder: (context, applicationcolor, _) =>
AnimatedContainer(
margin: EdgeInsets.all(5),
width: 100,
height: 100,
color: applicationcolor.color,
duration: Duration(milliseconds: 500),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(margin: EdgeInsets.all(5), child:
Text("AB")),
Consumer<ApplicationColor>(
builder: (context, applicationcolor, _) => Switch(
value: applicationcolor.isLightBlue,
onChanged: (newValue) {
applicationcolor.isLightBlue = newValue;
},
),
),
Container(margin: EdgeInsets.all(5), child:
Text("LB")),
],
)
],
),
),
),
),
);
}
}

application_color.dart
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class ApplicationColor with ChangeNotifier {


bool _isLightBlue = true;

bool get isLightBlue => _isLightBlue;


set isLightBlue(bool value){
_isLightBlue = value;
notifyListeners();
}

Color get color => (_isLightBlue) ? Colors.lightBlue : Colors.amber;


}

sebelum itu silahkan tambahkan plugin provider:^6.0.0 di pubspec.yaml seperti berikut ini.
cupertino_icons: ^1.0.2
provider: ^6.0.2

dev_dependencies:
flutter_test:
sdk: flutter

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy