basic state management
This commit is contained in:
parent
f701ddefad
commit
a718e02827
8
lib/extensions/obtainProviderExtension.dart
Normal file
8
lib/extensions/obtainProviderExtension.dart
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
extension ObtainProviderExtension on BuildContext {
|
||||||
|
T obtainState<T extends ChangeNotifier>() {
|
||||||
|
return Provider.of<T>(this, listen: false);
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +1,24 @@
|
|||||||
|
import 'package:app/state/legalStatusState.dart';
|
||||||
|
import 'package:app/state/mapState.dart';
|
||||||
import 'package:app/view/home.dart';
|
import 'package:app/view/home.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(
|
runApp(
|
||||||
const MyApp(),
|
MultiProvider(
|
||||||
|
providers: [
|
||||||
|
ChangeNotifierProvider<LegalStatusState>(create: (context) => LegalStatusState()),
|
||||||
|
ChangeNotifierProvider<MapState>(create: (context) => MapState()),
|
||||||
|
],
|
||||||
|
builder: (context, child) => const MyApp(),
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
const MyApp({super.key});
|
const MyApp({super.key});
|
||||||
|
|
||||||
// This widget is the root of your application.
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
|
6
lib/model/legelState.dart
Normal file
6
lib/model/legelState.dart
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
enum LegalState {
|
||||||
|
unknown,
|
||||||
|
allowed,
|
||||||
|
disallowedTime,
|
||||||
|
disallowedRegion
|
||||||
|
}
|
18
lib/state/legalStatusState.dart
Normal file
18
lib/state/legalStatusState.dart
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import 'package:app/model/legelState.dart';
|
||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
|
||||||
|
class LegalStatusState extends ChangeNotifier {
|
||||||
|
LegalState _legalState = LegalState.unknown;
|
||||||
|
|
||||||
|
LegalState get getLegalState => _legalState;
|
||||||
|
|
||||||
|
set setInSightOfDisallowedAreas(bool isInSight) {
|
||||||
|
_legalState = LegalState.disallowedRegion;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
set setIsInDisallowedTimeRanges(bool isInDisallowedTime) {
|
||||||
|
_legalState = LegalState.disallowedTime;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
12
lib/state/mapState.dart
Normal file
12
lib/state/mapState.dart
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
|
||||||
|
class MapState extends ChangeNotifier {
|
||||||
|
bool _isLocationLock = false;
|
||||||
|
|
||||||
|
bool get followLocation => _isLocationLock;
|
||||||
|
|
||||||
|
toggleLocationLock() {
|
||||||
|
_isLocationLock = !_isLocationLock;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,9 @@
|
|||||||
|
import 'package:app/extensions/obtainProviderExtension.dart';
|
||||||
|
import 'package:app/state/mapState.dart';
|
||||||
|
import 'package:app/view/legalStatus.dart';
|
||||||
import 'package:app/view/map.dart';
|
import 'package:app/view/map.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class HomeView extends StatefulWidget {
|
class HomeView extends StatefulWidget {
|
||||||
const HomeView({super.key});
|
const HomeView({super.key});
|
||||||
@ -18,12 +22,20 @@ class _HomePageState extends State<HomeView> {
|
|||||||
actions: [],
|
actions: [],
|
||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
child: const Icon(Icons.my_location),
|
child: Consumer<MapState>(builder: (context, value, child) => Icon(value.followLocation ? Icons.my_location : Icons.location_disabled)),
|
||||||
onPressed: () {
|
onPressed: () => context.obtainState<MapState>().toggleLocationLock(),
|
||||||
|
),
|
||||||
},
|
body: const Column(
|
||||||
|
children: [
|
||||||
|
SizedBox(
|
||||||
|
height: 100,
|
||||||
|
child: LegalStatusView(),
|
||||||
|
),
|
||||||
|
Expanded(
|
||||||
|
child: MapView(),
|
||||||
|
)
|
||||||
|
],
|
||||||
),
|
),
|
||||||
body: MapView(),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
lib/view/legalStatus.dart
Normal file
21
lib/view/legalStatus.dart
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import 'package:app/state/legalStatusState.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
class LegalStatusView extends StatefulWidget {
|
||||||
|
const LegalStatusView({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<LegalStatusView> createState() => _LegalStatusViewState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LegalStatusViewState extends State<LegalStatusView> {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Consumer<LegalStatusState>(
|
||||||
|
builder: (context, value, child) {
|
||||||
|
return Placeholder();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
15
lib/view/status.dart
Normal file
15
lib/view/status.dart
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class StatusView extends StatefulWidget {
|
||||||
|
const StatusView({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<StatusView> createState() => _StatusViewState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _StatusViewState extends State<StatusView> {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return const Placeholder();
|
||||||
|
}
|
||||||
|
}
|
47
pubspec.lock
47
pubspec.lock
@ -1,6 +1,14 @@
|
|||||||
# Generated by pub
|
# Generated by pub
|
||||||
# See https://dart.dev/tools/pub/glossary#lockfile
|
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||||
packages:
|
packages:
|
||||||
|
ansicolor:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: ansicolor
|
||||||
|
sha256: "8bf17a8ff6ea17499e40a2d2542c2f481cd7615760c6d34065cb22bfd22e6880"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.2"
|
||||||
archive:
|
archive:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -89,6 +97,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.3"
|
version: "3.0.3"
|
||||||
|
csslib:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: csslib
|
||||||
|
sha256: "706b5707578e0c1b4b7550f64078f0a0f19dec3f50a178ffae7006b0a9ca58fb"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
cupertino_icons:
|
cupertino_icons:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
@ -126,11 +142,32 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.1"
|
version: "3.0.1"
|
||||||
|
flutter_native_splash:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_native_splash
|
||||||
|
sha256: edf39bcf4d74aca1eb2c1e43c3e445fd9f494013df7f0da752fefe72020eedc0
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.4.0"
|
||||||
flutter_test:
|
flutter_test:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
flutter_web_plugins:
|
||||||
|
dependency: transitive
|
||||||
|
description: flutter
|
||||||
|
source: sdk
|
||||||
|
version: "0.0.0"
|
||||||
|
html:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: html
|
||||||
|
sha256: "3a7812d5bcd2894edf53dfaf8cd640876cf6cef50a8f238745c8b8120ea74d3a"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.15.4"
|
||||||
image:
|
image:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -312,6 +349,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.2"
|
version: "1.3.2"
|
||||||
|
universal_io:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: universal_io
|
||||||
|
sha256: "1722b2dcc462b4b2f3ee7d188dad008b6eb4c40bbd03a3de451d82c78bba9aad"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.2.2"
|
||||||
vector_math:
|
vector_math:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -346,4 +391,4 @@ packages:
|
|||||||
version: "3.1.2"
|
version: "3.1.2"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=3.3.2 <4.0.0"
|
dart: ">=3.3.2 <4.0.0"
|
||||||
flutter: ">=1.16.0"
|
flutter: ">=2.5.0"
|
||||||
|
Reference in New Issue
Block a user