Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 我的平面按钮就在文本字段的下方,当键盘显示时,文本字段会隐藏起来_Flutter_Logic_Textfield - Fatal编程技术网

Flutter 我的平面按钮就在文本字段的下方,当键盘显示时,文本字段会隐藏起来

Flutter 我的平面按钮就在文本字段的下方,当键盘显示时,文本字段会隐藏起来,flutter,logic,textfield,Flutter,Logic,Textfield,例如,在flatterdev上提供的一个包中,我做了一些编辑。下面是代码。如何让我的底部按钮在点击文本字段时立即显示 (另一个文件中已经定义了p.S.BottomButton,不用担心。我尝试过这样做-如果你愿意,请参见底部-但没有完成工作。) 代码如下: //library international_phone_input; import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'

例如,在flatterdev上提供的一个包中,我做了一些编辑。下面是代码。如何让我的
底部按钮
在点击
文本字段
时立即显示

(另一个文件中已经定义了p.S.
BottomButton
,不用担心。我尝试过这样做-如果你愿意,请参见底部-但没有完成工作。)

代码如下:

//library international_phone_input;

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:international_phone_input/src/phone_service.dart';

import 'country.dart';
import 'package:byteseal/components/bottom_button.dart';
import 'package:byteseal/screens/otp_screen1.dart';

class InternationalPhoneInput extends StatefulWidget {
  final void Function(String phoneNumber, String internationalizedPhoneNumber,
      String isoCode) onPhoneNumberChange;
  final String initialPhoneNumber;
  final String initialSelection;
  final String errorText;
  final String hintText;
  final String labelText;
  final TextStyle errorStyle;
  final TextStyle hintStyle;
  final TextStyle labelStyle;
  final int errorMaxLines;
  final List<String> enabledCountries;
  final InputDecoration decoration;
  final bool showCountryCodes;
  final bool showCountryFlags;
  final Widget dropdownIcon;
  final InputBorder border;

  InternationalPhoneInput(
      {this.onPhoneNumberChange,
      this.initialPhoneNumber,
      this.initialSelection,
      this.errorText,
      this.hintText,
      this.labelText,
      this.errorStyle,
      this.hintStyle,
      this.labelStyle,
      this.enabledCountries = const [],
      this.errorMaxLines,
      this.decoration,
      this.showCountryCodes = true,
      this.showCountryFlags = true,
      this.dropdownIcon,
      this.border});

  static Future<String> internationalizeNumber(String number, String iso) {
    return PhoneService.getNormalizedPhoneNumber(number, iso);
  }

  @override
  _InternationalPhoneInputState createState() =>
      _InternationalPhoneInputState();
}

class _InternationalPhoneInputState extends State<InternationalPhoneInput> {
  Country selectedItem;
  List<Country> itemList = [];

  String errorText;
  String hintText;
  String labelText;

  TextStyle errorStyle;
  TextStyle hintStyle;
  TextStyle labelStyle;

  int errorMaxLines;

  bool hasError = false;
  bool showCountryCodes;
  bool showCountryFlags;

  InputDecoration decoration;
  Widget dropdownIcon;
  InputBorder border;

  _InternationalPhoneInputState();

  final phoneTextController = TextEditingController();

  @override
  void initState() {
    errorText = widget.errorText ?? 'Please enter a valid phone number';
    hintText = widget.hintText ?? 'eg. 244056345';
    labelText = widget.labelText;
    errorStyle = widget.errorStyle;
    hintStyle = widget.hintStyle;
    labelStyle = widget.labelStyle;
    errorMaxLines = widget.errorMaxLines;
    decoration = widget.decoration;
    showCountryCodes = widget.showCountryCodes;
    showCountryFlags = widget.showCountryFlags;
    dropdownIcon = widget.dropdownIcon;

    phoneTextController.addListener(_validatePhoneNumber);
    phoneTextController.text = widget.initialPhoneNumber;

    _fetchCountryData().then((list) {
      Country preSelectedItem;

      if (widget.initialSelection != null) {
        preSelectedItem = list.firstWhere(
            (e) =>
                (e.code.toUpperCase() ==
                    widget.initialSelection.toUpperCase()) ||
                (e.dialCode == widget.initialSelection.toString()),
            orElse: () => list[0]);
      } else {
        preSelectedItem = list[0];
      }

      setState(() {
        itemList = list;
        selectedItem = preSelectedItem;
      });
    });

    super.initState();
  }

