android 在按下提升按钮,它应该指向文本字段,但我面临着一个错误时,按下

w41d8nur  于 2022-12-09  发布在  Android
关注(0)|答案(1)|浏览(104)

按下提升按钮时,光标必须转到文本字段,但遇到如下错误:

Launching lib\main.dart on M2003J15SC in debug mode...
Parameter format not correct -
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:64987/N4nL0hg-cQU=/ws
E/ion     (13885): ioctl c0044901 failed with code -1: Invalid argument
I/vehicle_parkin(13885): Compiler allocated 4379KB to compile void android.view.ViewRootImpl.performTraversals()
I/vehicle_parkin(13885): ProcessProfilingInfo new_methods=1121 is saved saved_to_disk=1 resolve_classes_delay=8000

════════ Exception caught by gesture ═══════════════════════════════════════════
The following FormatException was thrown while handling a gesture:
Invalid number (at character 1)

^

When the exception was thrown, this was the stack
#0      int._handleFormatError (dart:core-patch/integers_patch.dart:129:7)
#1      int.parse (dart:core-patch/integers_patch.dart:55:14)
#2      _HomeState.build.showTextFields.<anonymous closure>
#3      State.setState
#4      _HomeState.build.showTextFields
#5      _InkResponseState.handleTap
#6      GestureRecognizer.invokeCallback
#7      TapGestureRecognizer.handleTapUp
#8      BaseTapGestureRecognizer._checkUp
#9      BaseTapGestureRecognizer.handlePrimaryPointer
#10     PrimaryPointerGestureRecognizer.handleEvent
#11     PointerRouter._dispatch
#12     PointerRouter._dispatchEventToRoutes.<anonymous closure>
#13     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#14     PointerRouter._dispatchEventToRoutes
#15     PointerRouter.route
#16     GestureBinding.handleEvent
#17     GestureBinding.dispatchEvent
#18     RendererBinding.dispatchEvent
#19     GestureBinding._handlePointerEventImmediately
#20     GestureBinding.handlePointerEvent
#21     GestureBinding._flushPointerEventQueue
#22     GestureBinding._handlePointerDataPacket
#23     _invoke1 (dart:ui/hooks.dart:167:13)
#24     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:341:7)
#25     _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
Handler: "onTap"
Recognizer: TapGestureRecognizer#38721
    debugOwner: GestureDetector
    state: possible
    won arena
    finalPosition: Offset(289.6, 338.4)
    finalLocalPosition: Offset(229.6, 25.0)
    button: 1
    sent tap down
════════════════════════════════════════════════════════════════════════════════
V/PhoneWindow(13885): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@81d85c9, this = DecorView@5cb64ce[MainActivity]
I/AssistStructure(13885): Flattened final assist data: 400 bytes, containing 1 windows, 3 views
D/InputConnectionAdaptor(13885): The input method toggled cursor monitoring on
D/MIUIInput(13885): [KeyEvent] ViewRootImpl KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=1215968560, downTime=1215968560, deviceId=-1, source=0x101, displayId=0 }
D/MIUIInput(13885): [KeyEvent] ViewRootImpl KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=1215968644, downTime=1215968560, deviceId=-1, source=0x101, displayId=0 }

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by gesture ═══════════════════════════════════════════
Invalid number (at character 1)

^
════════════════════════════════════════════════════════════════════════════════

在录音上的按钮光标必须移动到文本字段...我试过这样,这是我的源代码:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Home());
  }
}

class Home extends StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
   final GlobalKey _parentKey = GlobalKey();
  int countItems = 0;
  late TextEditingController textController;
  String? vehicle;
  //String? vehicleNumber; //no radio button will be selected
  //String vehicle = "bike"; //if you want to set default value
  @override
  void initState() {
    super.initState();
    textController = TextEditingController();
  }

  Widget build(BuildContext context) {
     void showTextFields() {
      setState(() {
        countItems = int.parse(textController.text);
      });
    }
    return Scaffold(
        appBar: AppBar(
          title: Text("GL-Complex"),
          backgroundColor: Color.fromARGB(255, 106, 19, 19),
        ),
        body: SingleChildScrollView(
            child: Container(
          padding: EdgeInsets.all(36),
          child: Column(children: [
            Text(
              "GL-Parking(Vehicles)",
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
            Divider(),
            RadioListTile(
              title: Text("Bike"),
              value: "bike",
              groupValue: vehicle,
              onChanged: (value) {
                setState(() {
                  vehicle = value.toString();
                });
              },
            ),
            RadioListTile(
              title: Text("Car"),
              value: "car",
              groupValue: vehicle,
              onChanged: (value) {
                setState(() {
                  vehicle = value.toString();
                });
              },
            ),
            const SizedBox(
              height: 10,
            ),
            Padding(
                padding: EdgeInsets.all(24),
                child: Column(children: <Widget>[
                  ElevatedButton(
                    onPressed: showTextFields,
                    child: Text("Check In"), //label text
                    style: ElevatedButton.styleFrom(
                      primary: Color.fromARGB(255, 106, 19, 19),
                      minimumSize: const Size.fromHeight(50),
                    ),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  ElevatedButton(
                    onPressed:showTextFields,
                    child: Text("Check Out"), //label text
                    style: ElevatedButton.styleFrom(
                      primary: Color.fromARGB(255, 106, 19, 19),
                      minimumSize: const Size.fromHeight(
                          50), //elevated btton background color
                    ),
                  ),
                ])),
                const SizedBox(
              height: 10,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 20, right: 20.0),
              child: TextField(
                controller: textController,
                decoration:
                    const InputDecoration( 
                    labelText: 'Vehicle Number'
                    ),   
              ),
            ),
           const SizedBox(
              height: 10,
            ),
            (countItems > 0)
                ? Flexible(
                    child: ListView.builder(
                        itemCount: countItems,
                        itemBuilder: (BuildContext context, int index) {
                          return TextField(
                            style: TextStyle(color: Colors.white),
                            cursorColor: Colors.white,
                            textAlign: TextAlign.center,
                            decoration: InputDecoration(
                              hintText: "Enter the Vehicle Number " + index.toString(),
                              hintStyle: TextStyle(color: Colors.grey[400]),
                            ),
                          );
                        }),
                  )
                : const SizedBox(),
               
             Container(
               color: Colors.grey[200],
               padding: EdgeInsets.all(24),
               child: Row(children: [
                Text(
                  "Print:",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(
                  width: 80,
                ),
               Expanded(
                  child: TextButton.icon(
                    onPressed: () {
                      
                    },
                    icon: Icon(Icons.print),
                    label: Text('Save&Print'),
                    style: TextButton.styleFrom(
                        primary: Colors.white, backgroundColor: Colors.green),
                  ),
                ),
             ]),
            )
          ]
          ),
          ),
        )
        );
  }
}

你能帮我解决这个问题吗?
请帮助我得到解决方案的代码有当按下它musth出现在一个文本字段:
例如:enter image description here

5hcedyr0

5hcedyr01#

您的问题描述不是很清楚,您试图将文本字段中的文本解析为整数,而该文本字段中没有任何内容,因此会发出错误。
如果只关注文本字段,可以在这里使用focus-node。我修改了代码来实现这一点,我注解掉了试图将字段中的文本解析为数字的部分。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Home());
  }
}

