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/views/dictionaries/category/category_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/work/work_view.dart';
import 'package:satu/widgets/drawer/app_drawer.dart';

View File

@ -1,26 +1,49 @@
import 'package:flutter/material.dart';
import 'package:satu/shared/app_colors.dart';
class SettingItem extends StatefulWidget {
const SettingItem({Key? key, this.name, this.value, this.onTap})
: super(key: key);
class SettingItem extends StatelessWidget {
const SettingItem({
required this.title,
Key? key,
this.onPress,
this.subTitle,
}) : super(key: key);
final String? name;
final String? value;
final Function()? onTap;
final String title;
final String? subTitle;
@override
_SettingItemState createState() => _SettingItemState();
}
final Function()? onPress;
class _SettingItemState extends State<SettingItem> {
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(widget.name ?? ''),
subtitle: widget.value != null ? Text(widget.value ?? '') : null,
trailing: const Icon(Icons.chevron_right),
onTap: widget.onTap,
return Container(
decoration: const BoxDecoration(color: whiteColor),
child: Material(
color: Colors.transparent,
child: InkWell(
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/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 {
const PrinterSelectView({Key? key}) : super(key: key);
class PrinterSelect extends StatefulWidget {
const PrinterSelect({Key? key}) : super(key: key);
@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
void initState() {
bluetoothPrint = BluetoothPrint.instance;
if (WidgetsBinding.instance != null) {
WidgetsBinding.instance!.addPostFrameCallback((_) => initBluetooth());
}
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
Widget build(BuildContext context) {
return Scaffold(
body: Container(),
return MaterialApp(
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/services.dart';
import 'package:logger/logger.dart';
import 'package:satu/core/utils/logger.dart';
import 'package:satu/core/utils/pos_printer.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/shared/ui_helpers.dart';
import 'package:satu/widgets/bar/products_app_bar.dart';
import 'package:satu/widgets/bar/products_title_bar.dart';
import 'package:satu/widgets/fields/line_tile.dart';
class PrinterView extends StatefulWidget {
const PrinterView({Key? key}) : super(key: key);
@ -20,187 +13,50 @@ class PrinterView extends StatefulWidget {
}
class _PrinterViewState extends State<PrinterView> {
final Logger log = getLogger('_PrinterViewState');
BluetoothPrint? bluetoothPrint;
bool _connected = false;
BluetoothDevice? _device;
String tips = 'no device connect';
@override
void initState() {
bluetoothPrint = BluetoothPrint.instance;
if (WidgetsBinding.instance != null) {
WidgetsBinding.instance!.addPostFrameCallback((_) => initBluetooth());
}
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
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('BluetoothPrint example app'),
return Scaffold(
appBar: ProductsAppBar(
title: 'Принтер',
actions: [
IconButton(
onPressed: () {},
icon: const Icon(
Icons.print,
color: placeholderColor,
),
body: RefreshIndicator(
onRefresh: () =>
bluetoothPrint!.startScan(timeout: Duration(seconds: 4)),
child: SingleChildScrollView(
),
],
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding:
EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Text(tips),
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
verticalSpaceSmall,
LineTile(
'Устройство не выбрано',
onTap: () {},
labelText: 'Устройство печати',
),
verticalSpaceSmall,
LineTile(
'Размер не указан',
onTap: () {},
labelText: 'Размер чека',
),
verticalSpaceSmall,
LineTile(
'Кодировка не указана',
onTap: () {},
labelText: 'Кодировка печати',
),
],
),
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

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