printer setting ui

null-safety-migration
error500 2021-10-04 10:54:33 +06:00
parent 068374e794
commit 4140e7c2fe
5 changed files with 289 additions and 221 deletions

View File

@ -6,7 +6,7 @@ import 'package:satu/core/services/navigator_service.dart';
import 'package:satu/core/utils/locator.dart'; import 'package:satu/core/utils/locator.dart';
import 'package:satu/views/dictionaries/category/category_view.dart'; import 'package:satu/views/dictionaries/category/category_view.dart';
import 'package:satu/views/dictionaries/goods/goods_view.dart'; import 'package:satu/views/dictionaries/goods/goods_view.dart';
import 'package:satu/views/settings/printer_bluetooth/printer_select.dart'; import 'package:satu/views/settings/printer_bluetooth/printer_view.dart';
import 'package:satu/views/settings/setting_view.dart'; import 'package:satu/views/settings/setting_view.dart';
import 'package:satu/views/work/work_view.dart'; import 'package:satu/views/work/work_view.dart';
import 'package:satu/widgets/drawer/app_drawer.dart'; import 'package:satu/widgets/drawer/app_drawer.dart';

View File

@ -1,26 +1,49 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:satu/shared/app_colors.dart';
class SettingItem extends StatefulWidget { class SettingItem extends StatelessWidget {
const SettingItem({Key? key, this.name, this.value, this.onTap}) const SettingItem({
: super(key: key); required this.title,
Key? key,
this.onPress,
this.subTitle,
}) : super(key: key);
final String? name; final String title;
final String? value; final String? subTitle;
final Function()? onTap;
@override final Function()? onPress;
_SettingItemState createState() => _SettingItemState();
}
class _SettingItemState extends State<SettingItem> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Card( return Container(
child: ListTile( decoration: const BoxDecoration(color: whiteColor),
title: Text(widget.name ?? ''), child: Material(
subtitle: widget.value != null ? Text(widget.value ?? '') : null, color: Colors.transparent,
trailing: const Icon(Icons.chevron_right), child: InkWell(
onTap: widget.onTap, onTap: onPress,
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(fontSize: 12, color: textColor),
),
if( subTitle !=null )
Text(
subTitle ?? '',
style: const TextStyle(
fontSize: 10,
color: placeholderColor,
),
),
],
),
),
),
), ),
); );
} }

View File

@ -1,23 +1,207 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.dart';
import 'package:charset_converter/charset_converter.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:logger/logger.dart';
import 'package:satu/core/utils/logger.dart';
import 'package:satu/core/utils/pos_printer.dart';
class PrinterSelectView extends StatefulWidget { class PrinterSelect extends StatefulWidget {
const PrinterSelectView({Key? key}) : super(key: key); const PrinterSelect({Key? key}) : super(key: key);
@override @override
_PrinterSelectViewState createState() => _PrinterSelectViewState(); _PrinterSelectState createState() => _PrinterSelectState();
} }
class _PrinterSelectViewState extends State<PrinterSelectView> { class _PrinterSelectState extends State<PrinterSelect> {
final Logger log = getLogger('_PrinterViewState');
BluetoothPrint? bluetoothPrint;
bool _connected = false;
BluetoothDevice? _device;
String tips = 'no device connect';
@override @override
void initState() { void initState() {
bluetoothPrint = BluetoothPrint.instance;
if (WidgetsBinding.instance != null) {
WidgetsBinding.instance!.addPostFrameCallback((_) => initBluetooth());
}
super.initState(); super.initState();
} }
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initBluetooth() async {
bluetoothPrint!.startScan(timeout: Duration(seconds: 4));
final bool isConnected = await bluetoothPrint!.isConnected ?? false;
bluetoothPrint!.state.listen((state) {
print('cur device status: $state');
switch (state) {
case BluetoothPrint.CONNECTED:
setState(() {
_connected = true;
tips = 'connect success';
});
break;
case BluetoothPrint.DISCONNECTED:
setState(() {
_connected = false;
tips = 'disconnect success';
});
break;
default:
break;
}
});
if (!mounted) return;
if (isConnected) {
setState(() {
_connected = true;
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return MaterialApp(
body: Container(), home: Scaffold(
appBar: AppBar(
title: const Text('BluetoothPrint example app'),
),
body: RefreshIndicator(
onRefresh: () =>
bluetoothPrint!.startScan(timeout: Duration(seconds: 4)),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding:
EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Text(tips),
),
],
),
const Divider(),
StreamBuilder<List<BluetoothDevice>>(
stream: bluetoothPrint!.scanResults,
initialData: const [],
builder: (c, snapshot) => Column(
children: (snapshot.data ?? [])
.map((d) => ListTile(
title: Text(d.name ?? ''),
subtitle: Text(d.address ?? ''),
onTap: () async {
setState(() {
_device = d;
});
},
trailing: _device != null &&
_device!.address == d.address
? const Icon(
Icons.check,
color: Colors.green,
)
: null,
))
.toList(),
),
),
const Divider(),
Container(
padding: EdgeInsets.fromLTRB(20, 5, 20, 10),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
OutlinedButton(
onPressed: _connected
? null
: () async {
if (_device != null &&
_device!.address != null) {
await bluetoothPrint!.connect(_device!);
} else {
setState(() {
tips = 'please select device';
});
print('please select device');
}
},
child: const Text('connect'),
),
SizedBox(width: 10.0),
OutlinedButton(
onPressed: _connected
? () async {
await bluetoothPrint!.disconnect();
}
: null,
child: const Text('disconnect'),
),
],
),
OutlinedButton(
onPressed: _connected
? () async {
Map<String, dynamic> config = Map();
await bluetoothPrint!
.rawBytes(config, await getReceipt());
}
: null,
child: const Text('print receipt(esc) - text'),
),
OutlinedButton(
onPressed: _connected
? () async {
Map<String, dynamic> config = Map();
await bluetoothPrint!
.rawBytes(config, await getReceiptImg());
}
: null,
child: const Text('print label(esc) img'),
),
],
),
)
],
),
),
),
floatingActionButton: StreamBuilder<bool>(
stream: bluetoothPrint!.isScanning,
initialData: false,
builder: (c, snapshot) {
if (snapshot.data != null) {
return FloatingActionButton(
onPressed: () => bluetoothPrint!.stopScan(),
backgroundColor: Colors.red,
child: const Icon(Icons.stop),
);
} else {
return FloatingActionButton(
child: const Icon(Icons.search),
onPressed: () => bluetoothPrint!
.startScan(timeout: const Duration(seconds: 4)));
}
},
),
),
); );
} }
} }