class Home extends StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final GlobalKey _parentKey = GlobalKey();
  int countItems = 0;
  late TextEditingController textController;
  String? vehicle;

  FocusNode focusNode = FocusNode();

  //String? vehicleNumber; //no radio button will be selected
  //String vehicle = "bike"; //if you want to set default value
  @override
  void initState() {
    super.initState();
    textController = TextEditingController();
  }

  void showTextFields() {
    focusNode.requestFocus();
    // setState(() {
    //   countItems = int.parse(textController.text);
    // });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("GL-Complex"),
          backgroundColor: Color.fromARGB(255, 106, 19, 19),
        ),
        body: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.all(36),
            child: Column(children: [
              Text(
                "GL-Parking(Vehicles)",
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
              Divider(),
              RadioListTile(
                title: Text("Bike"),
                value: "bike",
                groupValue: vehicle,
                onChanged: (value) {
                  setState(() {
                    vehicle = value.toString();
                  });
                },
              ),
              RadioListTile(
                title: Text("Car"),
                value: "car",
                groupValue: vehicle,
                onChanged: (value) {
                  setState(() {
                    vehicle = value.toString();
                  });
                },
              ),
              const SizedBox(
                height: 10,
              ),
              Padding(
                  padding: EdgeInsets.all(24),
                  child: Column(children: <Widget>[
                    ElevatedButton(
                      onPressed: showTextFields,
                      child: Text("Check In"), //label text
                      style: ElevatedButton.styleFrom(
                        primary: Color.fromARGB(255, 106, 19, 19),
                        minimumSize: const Size.fromHeight(50),
                      ),
                    ),
                    const SizedBox(
                      height: 10,
                    ),
                    ElevatedButton(
                      onPressed: showTextFields,
                      child: Text("Check Out"), //label text
                      style: ElevatedButton.styleFrom(
                        primary: Color.fromARGB(255, 106, 19, 19),
                        minimumSize:
                            const Size.fromHeight(50), //elevated btton background color
                      ),
                    ),
                  ])),
              const SizedBox(
                height: 10,
              ),
              Padding(
                padding: const EdgeInsets.only(left: 20, right: 20.0),
                child: TextField(
                  focusNode: focusNode,
                  controller: textController,
                  decoration: const InputDecoration(labelText: 'Vehicle Number'),
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              (countItems > 0)
                  ? Flexible(
                      child: ListView.builder(
                          itemCount: countItems,
                          itemBuilder: (BuildContext context, int index) {
                            return TextField(
                              style: TextStyle(color: Colors.white),
                              cursorColor: Colors.white,
                              textAlign: TextAlign.center,
                              decoration: InputDecoration(
                                hintText: "Enter the Vehicle Number " + index.toString(),
                                hintStyle: TextStyle(color: Colors.grey[400]),
                              ),
                            );
                          }),
                    )
                  : const SizedBox(),
              Container(
                color: Colors.grey[200],
                padding: EdgeInsets.all(24),
                child: Row(children: [
                  Text(
                    "Print:",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(
                    width: 80,
                  ),
                  Expanded(
                    child: TextButton.icon(
                      onPressed: () {},
                      icon: Icon(Icons.print),
                      label: Text('Save&Print'),
                      style: TextButton.styleFrom(
                          primary: Colors.white, backgroundColor: Colors.green),
                    ),
                  ),
                ]),
              )
            ]),
          ),
        ));
  }
}

我建议您设置一个if检查,如下所示:

if(textController.text.trim().isEmpty)

然后将其解析为数字。

相关问题