【问题标题】:How to call class on flutter?如何在颤振上打电话?
【发布时间】:2021-06-13 22:57:18
【问题描述】:
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()),
                );
              },
          ),
         ],
      )),
    );
}

发生此错误(红色下划线)。这一直是代码的一部分。 如何从另一个地方调用 _MainPageState? “class _MainPageState extends State ”这种格式和“class MyPage extends StatefulWidget”有什么区别吗?因为当我从另一个地方调用 SecondRoute 时,这很好,但是当我调用具有 State 格式的类时,例如 "class _MainPageState extends State {" ,它似乎不起作用。

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

【问题讨论】:

  • 您通常不会导航到状态,而是导航到主类。我认为你应该重命名你的类,我发现那里有一些异常。

标签: android flutter


【解决方案1】:

_MainPageState 是一个私有类(其名称以下划线开头),定义了 StatefulWidget 的状态。您不是导航到 State,而是导航到 Widget。

您可能想要:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 2019-11-25
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2018-11-21
    • 2021-03-22
    相关资源
    最近更新 更多