  _validatePhoneNumber() {
    String phoneText = phoneTextController.text;
    if (phoneText != null && phoneText.isNotEmpty) {
      PhoneService.parsePhoneNumber(phoneText, selectedItem.code)
          .then((isValid) {
        setState(() {
          hasError = !isValid;
        });

        if (widget.onPhoneNumberChange != null) {
          if (isValid) {
            PhoneService.getNormalizedPhoneNumber(phoneText, selectedItem.code)
                .then((number) {
              widget.onPhoneNumberChange(phoneText, number, selectedItem.code);
            });
          } else {
            widget.onPhoneNumberChange('', '', selectedItem.code);
          }
        }
      });
    }
  }

  Future<List<Country>> _fetchCountryData() async {
    var list = await DefaultAssetBundle.of(context)
        .loadString('packages/international_phone_input/assets/countries.json');
    List<dynamic> jsonList = json.decode(list);

    List<Country> countries = List<Country>.generate(jsonList.length, (index) {
      Map<String, String> elem = Map<String, String>.from(jsonList[index]);
      if (widget.enabledCountries.isEmpty) {
        return Country(
            name: elem['en_short_name'],
            code: elem['alpha_2_code'],
            dialCode: elem['dial_code'],
            flagUri: 'assets/flags/${elem['alpha_2_code'].toLowerCase()}.png');
      } else if (widget.enabledCountries.contains(elem['alpha_2_code']) ||
          widget.enabledCountries.contains(elem['dial_code'])) {
        return Country(
            name: elem['en_short_name'],
            code: elem['alpha_2_code'],
            dialCode: elem['dial_code'],
            flagUri: 'assets/flags/${elem['alpha_2_code'].toLowerCase()}.png');
      } else {
        return null;
      }
    });

    countries.removeWhere((value) => value == null);

    return countries;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          DropdownButtonHideUnderline(
            child: Padding(
              padding: EdgeInsets.only(top: 8),
              child: DropdownButton<Country>(
                value: selectedItem,
                icon: Padding(
                  padding:
                      EdgeInsets.only(bottom: (decoration != null) ? 6 : 0),
                  child: dropdownIcon ?? Icon(Icons.arrow_drop_down),
                ),
                onChanged: (Country newValue) {
                  setState(() {
                    selectedItem = newValue;
                  });
                  _validatePhoneNumber();
                },
                items: itemList.map<DropdownMenuItem<Country>>((Country value) {
                  return DropdownMenuItem<Country>(
                    value: value,
                    child: Container(
                      padding: const EdgeInsets.only(bottom: 5.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          if (showCountryFlags) ...[
                            Image.asset(
                              value.flagUri,
                              width: 32.0,
                              package: 'international_phone_input',
                            )
                          ],
                          if (showCountryCodes) ...[
                            SizedBox(width: 4),
                            Text(value.dialCode, style: TextStyle(color: Colors.white),)
                          ]
                        ],
                      ),
                    ),
                  );
                }).toList(),
              ),
            ),
          ),
          Flexible(
              child: TextField(
            keyboardType: TextInputType.phone,
            style: TextStyle(
              color: Colors.white,
            ),
            controller: phoneTextController,
            decoration: decoration ??
                InputDecoration(
                  hintText: hintText,
                  labelText: labelText,
                  errorText: hasError ? errorText : null,
                  hintStyle: hintStyle ?? null,
                  errorStyle: errorStyle ?? null,
                  labelStyle: labelStyle,
                  errorMaxLines: errorMaxLines ?? 3,
                  border: border ?? null,
                ),
                onTap: () {
                     setState(() {
                       BottomButton(
                           buttonTitle: 'Next',
                           onTap: (info) {
                             Navigator.push(
                                 context,
                                 MaterialPageRoute(
                                   builder: (context) => OTPScreen(number: phoneTextController.text, iso: info),
                                 )
                             );
                           });
                     });
                },
                // onTap: BottomButton(
                //     buttonTitle: 'Next',
                //     onTap: () {
                //       Navigator.push(
                //           context,
                //           MaterialPageRoute(
                //             builder: (context) => OTPScreen(number: phoneTextController.value, iso: selectedItem),
                //           )
                //       );
                //     }),
          ))
        ],
      ),
    );
  }
}
//图书馆国际电话输入;
导入“dart:async”;
导入“dart:convert”;
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
导入'package:international_phone_input/src/phone_service.dart';
输入“国家.省道”;
导入“package:byteseal/components/bottom_button.dart”;
导入“包装:byteseal/screens/otp_screen1.dart”;
类InternationalPhoneInput扩展StatefulWidget{
最终void函数(字符串phoneNumber、字符串国际化phoneNumber、,
字符串等位码)onPhoneNumberChange;
最后一个字符串initialPhoneNumber;
最终字符串初始值选择;
最终字符串错误文本;
最终字符串hintText;
最终字符串标签文本;
最终文本样式错误样式;
最终文本样式hintStyle;
最终文本样式标签样式;
最终输入错误最大线;
最终名单启用国家;
最终投入装饰装修;
最终布尔代码;
最后的国旗;
最终窗口小部件下拉图标;
最终输入边界;
国际电话输入(
{this.onPhoneNumberChange,
这是我的电话号码,
这是第一次选择,
这个.errorText,
this.hintText,
这是labelText,
这种风格,
这是辛茨泰勒,
这是labelStyle,
this.enabledCountries=const[],
这是我的最大行,
这个,装修,,
this.showCountryCodes=true,
this.showcontryflags=true,
这是dropdownIcon,
这条边界});
静态Future internationalizeNumber(字符串编号、字符串iso){
返回PhoneService.getNormalizedPhoneNumber(数字,iso);
}
@凌驾
_InternationalPhoneInputState createState()=>
_InternationalPhoneInputState();
}
类_InternationalPhoneInputState扩展状态{
国家选择项目;
列表项列表=[];
字符串错误文本;
字符串hintText;
字符串标签文本;
文本样式错误样式;
文本样式hintStyle;
文本样式标签样式;
int errorMaxLines;
bool hasrerror=false;
布尔代码;
bool showCountryFlags;
输入装饰;
控件下拉图标;
输入边界;
_InternationalPhoneInputState();
final phoneTextController=TextEditingController();
@凌驾
void initState(){
errorText=widget.errorText???“请输入有效的电话号码”;
hintText=widget.hintText??'如244056345';
labelText=widget.labelText;
errorStyle=widget.errorStyle;
hintStyle=widget.hintStyle;
labelStyle=widget.labelStyle;
errorMaxLines=widget.errorMaxLines;
装饰=widget.decoration;
showCountryCodes=widget.showCountryCodes;
showCountryFlags=widget.showCountryFlags;
dropdownIcon=widget.dropdownIcon;
phoneTextController.addListener(_validatePhoneNumber);
phoneTextController.text=widget.initialPhoneNumber;
_fetchCountryData()。然后((列表){
国家预选;
if(widget.initialSelection!=null){
preSelectedItem=list.firstWhere(
(e) =>
(e.code.toUpperCase()==
widget.initialSelection.toUpperCase())||
(e.dialCode==widget.initialSelection.toString()),
orElse:()=>列表[0]);
}否则{
preSelectedItem=列表[0];
}
设置状态(){
itemList=列表;
selectedItem=预选editem;
});
});
super.initState();
}
_validatePhoneNumber(){
字符串phoneText=phoneTextController.text;
if(phoneText!=null&&phoneText.isNotEmpty){
PhoneService.parsePhoneNumber(phoneText,selectedItem.code)
.然后((有效){
设置状态(){
hasError=!isValid;
});
if(widget.onPhoneNumberChange!=null){
如果(有效){
PhoneService.getNormalizedPhoneNumber(phoneText,selectedItem.code)
.然后((数字){
onPhoneNumberChange(phoneText、number、selectedItem.code);
});
}否则{
widget.onPhoneNumberChange('','',selectedItem.code);
}
}
});
}
}
Future\u fetchCountryData()异步{
var list=await DefaultAssetBundle.of(上下文)
.loadString('packages/international_phone_input/assets/countries.json');
List jsonList=json.decode(List);
列表国家=List.generate(jsonList.length,(索引){
Map elem=Map.from(jsonList[index]);
if(widget.enabledCountries.isEmpty){
返回国(
名称:elem['en_short_name'],
代码:elem['alpha_2_code'],
拨号码:elem['dial_code'],
flagUri:'assets/flags/${elem['alpha_2_code'].toLowerCase()}.png');
}else if(widget.enabledCountries.contains(elem['alpha_2_code']))||
widget.enabledCountries.contains(元素['dial_code'])){
返回国(
名称:elem['en_short_name'],
代码:elem['alpha_2_code'],
拨号码:elem['dial_code'],
flagUri:'assets/flags/${elem['alpha_2_code'].toLowerCase()}.png');
}否则{
返回null;
}
});
countries.removeWhere((value)=>value==null);
返回国;
}
@凌驾
小部件构建(构建上下文){
返回容器(
孩子:排(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
下拉按钮侧下划线(
孩子:填充(
填充:仅限边缘设置(顶部:8),
孩子:下拉按钮(
值:selectedItem
bool buttonVisible;
FocusNode _focus = new FocusNode();

@override
void initState() {
    super.initState();
    buttonVisible = false;
    _focus.addListener(_changeVisibility);
}

void _changeVisibility() {
    setState(() {
       buttonVisible = true;
     });
}
Column(
    children: <Widget>[
        TextField(
            focusNode: _focus,
            ),
         Visibility(
           visible: buttonVisible,
           child: RaisedButton(
             onPressed: () { },
             child: Text("My Button")
              ),
         ),
    ]
)