Flutter TextField输入内容后 保持光标在最后

发布于 3 年前 作者 phonegap100 1172 次浏览 最后一次编辑是 3 年前 来自 分享

Flutter TextField输入内容后 保持光标在最后

import 'package:flutter/material.dart';

class TextFieldFocusDemoPage extends StatefulWidget {
  TextFieldFocusDemoPage({Key key}) : super(key: key);

  _TextFieldDemoPageState createState() => _TextFieldDemoPageState();
}

class _TextFieldDemoPageState extends State<TextFieldFocusDemoPage> {

   TextEditingController inputController;   
   String _username="lisi";
   var _password;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //  删除数据的时候光标在最后面示例demo
    inputController  = TextEditingController.fromValue(
      TextEditingValue(
        text: _username,
        selection: TextSelection.fromPosition(
          TextPosition(
            affinity: TextAffinity.downstream,
            offset:_username.length

          )
        )
        
      )
    );
    
  
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('光标在最后面示例demo'),
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        // child:TextDemo() ,    
        child:Column(
          children: <Widget>[
            TextField(
              decoration: InputDecoration(
                hintText: "请输入用户名"
              ),
              controller: inputController,
              onChanged: (value){
                 setState(() {
                    _username=value; 
                 });
              },
              
            ),
            SizedBox(height: 10),
            TextField(
              obscureText: true,
              decoration: InputDecoration(
                hintText: "密码"
              ),             
              onChanged: (value){
                 setState(() {
                    this._password=value; 
                 });
              },
              
            ),
            SizedBox(height: 40),
            Container(
              width: double.infinity,
              height: 40,
              child: RaisedButton(
                child: Text("登录"),
                onPressed: (){
                  print(this._username);
                  print(this._password);
                },
                color: Colors.blue,
                textColor: Colors.white,
              ),
            )
          ],
        ) ,     
      )
    );
  }
}
回到顶部