我想在我的应用程序中添加一个复选框,因此我尝试将父级转换为无状态小部件,但是,这导致应用程序的所有格式都被破坏。有没有办法在有状态小部件中添加无状态代码?
答案 0 :(得分:1)
根据您的问题,这可能有效
class Test1 extends StatefulWidget {
@override
_Test1State createState() => _Test1State();
}
class _Test1State extends State<Test1> {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Test app',
home: new Scaffold(
appBar: new AppBar(
title: new Text("THis is stateful"),
),
body: new Test2(),
),
);
}
}
class Test2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text("This is stateless"),
),
);
}
}
答案 1 :(得分:0)
也许将父小部件用作有状态小部件,并为您的复选框和表单创建一个新的无状态小部件,然后通过导入在有状态小部件中使用它。