diff --git a/lib/views/main/main_view.dart b/lib/views/main/main_view.dart index 79da11a..bb7aa89 100644 --- a/lib/views/main/main_view.dart +++ b/lib/views/main/main_view.dart @@ -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'; diff --git a/lib/views/settings/component/setting_item.dart b/lib/views/settings/component/setting_item.dart index 67c26b3..0f3acd1 100644 --- a/lib/views/settings/component/setting_item.dart +++ b/lib/views/settings/component/setting_item.dart @@ -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 { @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, + ), + ), + ], + ), + ), + ), ), ); } diff --git a/lib/views/settings/printer_bluetooth/printer_select.dart b/lib/views/settings/printer_bluetooth/printer_select.dart index 0e6bdc6..fb0c37d 100644 --- a/lib/views/settings/printer_bluetooth/printer_select.dart +++ b/lib/views/settings/printer_bluetooth/printer_select.dart @@ -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 { +class _PrinterSelectState extends State { + 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 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: [ + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Padding( + padding: + EdgeInsets.symmetric(vertical: 10, horizontal: 10), + child: Text(tips), + ), + ], + ), + const Divider(), + StreamBuilder>( + 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: [ + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + 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 config = Map(); + await bluetoothPrint! + .rawBytes(config, await getReceipt()); + } + : null, + child: const Text('print receipt(esc) - text'), + ), + OutlinedButton( + onPressed: _connected + ? () async { + Map config = Map(); + await bluetoothPrint! + .rawBytes(config, await getReceiptImg()); + } + : null, + child: const Text('print label(esc) img'), + ), + ], + ), + ) + ], + ), + ), + ), + floatingActionButton: StreamBuilder( + 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))); + } + }, + ), + ), ); } } diff --git a/lib/views/settings/printer_bluetooth/printer_view.dart b/lib/views/settings/printer_bluetooth/printer_view.dart index 58a20ed..8b1a293 100644 --- a/lib/views/settings/printer_bluetooth/printer_view.dart +++ b/lib/views/settings/printer_bluetooth/printer_view.dart @@ -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,186 +13,49 @@ class PrinterView extends StatefulWidget { } class _PrinterViewState extends State { - 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 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'), - ), - body: RefreshIndicator( - onRefresh: () => - bluetoothPrint!.startScan(timeout: Duration(seconds: 4)), - child: SingleChildScrollView( - child: Column( - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: - EdgeInsets.symmetric(vertical: 10, horizontal: 10), - child: Text(tips), - ), - ], - ), - const Divider(), - StreamBuilder>( - 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: [ - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - 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 config = Map(); - await bluetoothPrint! - .rawBytes(config, await getReceipt()); - } - : null, - child: const Text('print receipt(esc) - text'), - ), - OutlinedButton( - onPressed: _connected - ? () async { - Map config = Map(); - await bluetoothPrint! - .rawBytes(config, await getReceiptImg()); - } - : null, - child: const Text('print label(esc) img'), - ), - ], - ), - ) - ], + return Scaffold( + appBar: ProductsAppBar( + title: 'Принтер', + actions: [ + IconButton( + onPressed: () {}, + icon: const Icon( + Icons.print, + color: placeholderColor, ), ), - ), - floatingActionButton: StreamBuilder( - 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))); - } - }, + ], + ), + body: SingleChildScrollView( + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + verticalSpaceSmall, + LineTile( + 'Устройство не выбрано', + onTap: () {}, + labelText: 'Устройство печати', + ), + verticalSpaceSmall, + LineTile( + 'Размер не указан', + onTap: () {}, + labelText: 'Размер чека', + ), + verticalSpaceSmall, + LineTile( + 'Кодировка не указана', + onTap: () {}, + labelText: 'Кодировка печати', + ), + ], ), ), ); diff --git a/lib/views/settings/setting_view.dart b/lib/views/settings/setting_view.dart index d86a69a..db3c8b2 100644 --- a/lib/views/settings/setting_view.dart +++ b/lib/views/settings/setting_view.dart @@ -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(); + @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); + }, + ), + ], ), ), );