这是 main.dart 文件:
import 'package:flutter/material.dart';
import 'package:untitled/button.dart';
import 'package:untitled/form.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[MyForm(), MyButton()],
),
),
);
}
}
这是 form.dart 文件:
import 'package:flutter/material.dart';
class MyForm extends StatelessWidget {
@override
Widget build(BuildContext context) {
TextEditingController x = TextEditingController();
return Container(
child: Center(
child: TextField(
controller: x,
decoration: InputDecoration(labelText: "Name"),
),
),
);
}
}
这是 button.dart 文件:
import 'package:flutter/material.dart';
class MyButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: ElevatedButton(
onPressed: () {
//if I press this button the textediting field will show 10
},
child: Text("Pass 10"),
),
),
);
}
}
- 如果我按下 button.dart 中的按钮,如何在文本编辑字段中显示数据?
- 做这些事情的最佳方法是什么?
- 我应该在 statelesswidget 中使用 statefulwidgets 还是在 statefulwidgets 中使用 statelesswidgets?
我的想法是使用redux或provider来传递这种东西。虽然我不知道这个简单的东西是否需要状态管理系统。