View File

@ -1,16 +1,9 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:bluetooth_print/bluetooth_print.dart';
import 'package:bluetooth_print/bluetooth_print_model.dart';
import 'package:charset_converter/charset_converter.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:satu/shared/app_colors.dart';
import 'package:satu/shared/ui_helpers.dart';
import 'package:logger/logger.dart'; import 'package:satu/widgets/bar/products_app_bar.dart';
import 'package:satu/core/utils/logger.dart'; import 'package:satu/widgets/bar/products_title_bar.dart';
import 'package:satu/core/utils/pos_printer.dart'; import 'package:satu/widgets/fields/line_tile.dart';
class PrinterView extends StatefulWidget { class PrinterView extends StatefulWidget {
const PrinterView({Key? key}) : super(key: key); const PrinterView({Key? key}) : super(key: key);
@ -20,186 +13,49 @@ class PrinterView extends StatefulWidget {
} }
class _PrinterViewState extends State<PrinterView> { class _PrinterViewState extends State<PrinterView> {
final Logger log = getLogger('_PrinterViewState');
BluetoothPrint? bluetoothPrint;
bool _connected = false;
BluetoothDevice? _device;
String tips = 'no device connect';
@override @override
void initState() { void initState() {
bluetoothPrint = BluetoothPrint.instance;
if (WidgetsBinding.instance != null) {
WidgetsBinding.instance!.addPostFrameCallback((_) => initBluetooth());
}
super.initState(); super.initState();
} }
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initBluetooth() async {
bluetoothPrint!.startScan(timeout: Duration(seconds: 4));
final bool isConnected = await bluetoothPrint!.isConnected ?? false;
bluetoothPrint!.state.listen((state) {
print('cur device status: $state');
switch (state) {
case BluetoothPrint.CONNECTED:
setState(() {
_connected = true;
tips = 'connect success';
});
break;
case BluetoothPrint.DISCONNECTED:
setState(() {
_connected = false;
tips = 'disconnect success';
});
break;
default:
break;
}
});
if (!mounted) return;
if (isConnected) {
setState(() {
_connected = true;
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return Scaffold(
home: Scaffold( appBar: ProductsAppBar(
appBar: AppBar( title: 'Принтер',
title: const Text('BluetoothPrint example app'), actions: [
), IconButton(
body: RefreshIndicator( onPressed: () {},
onRefresh: () => icon: const Icon(
bluetoothPrint!.startScan(timeout: Duration(seconds: 4)), Icons.print,
child: SingleChildScrollView( color: placeholderColor,
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding:
EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Text(tips),
),
],
),
const Divider(),
StreamBuilder<List<BluetoothDevice>>(
stream: bluetoothPrint!.scanResults,
initialData: const [],
builder: (c, snapshot) => Column(
children: (snapshot.data ?? [])
.map((d) => ListTile(
title: Text(d.name ?? ''),
subtitle: Text(d.address ?? ''),
onTap: () async {
setState(() {
_device = d;
});
},
trailing: _device != null &&
_device!.address == d.address
? const Icon(
Icons.check,
color: Colors.green,
)
: null,
))
.toList(),
),
),
const Divider(),
Container(
padding: EdgeInsets.fromLTRB(20, 5, 20, 10),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
OutlinedButton(
onPressed: _connected
? null
: () async {
if (_device != null &&
_device!.address != null) {
await bluetoothPrint!.connect(_device!);
} else {
setState(() {
tips = 'please select device';
});
print('please select device');
}
},
child: const Text('connect'),
),
SizedBox(width: 10.0),
OutlinedButton(
onPressed: _connected
? () async {
await bluetoothPrint!.disconnect();
}
: null,
child: const Text('disconnect'),
),
],
),
OutlinedButton(
onPressed: _connected
? () async {
Map<String, dynamic> config = Map();
await bluetoothPrint!
.rawBytes(config, await getReceipt());
}
: null,
child: const Text('print receipt(esc) - text'),
),
OutlinedButton(
onPressed: _connected
? () async {
Map<String, dynamic> config = Map();
await bluetoothPrint!
.rawBytes(config, await getReceiptImg());
}
: null,
child: const Text('print label(esc) img'),
),
],
),
)
],
), ),
), ),
), ],
floatingActionButton: StreamBuilder<bool>( ),
stream: bluetoothPrint!.isScanning, body: SingleChildScrollView(
initialData: false, child: Column(
builder: (c, snapshot) { crossAxisAlignment: CrossAxisAlignment.stretch,
if (snapshot.data != null) { children: [
return FloatingActionButton( verticalSpaceSmall,
onPressed: () => bluetoothPrint!.stopScan(), LineTile(
backgroundColor: Colors.red, 'Устройство не выбрано',
child: const Icon(Icons.stop), onTap: () {},
); labelText: 'Устройство печати',
} else { ),
return FloatingActionButton( verticalSpaceSmall,
child: const Icon(Icons.search), LineTile(
onPressed: () => bluetoothPrint! 'Размер не указан',
.startScan(timeout: const Duration(seconds: 4))); onTap: () {},
} labelText: 'Размер чека',
}, ),
verticalSpaceSmall,
LineTile(
'Кодировка не указана',
onTap: () {},
labelText: 'Кодировка печати',
),
],
), ),
), ),
); );

