如何在颤振上调用类?

时间:2021-03-16 16:04:45

标签: android flutter

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Main Page"),
      ),
      body: Center(
        child: Column(
          children: [
            Container(
              height: 160,
              width: 500,
              margin: EdgeInsets.all(100),
              padding: EdgeInsets.only(top: 60),
              child: Container(color: Colors.blue),
            ),
          ],
        ),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  TextEditingController nameController = TextEditingController();
  TextEditingController ageController = TextEditingController();
  TextEditingController addressController = TextEditingController();
  TextEditingController weightController = TextEditingController();
  TextEditingController heightController = TextEditingController();
  TextEditingController traitController = TextEditingController();
  TextEditingController appearanceController = TextEditingController();
  String fullName = '';
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.lightBlue,
      ),
      home: Scaffold(
          appBar: AppBar(
            title: Text('Input Information'),
          ),
          drawer: Drawer(
            child: ListView(
              padding: EdgeInsets.zero,
              children: <Widget>[
                DrawerHeader(
                  child: Text('Drawer Header'),
                  decoration: BoxDecoration(
                    color: Colors.blueGrey,
                  ),
                ),
                ListTile(
                  title: Text('Item 1'),
                  onTap: () {
                    Navigator.pop(context);
                  },
                ),
                ListTile(
                  title: Text('Item 2'),
                  onTap: () {
                    Navigator.pop(context);
                  },
                ),
              ],
            ),
          ),
          body: Center(
              child: Column(children: <Widget>[
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: nameController,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Full Name',
                  ),
                )),
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: ageController,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Age',
                  ),
                )),
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: addressController,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Address',
                  ),
                )),
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: weightController,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Weight',
                  ),
                )),
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: heightController,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Height',
                  ),
                )),
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: traitController,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Unique Trait',
                  ),
                )),
            // Container(
            //     margin: EdgeInsets.all(20),
            //     child: TextField(
            //       controller: appearanceController,
            //       decoration: InputDecoration(
            //         border: OutlineInputBorder(),
            //         labelText: 'Apperance',
            //       ),
            //     )),
            Builder(
              builder: (context) => ElevatedButton(
                child: Text("Next"),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => SecondRoute()),
                  );
                },
              ),
            ),
            Container(
              margin: EdgeInsets.all(20),
              child: Text(fullName),
            )
          ]))),
    );
  }
}

class SecondRoute extends StatelessWidget {
  TextEditingController phill1Controller = TextEditingController();
  TextEditingController phill2Controller = TextEditingController();
  TextEditingController phill3Controller = TextEditingController();
  TextEditingController phill4Controller = TextEditingController();
  // String fullName = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Medication"),
        ),
        body: Center(
          child: Column(children: [
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: phill1Controller,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Phill 1',
                  ),
                )),
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: phill2Controller,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Phill 2',
                  ),
                )),
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: phill3Controller,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Phill 3',
                  ),
                )),
            Container(
                margin: EdgeInsets.all(20),
                child: TextField(
                  controller: phill4Controller,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Phill 4',
                  ),
                )),
            ElevatedButton(
                child: Text("Save"),
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => _MainPageState()),
                );
              },
          ),
         ],
      )),
    );
}

enter image description here

发生此错误(红色下划线)。这是代码的全部内容。 如何从另一个地方调用 _MainPageState? “class _MainPageState extends State ”这种格式和“class MyPage extends StatefulWidget”有什么区别?因为当我从另一个地方调用 SecondRoute 时,它​​很好,但是当我用 State<~~> 格式调用 class 时,就像这样“class _MainPageState extends State {”,它似乎不起作用。

非常感谢所有解决问题的人!

1 个答案:

答案 0 :(得分:0)

_MainPageState 是一个私有类(它的名字以下划线开头)定义了你的 StatefulWidget 的状态。您不会导航到状态,而是导航到小部件。

您可能想:

Navigator.push(context, MaterialPageRoute(builder: (context) => MyPage()));