Skip to content

Commit

Permalink
fix: nfc scan
Browse files Browse the repository at this point in the history
  • Loading branch information
ezeanyimhenry committed Jul 31, 2024
1 parent 86d972c commit 12bf0b1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
33 changes: 31 additions & 2 deletions lib/notifier/nfc_notifier.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:nfc_app/notifier/nfc_broadcast_receiver.dart';
import 'package:nfc_app/presentation/screens/home.dart';
import 'package:nfc_app/presentation/screens/translate/translate_screen.dart';
import 'package:nfc_app/presentation/widgets/app_bottom_sheet.dart';
import 'package:nfc_app/presentation/widgets/circle_progress_indicator.dart';
Expand Down Expand Up @@ -54,7 +55,8 @@ class NFCNotifier extends ChangeNotifier {
if (nfcOperation == NFCOperation.read) {
await _readFromTag(tag: nfcTag, context: context);
} else if (nfcOperation == NFCOperation.write) {
await _writeToTag(nfcTag: nfcTag, content: content);
await _writeToTag(
nfcTag: nfcTag, content: content, context: context);
_message = "DONE";
}

Expand Down Expand Up @@ -144,9 +146,36 @@ class NFCNotifier extends ChangeNotifier {
Future<void> _writeToTag({
required NfcTag nfcTag,
required String content,
required BuildContext context,
}) async {
NdefMessage message = _createNdefMessage(content: content);
await Ndef.from(nfcTag)?.write(message);
showModalBottomSheet(
isDismissible: false,
context: context,
builder: (context) {
return SizedBox(
height: MediaQuery.of(context).size.height * 0.4,
child: AppBottomsheet(
hasPrimaryButton: true,
primaryButtonText: "Continue",
primaryButtonOnTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(initialIndex: 0),
),
);
},
message: "",
title: "Write Successful!",
centerContent: const ProgressIndicatorWithText(
progress: 1.0,
),
),
);
},
);
}

NdefMessage _createNdefMessage({required String content}) {
Expand All @@ -157,7 +186,7 @@ class NFCNotifier extends ChangeNotifier {
_isProcessing = false;
_message = error.toString();
notifyListeners();
NfcManager.instance.stopSession(); // Ensure session is stopped
NfcManager.instance.stopSession();
}
}

Expand Down
24 changes: 15 additions & 9 deletions lib/presentation/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import '../../notifier/bottom_nav.dart';
import 'read-nfc/read_nfc_screen.dart';

class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
const HomeScreen({super.key, this.initialIndex = 0});

final int initialIndex;

final List<Widget> _screens = const [
ReadNFCScreen(),
TextRecordScreen(),
Expand All @@ -18,13 +21,16 @@ class HomeScreen extends StatelessWidget {
];
@override
Widget build(BuildContext context) {
return Scaffold(body: Consumer<BottomNavigationProvider>(
builder: (context, provider, child) {
return _screens[provider.currentIndex];
},
), bottomNavigationBar:
Consumer<BottomNavigationProvider>(builder: (context, provider, child) {
return AppBottomNav(selectedindex: provider.currentIndex);
}));
return ChangeNotifierProvider(
create: (context) => BottomNavigationProvider()..setIndex(initialIndex),
child: Scaffold(body: Consumer<BottomNavigationProvider>(
builder: (context, provider, child) {
return _screens[provider.currentIndex];
},
), bottomNavigationBar: Consumer<BottomNavigationProvider>(
builder: (context, provider, child) {
return AppBottomNav(selectedindex: provider.currentIndex);
})),
);
}
}
12 changes: 11 additions & 1 deletion lib/presentation/screens/read-nfc/read_nfc_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ReadNFCScreen extends StatefulWidget {
}

class _ReadNFCScreenState extends State<ReadNFCScreen> {
bool _nfcEnabled = false;
bool _nfcEnabled = true;
@override
void initState() {
super.initState();
Expand Down Expand Up @@ -89,6 +89,16 @@ class _ReadNFCScreenState extends State<ReadNFCScreen> {
),
),
),
Visibility(
visible: !_nfcEnabled,
child: SizedBox(
width: MediaQuery.sizeOf(context).width * 0.6,
child: InactiveButton(
onTap: () {},
text: 'Scan NFC tag',
),
),
),
const YGap(),
Text(
_nfcEnabled
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/widgets/circle_progress_indicator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ProgressIndicatorWithText extends StatefulWidget {
}

class ProgressIndicatorWithTextState extends State<ProgressIndicatorWithText> {
double _progress = 0.5;
double _progress = 1.0;

void updateProgress(double progress) {
setState(() {
Expand Down

0 comments on commit 12bf0b1

Please sign in to comment.