flutter中如果使用TextButton页面报错或警告要重启一下

参考代码-(因为我也是第一次学习,所以记录一下坑,然后再说一下,vscode运行时热更新)
尽量不要直接在控制台输入 flutter run -d chrome --web-port 3838 这种的要手动刷新r或者R才行
要使用run code执行代码或者左侧的爬虫按钮执行,顺便看一下vscode的设置
打开设置搜索
Dart: Flutter Hot Reload On Save
image

import 'package:flutter/material.dart';

void main(List<String> args) {
  runApp(mainPage());
}

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

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

class _mainPageState extends State<mainPage> {
  TextEditingController _iphone = TextEditingController();
  TextEditingController _password = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(centerTitle: true, title: Text('TextField登录')),
        body: Container(
          padding: EdgeInsets.all(20),
          color: Colors.white,
          child: Column(
            children: [
              TextField(
                controller: _iphone,
                // maxLength: 11,
                decoration: InputDecoration(
                  filled: true,
                  fillColor: Color.fromARGB(255, 100, 240, 200),
                  contentPadding: EdgeInsets.only(left: 20),
                  hintText: '请输入手机号',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(16),
                    borderSide: BorderSide.none,
                  ),
                ),
              ),
              SizedBox(height: 40),
              TextField(
                controller: _password,
                obscureText: true,
                decoration: InputDecoration(
                  filled: true,
                  fillColor: Color.fromARGB(255, 100, 240, 200),
                  contentPadding: EdgeInsets.only(left: 20),
                  hintText: '请输入密码',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(16),
                    borderSide: BorderSide.none,
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 20),
                width: double.infinity,
                height: 50,
                decoration: BoxDecoration(
                  color: Colors.black,
                  borderRadius: BorderRadius.circular(20),
                ),
                child: TextButton(
                  onPressed: () =>
                      print('当前手机号${_iphone.text},密码${_password.text}'),
                  child: Text('登录', style: TextStyle(color: Colors.white)),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
posted @ 2026-04-19 00:20  jialiangzai  阅读(5)  评论(0)    收藏  举报