父组件:

ElevatedButton(
          onPressed: () {
            Navigator.of(context).push(MaterialPageRoute(
                builder: (context) => FormPage(title: 'form page!')));
          },
          style: ButtonStyle(
              padding: MaterialStateProperty.all(
                  const EdgeInsets.fromLTRB(20, 10, 20, 10))),
          child: const Text('传值按钮'),
        )

子组件的StatelessWidget模式:

class FormPage extends StatelessWidget {
  late String title;
  FormPage({Key? key, this.title = "form"}) : super(key: key);
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: ListView(
        children: const [
          ListTile(
            title: Text('表单内容'),
          )
        ],
      ),
    );
  }
}

子组件的StatefulWidget模式:

class FormPage extends StatefulWidget {
  String title;
  FormPage({Key? key, required this.title}) : super(key: key);

  
  // ignore: no_logic_in_create_state
  State<FormPage> createState() => _FormPageState(title: title);
}

class _FormPageState extends State<FormPage> {
  String title;
  _FormPageState({required this.title});
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView(
        children: const [
          ListTile(
            title: Text('表单内容'),
          )
        ],
      ),
    );
    ;
  }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