flutter 制作电话拨号器应用程序,如何链接浮动操作按钮,使其打开一个文本字段,我可以在其中输入数字

jaql4c8m  于 2023-01-18  发布在  Flutter
关注(0)|答案(1)|浏览(110)

我试过创建一个返回TextField的函数,然后把函数名放在onPressed参数中,但是当我按下按钮时,除了确认按钮已被按下外,即使在终端中也没有任何React。
我只是一个星期左右的flutter学习和stackoverflow完全新的,所以请原谅我是愚蠢的。这是我的代码:-

import 'package:flutter/material.dart';

void main() {
  runApp(
    dialer(),
  );
}

class dialer extends StatefulWidget {
  const dialer({super.key});

  @override
  State<dialer> createState() => _dialerState();
}

txtfld(onPressed) {
  return Container(
    child: TextField(
      keyboardType: TextInputType.phone,
    ),
  );
}

class _dialerState extends State<dialer> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        backgroundColor: Colors.black,
        appBar: AppBar(
          backgroundColor: Colors.black,
          actions: <Widget>[
            Icon(
              Icons.search,
              size: 30,
            ),
            Icon(
              Icons.more_vert,
              size: 30,
            ),
            Padding(
              padding: EdgeInsets.all(5),
            ),
          ],
        ),
        body: Column(
          children: <Widget>[
            Flexible(
              flex: 3,
              fit: FlexFit.tight,
              child: Container(
                margin: EdgeInsets.only(top: 50),
                child: Text(
                  'Phone',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 30,
                  ),
                ),
              ),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.end,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Container(),
                    Container(
                      height: 80,
                      width: 314,
                      color: Color.fromARGB(255, 59, 59, 59),
                      padding: EdgeInsets.all(10),
                      child: Text(
                        '94693-68171',
                        style: TextStyle(
                          color: Colors.grey,
                        ),
                        textScaleFactor: 1.25,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 5,
                ),
                Row(
                  children: <Widget>[
                    Container(),
                    Container(
                      height: 80,
                      width: 314,
                      color: Color.fromARGB(255, 59, 59, 59),
                      padding: EdgeInsets.all(10),
                      child: Text(
                        '696969696969',
                        style: TextStyle(color: Colors.grey),
                        textScaleFactor: 1.25,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 5,
                ),
                Row(
                  children: <Widget>[
                    Container(
                      height: 80,
                      width: 314,
                      color: Color.fromARGB(255, 59, 59, 59),
                      padding: EdgeInsets.all(10),
                      child: Text(
                        '4204204204',
                        style: TextStyle(color: Colors.grey),
                        textScaleFactor: 1.25,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 5,
                ),
                Row(
                  children: <Widget>[
                    Container(
                      height: 80,
                      width: 314,
                      color: Color.fromARGB(255, 59, 59, 59),
                      padding: EdgeInsets.all(10),
                      child: Text(
                        '6666666666',
                        style: TextStyle(color: Colors.grey),
                        textScaleFactor: 1.25,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 5,
                ),
                Row(
                  children: <Widget>[
                    Container(
                      height: 80,
                      width: 314,
                      color: Color.fromARGB(255, 59, 59, 59),
                      padding: EdgeInsets.all(10),
                      child: Text(
                        '1234567890',
                        style: TextStyle(color: Colors.grey),
                        textScaleFactor: 1.25,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 5,
                ),
                Row(
                  children: <Widget>[
                    Container(
                      height: 80,
                      width: 314,
                      color: Color.fromARGB(255, 59, 59, 59),
                      padding: EdgeInsets.all(10),
                      child: Text(
                        'Elon musk',
                        style: TextStyle(color: Colors.grey),
                        textScaleFactor: 1.25,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 5,
                ),
              ],
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.call),
          onPressed: (() => txtfld),
          elevation: 12,
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      ),
    );
  }
}
vjrehmav

vjrehmav1#

试试这个:为你的文本域定义一个焦点节点,记住一旦onTap触发了它,就像下面这样处理它

late FocusNode commentFocus;

 @override
  void dispose() {
    commentFocus.dispose();
   
  }

@override
initState(){
   commentFocus = FocusNode();
 super.initState();
}

...
txtfld(onPressed) {
  return Container(
    child: TextField(
       focusNode: commentFocus,
      keyboardType: TextInputType.phone,
    ),
  );
}
...


floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.call),
          onPressed: (){
          commentFocus.requestFocus();///Trigger here. 
          },
          elevation: 12,
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,

相关问题