Skip to content

Commit

Permalink
Create a terms and conditions widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nck974 committed Sep 8, 2023
1 parent 5e947b1 commit 0d5e16c
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 27 deletions.
20 changes: 13 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:memories/pages/home/home.dart';
import 'package:memories/services/level_service.dart';
import 'package:memories/services/storage_service.dart';
import 'package:memories/services/terms_and_conditions_service.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

Expand Down Expand Up @@ -37,16 +38,21 @@ class MyApp extends StatelessWidget {
snapshot.hasData) {
final preferences = snapshot.data as SharedPreferences;
final storageService = StorageService(preferences);
return ChangeNotifierProvider(
create: (context) => LevelService(storageService),
builder: (context, child) {
return MaterialApp(
return MultiProvider(
providers: [
ChangeNotifierProvider<LevelService>(
create: (context) => LevelService(storageService),
),
ChangeNotifierProvider<TermsAndConditionsService>(
create: (context) =>
TermsAndConditionsService(storageService),
),
],
child: MaterialApp(
title: 'Memories',
theme: _setUpTheme(),
// Use a future builder to await the access to shared preferences
home: const HomePage());
},
);
home: const HomePage()));
} else {
return const Center(child: CircularProgressIndicator());
}
Expand Down
40 changes: 40 additions & 0 deletions lib/pages/home/components/terms_and_conditions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter/material.dart';

class TermsAndConditions extends StatelessWidget {
final VoidCallback onAcceptConditions;

const TermsAndConditions({super.key, required this.onAcceptConditions});

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
const Text(
'The content of the app can not be shared with anyone.'
'By accepting, you agree to the terms and conditions.',
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.normal,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 16.0),
ElevatedButton(
onPressed: onAcceptConditions,
child: const Text(
'Accept',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
],
),
);
}
}
72 changes: 52 additions & 20 deletions lib/pages/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import 'package:memories/model/gift.dart';
import 'package:memories/model/quiz_question.dart';
import 'package:memories/pages/gift/gift.dart';
import 'package:memories/pages/home/components/level_box.dart';
import 'package:memories/pages/home/components/terms_and_conditions.dart';
import 'package:memories/pages/home/services/question_service.dart';
import 'package:memories/pages/prize/prize.dart';
import 'package:memories/pages/question/question.dart';
import 'package:memories/services/level_service.dart';
import 'package:memories/services/terms_and_conditions_service.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
Expand All @@ -21,6 +23,7 @@ class _HomePageState extends State<HomePage> {
final int columnCount = 3;
int currentLevel = 0;
List<QuizQuestion> questions = [];
bool termsAccepted = false;

void _navigateToQuestion(int index) {
Navigator.push(
Expand Down Expand Up @@ -65,6 +68,7 @@ class _HomePageState extends State<HomePage> {
this.questions = questions;
});
});
Future.delayed(const Duration(milliseconds: 100), _setTermsAndConditions);
Future.delayed(const Duration(milliseconds: 100), _setCurrentLevel);
}

Expand All @@ -83,6 +87,18 @@ class _HomePageState extends State<HomePage> {
});
}

void _setTermsAndConditions() {
bool? termsAndConditions =
Provider.of<TermsAndConditionsService>(context, listen: false)
.getTermsAndConditions();
print("Terms and conditions is $currentLevel");
if (termsAndConditions != null) {
setState(() {
termsAccepted = termsAndConditions;
});
}
}

void _onTapLevel(int index, int level) {
if (level > currentLevel) {
return;
Expand All @@ -99,6 +115,39 @@ class _HomePageState extends State<HomePage> {
_navigateToQuestion(index);
}

Widget _buildQuestions() {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: GridView.count(
crossAxisCount: columnCount,
children: List.generate(
questions.length,
(index) {
int level = questions[index].level;
return GestureDetector(
onTap: () => _onTapLevel(index, level),
child: LevelBox(
index: level,
completed: level < currentLevel,
disabled: level > currentLevel,
selected: level == currentLevel),
);
},
),
),
);
}

void _onAcceptConditions() {
Provider.of<TermsAndConditionsService>(context, listen: false)
.setTermsAndConditions(true)
.then((value) => {
setState(() {
termsAccepted = true;
})
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -109,26 +158,9 @@ class _HomePageState extends State<HomePage> {
),
body: questions.isEmpty
? const Center(child: CircularProgressIndicator())
: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: GridView.count(
crossAxisCount: columnCount,
children: List.generate(
questions.length,
(index) {
int level = questions[index].level;
return GestureDetector(
onTap: () => _onTapLevel(index, level),
child: LevelBox(
index: level,
completed: level < currentLevel,
disabled: level > currentLevel,
selected: level == currentLevel),
);
},
),
),
),
: termsAccepted
? _buildQuestions()
: TermsAndConditions(onAcceptConditions: _onAcceptConditions),
);
}
}
16 changes: 16 additions & 0 deletions lib/services/terms_and_conditions_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter/foundation.dart';
import 'package:memories/services/storage_service.dart';

class TermsAndConditionsService extends ChangeNotifier {
final StorageService _storageService;

TermsAndConditionsService(this._storageService);

Future<void> setTermsAndConditions(bool termsAndConditions) async {
await _storageService.saveBool('termsAccepted', termsAndConditions);
}

bool? getTermsAndConditions() {
return _storageService.getBool('termsAccepted');
}
}

0 comments on commit 0d5e16c

Please sign in to comment.