View File

@ -2,30 +2,35 @@ import 'package:flutter/material.dart';
import 'package:satu/core/services/navigator_service.dart'; import 'package:satu/core/services/navigator_service.dart';
import 'package:satu/core/utils/locator.dart'; import 'package:satu/core/utils/locator.dart';
import 'package:satu/routes/route_names.dart'; import 'package:satu/routes/route_names.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/widgets/bar/products_app_bar.dart'; import 'package:satu/widgets/bar/products_app_bar.dart';
import 'package:satu/widgets/bar/products_title_bar.dart';
import 'component/setting_item.dart'; import 'component/setting_item.dart';
class SettingsView extends StatelessWidget { class SettingsView extends StatelessWidget {
final NavigatorService _navigatorService = locator<NavigatorService>(); final NavigatorService _navigatorService = locator<NavigatorService>();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: const ProductsAppBar(title: 'Настройки', drawerShow: true,), appBar: const ProductsAppBar(
body: Padding( title: 'Настройки',
padding: const EdgeInsets.symmetric( horizontal: 8.0 ), drawerShow: true,
child: SingleChildScrollView( ),
child: Column( body: SingleChildScrollView(
children: [ child: Column(
SettingItem( crossAxisAlignment: CrossAxisAlignment.stretch,
name: 'Принтер', children: [
value: 'не выбран', const ProductsTitleBarBar(title: 'Разделы'),
onTap: (){ SettingItem(
_navigatorService.push(settingPrinterBluetoothViewRoute); title: 'Принтер',
} subTitle: 'Настройка печати чеков',
) onPress: () {
] _navigatorService.push(settingPrinterBluetoothViewRoute);
), },
),
],
), ),
), ),
); );