Skip to content

Commit 78a2a2d

Browse files
fixed app bar issue, deprecoated old one
1 parent f24027e commit 78a2a2d

7 files changed

Lines changed: 137 additions & 29 deletions

File tree

example/lib/main.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:simple_barcode_scanner/barcode_appbar.dart';
23
import 'package:simple_barcode_scanner/simple_barcode_scanner.dart';
34

45
void main() {
@@ -43,6 +44,12 @@ class _HomePageState extends State<HomePage> {
4344
context,
4445
MaterialPageRoute(
4546
builder: (context) => const SimpleBarcodeScannerPage(
47+
barcodeAppBar: BarcodeAppBar(
48+
appBarTitle: 'Test',
49+
centerTitle: false,
50+
enableBackButton: true,
51+
backButtonIcon: Icon(Icons.arrow_back_ios),
52+
),
4653
child: Column(
4754
children: [
4855
SizedBox(

lib/barcode_appbar.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:flutter/material.dart';
2+
3+
class BarcodeAppBar {
4+
final String? appBarTitle;
5+
final bool? centerTitle;
6+
final bool? enableBackButton;
7+
final Icon? backButtonIcon;
8+
9+
const BarcodeAppBar({
10+
this.appBarTitle,
11+
this.centerTitle,
12+
this.enableBackButton,
13+
this.backButtonIcon,
14+
});
15+
}

lib/screens/io_device.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
44
import 'package:simple_barcode_scanner/enum.dart';
55
import 'package:simple_barcode_scanner/screens/window.dart';
66

7+
import '../barcode_appbar.dart';
78
import '../flutter_barcode_scanner.dart';
89

910
/// Barcode scanner for mobile and desktop devices
@@ -16,6 +17,8 @@ class BarcodeScanner extends StatelessWidget {
1617
final String? appBarTitle;
1718
final bool? centerTitle;
1819
final Widget? child;
20+
final BarcodeAppBar? barcodeAppBar;
21+
1922
const BarcodeScanner({
2023
super.key,
2124
required this.lineColor,
@@ -26,6 +29,7 @@ class BarcodeScanner extends StatelessWidget {
2629
this.child,
2730
this.appBarTitle,
2831
this.centerTitle,
32+
this.barcodeAppBar,
2933
});
3034

3135
@override

lib/screens/unsupported.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:simple_barcode_scanner/barcode_appbar.dart';
23
import 'package:simple_barcode_scanner/enum.dart';
34

45
class BarcodeScanner extends StatelessWidget {
@@ -10,6 +11,7 @@ class BarcodeScanner extends StatelessWidget {
1011
final String? appBarTitle;
1112
final bool? centerTitle;
1213
final Widget? child;
14+
final BarcodeAppBar? barcodeAppBar;
1315
const BarcodeScanner(
1416
{super.key,
1517
this.lineColor = "#ff6666",
@@ -19,7 +21,8 @@ class BarcodeScanner extends StatelessWidget {
1921
required this.onScanned,
2022
this.appBarTitle,
2123
this.child,
22-
this.centerTitle});
24+
this.centerTitle,
25+
this.barcodeAppBar});
2326

2427
@override
2528
Widget build(BuildContext context) {

lib/screens/web.dart

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// ignore: avoid_web_libraries_in_flutter
2-
import 'package:web/web.dart' as html;
31
import 'dart:ui_web' as ui;
42

53
import 'package:flutter/material.dart';
64
import 'package:simple_barcode_scanner/constant.dart';
75
import 'package:simple_barcode_scanner/enum.dart';
6+
// ignore: avoid_web_libraries_in_flutter
7+
import 'package:web/web.dart' as html;
8+
9+
import '../barcode_appbar.dart';
810

911
/// Barcode scanner for web using iframe
1012
class BarcodeScanner extends StatelessWidget {
@@ -16,6 +18,8 @@ class BarcodeScanner extends StatelessWidget {
1618
final String? appBarTitle;
1719
final bool? centerTitle;
1820
final Widget? child;
21+
final BarcodeAppBar? barcodeAppBar;
22+
1923
const BarcodeScanner({
2024
super.key,
2125
required this.lineColor,
@@ -26,6 +30,7 @@ class BarcodeScanner extends StatelessWidget {
2630
this.appBarTitle,
2731
this.centerTitle,
2832
this.child,
33+
this.barcodeAppBar,
2934
});
3035

3136
@override
@@ -55,10 +60,7 @@ class BarcodeScanner extends StatelessWidget {
5560
final width = MediaQuery.of(context).size.width;
5661
final height = width / (16 / 9);
5762
return Scaffold(
58-
appBar: AppBar(
59-
title: Text(appBarTitle ?? kScanPageTitle),
60-
centerTitle: centerTitle,
61-
),
63+
appBar: _buildAppBar(context),
6264
body: SingleChildScrollView(
6365
child: Column(
6466
children: [
@@ -75,4 +77,34 @@ class BarcodeScanner extends StatelessWidget {
7577
),
7678
);
7779
}
80+
81+
_buildAppBar(BuildContext context) {
82+
if (appBarTitle == null && barcodeAppBar == null) {
83+
return null;
84+
}
85+
if (barcodeAppBar != null) {
86+
return AppBar(
87+
title: barcodeAppBar?.appBarTitle != null
88+
? Text(barcodeAppBar!.appBarTitle!)
89+
: null,
90+
centerTitle: barcodeAppBar?.centerTitle ?? false,
91+
leading: barcodeAppBar?.enableBackButton == true
92+
? IconButton(
93+
onPressed: () => Navigator.pop(context),
94+
icon: barcodeAppBar?.backButtonIcon ??
95+
const Icon(Icons.arrow_back_ios))
96+
: null,
97+
automaticallyImplyLeading: false,
98+
);
99+
}
100+
return AppBar(
101+
title: Text(appBarTitle ?? kScanPageTitle),
102+
centerTitle: centerTitle,
103+
automaticallyImplyLeading: true,
104+
leading: IconButton(
105+
icon: const Icon(Icons.arrow_back_ios),
106+
onPressed: () => Navigator.pop(context),
107+
),
108+
);
109+
}
78110
}

lib/screens/window.dart

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'package:simple_barcode_scanner/constant.dart';
88
import 'package:simple_barcode_scanner/enum.dart';
99
import 'package:webview_windows/webview_windows.dart';
1010

11+
import '../barcode_appbar.dart';
12+
1113
class WindowBarcodeScanner extends StatelessWidget {
1214
final String lineColor;
1315
final String cancelButtonText;
@@ -16,6 +18,7 @@ class WindowBarcodeScanner extends StatelessWidget {
1618
final Function(String) onScanned;
1719
final String? appBarTitle;
1820
final bool? centerTitle;
21+
final BarcodeAppBar? barcodeAppBar;
1922

2023
const WindowBarcodeScanner({
2124
super.key,
@@ -26,6 +29,7 @@ class WindowBarcodeScanner extends StatelessWidget {
2629
required this.onScanned,
2730
this.appBarTitle,
2831
this.centerTitle,
32+
this.barcodeAppBar,
2933
});
3034

3135
@override
@@ -39,18 +43,7 @@ class WindowBarcodeScanner extends StatelessWidget {
3943
});
4044

4145
return Scaffold(
42-
appBar: AppBar(
43-
title: Text(appBarTitle ?? kScanPageTitle),
44-
centerTitle: centerTitle,
45-
leading: IconButton(
46-
onPressed: () {
47-
/// send close event to web-view
48-
controller.postWebMessage(json.encode({"event": "close"}));
49-
Navigator.pop(context);
50-
},
51-
icon: const Icon(Icons.arrow_back_ios),
52-
),
53-
),
46+
appBar: _buildAppBar(controller, context),
5447
body: FutureBuilder<bool>(
5548
future: initPlatformState(
5649
controller: controller,
@@ -155,4 +148,42 @@ class WindowBarcodeScanner extends StatelessWidget {
155148
}
156149
return true;
157150
}
151+
152+
_buildAppBar(WebviewController controller, BuildContext context) {
153+
if (appBarTitle == null && barcodeAppBar == null) {
154+
return null;
155+
}
156+
if (barcodeAppBar != null) {
157+
return AppBar(
158+
title: barcodeAppBar?.appBarTitle != null
159+
? Text(barcodeAppBar!.appBarTitle!)
160+
: null,
161+
centerTitle: barcodeAppBar?.centerTitle ?? false,
162+
leading: barcodeAppBar!.enableBackButton == true
163+
? IconButton(
164+
onPressed: () {
165+
/// send close event to web-view
166+
controller.postWebMessage(json.encode({"event": "close"}));
167+
Navigator.pop(context);
168+
},
169+
icon: barcodeAppBar?.backButtonIcon ??
170+
const Icon(Icons.arrow_back_ios),
171+
)
172+
: null,
173+
automaticallyImplyLeading: false,
174+
);
175+
}
176+
return AppBar(
177+
title: Text(appBarTitle ?? kScanPageTitle),
178+
centerTitle: centerTitle,
179+
leading: IconButton(
180+
onPressed: () {
181+
/// send close event to web-view
182+
controller.postWebMessage(json.encode({"event": "close"}));
183+
Navigator.pop(context);
184+
},
185+
icon: const Icon(Icons.arrow_back_ios),
186+
),
187+
);
188+
}
158189
}

lib/simple_barcode_scanner.dart

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library simple_barcode_scanner;
22

33
import 'package:flutter/material.dart';
4+
import 'package:simple_barcode_scanner/barcode_appbar.dart';
45
import 'package:simple_barcode_scanner/enum.dart';
56
import 'package:simple_barcode_scanner/screens/shared.dart';
67

@@ -20,11 +21,18 @@ class SimpleBarcodeScannerPage extends StatelessWidget {
2021
final ScanType scanType;
2122

2223
///AppBar Title
24+
@Deprecated(
25+
'Use BarcodeAppBar instead. This field will be removed in future versions.')
2326
final String? appBarTitle;
2427

28+
@Deprecated(
29+
'Use BarcodeAppBar instead. This field will be removed in future versions.')
30+
2531
///center Title
2632
final bool? centerTitle;
2733

34+
final BarcodeAppBar? barcodeAppBar;
35+
2836
/// Specify a child widget to be positioned beneath the scanner.
2937
/// This is beneficial when you need to include a customized text field
3038
/// for manual entry of barcode/QR code.
@@ -48,26 +56,34 @@ class SimpleBarcodeScannerPage extends StatelessWidget {
4856

4957
/// appBatTitle and centerTitle support in web and window only
5058
/// Remaining field support in only mobile devices
51-
const SimpleBarcodeScannerPage({
52-
super.key,
53-
this.lineColor = "#ff6666",
54-
this.cancelButtonText = "Cancel",
55-
this.isShowFlashIcon = false,
56-
this.scanType = ScanType.barcode,
57-
this.appBarTitle,
58-
this.centerTitle,
59-
this.child,
60-
});
59+
const SimpleBarcodeScannerPage(
60+
{super.key,
61+
this.lineColor = "#ff6666",
62+
this.cancelButtonText = "Cancel",
63+
this.isShowFlashIcon = false,
64+
this.scanType = ScanType.barcode,
65+
this.appBarTitle,
66+
this.centerTitle,
67+
this.child,
68+
this.barcodeAppBar});
6169

6270
@override
6371
Widget build(BuildContext context) {
72+
assert(
73+
(appBarTitle == null && centerTitle == null) ||
74+
barcodeAppBar == null ||
75+
(appBarTitle != null &&
76+
centerTitle != null &&
77+
barcodeAppBar == null),
78+
'Either provide both appBarTitle and centerTitle together, or provide barcodeAppBar, but not both.');
6479
return BarcodeScanner(
6580
lineColor: lineColor,
6681
cancelButtonText: cancelButtonText,
6782
isShowFlashIcon: isShowFlashIcon,
6883
scanType: scanType,
6984
appBarTitle: appBarTitle,
7085
centerTitle: centerTitle,
86+
barcodeAppBar: barcodeAppBar,
7187
child: child,
7288
onScanned: (res) {
7389
Navigator.pop(context, res);

0 commit comments

Comments
 (0)