@@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
77import 'package:flutter_gettext/flutter_gettext/context_ext.dart' ;
88import 'package:logger/logger.dart' ;
99import 'package:provider/provider.dart' ;
10+ import 'package:toastification/toastification.dart' ;
1011
1112import '../../services/device_manager.dart' ;
1213import '../../view_models/devices/device_discovery_view_model.dart' ;
@@ -246,31 +247,27 @@ class NewDeviceAddedSnackBarListener extends StatelessWidget {
246247 return Selector <DeviceDiscoveryViewModel , DeviceEntity ?>(
247248 selector: (_, viewModel) => viewModel.lastestAddedDevice,
248249 builder: (context, lastestAdded, child) {
249- final vm = context.read <DeviceDiscoveryViewModel >();
250250 if (lastestAdded != null ) {
251251 WidgetsBinding .instance.addPostFrameCallback ((_) {
252- ScaffoldMessenger .of (context)
253- .showSnackBar (
254- SnackBar (
255- duration: Duration (seconds: 3 ),
256- content: Text (
257- context.translate (
258- 'New device "{deviceName}" has been added.' ,
259- nArgs: {'deviceName' : lastestAdded.name},
260- ),
261- style: Theme .of (
262- context,
263- ).textTheme.bodyMedium? .copyWith (color: Theme .of (context).colorScheme.onTertiaryContainer),
264- ),
265- backgroundColor: Theme .of (context).colorScheme.tertiaryContainer,
266- ),
267- )
268- .closed
269- .then ((_) {
270- if (! vm.isDisposed) {
271- vm.clearAddedDevice ();
272- }
273- });
252+ toastification.dismissAll ();
253+ toastification.show (
254+ context: context,
255+ type: ToastificationType .success,
256+ style: ToastificationStyle .fillColored,
257+ title: Text (context.translate ('A new device has been added.' )),
258+ description: Text (lastestAdded.name),
259+ autoCloseDuration: const Duration (seconds: 3 ),
260+ icon: const Icon (Icons .device_hub),
261+ );
262+
263+ // 在 toast 关闭后清除状态
264+ /*
265+ Future.delayed(const Duration(seconds: 3), () {
266+ if (!vm.isDisposed) {
267+ vm.clearAddedDevice();
268+ }
269+ });
270+ */
274271 });
275272 }
276273 return child! ;
@@ -329,71 +326,74 @@ class DeviceDiscoveryScreen extends StatelessWidget {
329326 Navigator .of (context).pop (vm.newDeviceCount > 0 );
330327 }
331328 },
332- child: Column (
333- crossAxisAlignment: CrossAxisAlignment .stretch,
334- mainAxisSize: MainAxisSize .max,
335- children: [
336- Container (
337- color: Theme .of (context).colorScheme.surface,
338- child: Column (
339- crossAxisAlignment: CrossAxisAlignment .start,
340- children: [
341- // Input Fields
342- SmartConfigFormPanel (),
343- SizedBox (height: 8 ),
344- // Start/Stop Button
345- Container (
346- padding: EdgeInsets .symmetric (horizontal: 16 , vertical: 8 ),
347- width: double .infinity,
348- child: StartStopButton (),
349- ),
350- SizedBox (height: 16 ),
351- ],
352- ),
353- ),
354- ValueListenableBuilder <List <SupportedDeviceDescriptor >>(
355- valueListenable: vm.discoveredDevices,
356- builder: (context, devices, child) => devices.isNotEmpty ? child! : const SizedBox (height: 0 ),
357- child: Container (
358- padding: const EdgeInsets .fromLTRB (16 , 0 , 16 , 8 ),
359- child: Text (context.translate ('Discovered Devices:' ), style: Theme .of (context).textTheme.titleMedium),
329+ child: GestureDetector (
330+ onTap: () => FocusScope .of (context).unfocus (),
331+ child: Column (
332+ crossAxisAlignment: CrossAxisAlignment .stretch,
333+ mainAxisSize: MainAxisSize .max,
334+ children: [
335+ Container (
336+ color: Theme .of (context).colorScheme.surface,
337+ child: Column (
338+ crossAxisAlignment: CrossAxisAlignment .start,
339+ children: [
340+ // Input Fields
341+ SmartConfigFormPanel (),
342+ SizedBox (height: 8 ),
343+ // Start/Stop Button
344+ Container (
345+ padding: EdgeInsets .symmetric (horizontal: 16 , vertical: 8 ),
346+ width: double .infinity,
347+ child: StartStopButton (),
348+ ),
349+ SizedBox (height: 16 ),
350+ ],
351+ ),
360352 ),
361- ),
362- Expanded (
363- child: ValueListenableBuilder <List <SupportedDeviceDescriptor >>(
353+ ValueListenableBuilder <List <SupportedDeviceDescriptor >>(
364354 valueListenable: vm.discoveredDevices,
365- builder:
366- (context, value, child) => ListView .separated (
367- separatorBuilder:
368- (BuildContext context, int index) => DecoratedBox (
369- decoration: BoxDecoration (color: Theme .of (context).colorScheme.surfaceContainer),
370- child: Divider (height: 1 , indent: 16 , color: Theme .of (context).colorScheme.surface),
371- ),
372- itemCount: vm.discoveredDevices.value.length,
373- itemBuilder: (context, index) {
374- final user = vm.discoveredDevices.value[index];
375- return ListTile (
376- tileColor: Theme .of (context).colorScheme.surfaceContainer,
377- title: Text (user.name, style: Theme .of (context).textTheme.bodyLarge),
378- subtitle: Text (user.address.toString (), style: Theme .of (context).textTheme.bodySmall),
379- trailing: Consumer <DeviceDiscoveryViewModel >(
380- builder:
381- (context, vm, child) => IconButton .filledTonal (
382- onPressed:
383- vm.isBusy || vm.isDiscovering
384- ? null
385- : () => _showAddDeviceSheet (context, vm, vm.discoveredDevices.value[index]),
386- icon: child as Icon ,
387- ),
388- child: Icon (Icons .add_outlined),
389- ),
390- );
391- },
392- ),
355+ builder: (context, devices, child) => devices.isNotEmpty ? child! : const SizedBox (height: 0 ),
356+ child: Container (
357+ padding: const EdgeInsets .fromLTRB (16 , 0 , 16 , 8 ),
358+ child: Text (context.translate ('Discovered Devices:' ), style: Theme .of (context).textTheme.titleMedium),
359+ ),
393360 ),
394- ),
395- NewDeviceAddedSnackBarListener (child: SizedBox ()),
396- ],
361+ Expanded (
362+ child: ValueListenableBuilder <List <SupportedDeviceDescriptor >>(
363+ valueListenable: vm.discoveredDevices,
364+ builder:
365+ (context, value, child) => ListView .separated (
366+ separatorBuilder:
367+ (BuildContext context, int index) => DecoratedBox (
368+ decoration: BoxDecoration (color: Theme .of (context).colorScheme.surfaceContainer),
369+ child: Divider (height: 1 , indent: 16 , color: Theme .of (context).colorScheme.surface),
370+ ),
371+ itemCount: vm.discoveredDevices.value.length,
372+ itemBuilder: (context, index) {
373+ final user = vm.discoveredDevices.value[index];
374+ return ListTile (
375+ tileColor: Theme .of (context).colorScheme.surfaceContainer,
376+ title: Text (user.name, style: Theme .of (context).textTheme.bodyLarge),
377+ subtitle: Text (user.address.toString (), style: Theme .of (context).textTheme.bodySmall),
378+ trailing: Consumer <DeviceDiscoveryViewModel >(
379+ builder:
380+ (context, vm, child) => IconButton .filledTonal (
381+ onPressed:
382+ vm.isBusy || vm.isDiscovering
383+ ? null
384+ : () => _showAddDeviceSheet (context, vm, vm.discoveredDevices.value[index]),
385+ icon: child as Icon ,
386+ ),
387+ child: Icon (Icons .add_outlined),
388+ ),
389+ );
390+ },
391+ ),
392+ ),
393+ ),
394+ NewDeviceAddedSnackBarListener (child: SizedBox ()),
395+ ],
396+ ),
397397 ),
398398 );
399399 }
0 commit comments