微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter 如何在同一个屏幕上显示表单数据?

如何解决Flutter 如何在同一个屏幕上显示表单数据?

如何在不使用任何其他包的情况下在显示表单的同一屏幕上显示表格中的表单数据?

用户输入宽度和长度并且验证正常时,我希望数据显示在存在表单的同一页面上,并在添加矩形和重置按钮下的表格中显示数据。

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // debugShowCheckedModeBanner: false,title: 'Lab 5',theme: ThemeData(
        primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,),home: Scaffold(
        appBar: AppBar(
          title: Text("Rectangles"),backgroundColor: Colors.red,body: MyHomePage(),);
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  final _scaffoldkey = GlobalKey<ScaffoldState>();
  final _formKey = GlobalKey<FormState>();

  String _height;
  String _width;
  String _length;

  void _showdata(){
    if (_formKey.currentState.validate()) {
      _formKey.currentState.save();
    }else{
      Scaffold.of(context).showSnackBar(
        SnackBar(
          content: Text('Invalid form values!!'),duration: Duration(seconds: 3),);
    }
  }


  @override
  Widget build(BuildContext context) {
    return Form(
      key: _formKey,child: Padding(
        padding: const EdgeInsets.all(16.0),child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,children: <Widget>[
            TextFormField(
              keyboardType: TextInputType.numberWithOptions(),cursorColor: Colors.red,decoration: Inputdecoration(
                hintText: 'Enter the Width',hintStyle: TextStyle(color: Colors.grey),labelText: 'Width',labelStyle: TextStyle(color: Colors.red),validator: (value) {
                if (value.isEmpty || value.contains('0') || value.contains('-')) {
                  return 'Invalid Input';
                }
                return null;
              },onSaved: (value) => _width = value,TextFormField(
              keyboardType: TextInputType.numberWithOptions(),decoration: Inputdecoration(
                hintText: 'Enter the Length',labelText: 'Length',onSaved: (value) => _length = value,Padding(
              padding: const EdgeInsets.all(20.0),child: Row(
                children: [
                  RaisedButton(
                    onpressed: _showdata,child: Text("Add Rectangle",style: TextStyle(color: Colors.white),color: Colors.red,SizedBox(width: 70),RaisedButton(
                    onpressed: (){},textColor: Colors.white,child: Text("Reset"),],);
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。