我有一个类,其中有2个选项卡(使用DefaultTabController)。 AppBar具有“操作”按钮/文本,可以在每个选项卡中保存“编辑的文本”,它可以是tab0或tab1。两个选项卡都是独立的有状态小部件。我不知道如何在主类中按保存操作时如何从选项卡中获取EditedText / String?
我调查了类似的问题,但没有从中得到启发Flutter: send Data from TabBarView (StatefullWidgets) back to main Scaffold 非常感谢您的帮助。
class EditPocketMoney extends StatefulWidget {
@override
_EditPocketMoneyState createState() => _EditPocketMoneyState();
}
class _EditPocketMoneyState extends State<EditPocketMoney> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
backgroundColor: Theme.of(context).primaryColor,
appBar: AppBar(
elevation: 0.0,
automaticallyImplyLeading: false,
leading: IconButton(icon: Icon(Icons.clear,color: Color(0xffE44663),), onPressed: (){Navigator.pop(context);}),
title: Text('Edit',style: TextStyle(color: Colors.white,fontFamily: 'SF Pro Text',fontSize: 17.0, ),),
centerTitle: true,
actions: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: (){
},
child: Container(
margin:EdgeInsets.only(right:15.0 ),
child: Text('SAVE',style: TextStyle(color: Color(0xffE44663),fontFamily: 'SF Pro Text',fontSize: 17.0, ),)),
),
],
),
],
bottom: PreferredSize(
preferredSize: Size(MediaQuery.of(context).size.width*0.8, 45.0),
child: Container(
margin: EdgeInsets.only(left: 18.0,right: 18.0,top: 5.0),
child: TabBar(
unselectedLabelColor: Colors.white,
labelColor:Colors.black ,
labelPadding: EdgeInsets.all(8.0),
tabs: [
Center(child: Text('Per Day',style: TextStyle(fontSize: 15.0,fontWeight: FontWeight.w600),)),
Center(child: Text('Per Month',style: TextStyle(fontSize: 15.0,fontWeight: FontWeight.w600),)),
],
indicator: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: Colors.white,
),),
),
),
),
body: TabBarView(children: [
PocketMoneyPerDay(),
PocketMoneyEditPerMonth()
])
),
);
}
答案 0 :(得分:0)
您可以使用任何状态管理程序包,例如Provider。
为提供者https://medium.com/flutterpub/provider-state-management-in-flutter-d453e73537c5引用此链接
我已经给出了使用提供程序包的示例。
ChangeNotifier代码:
class Data extends ChangeNotifier {
TextEditingController tab_0_controller = TextEditingController();
TextEditingController tab_1_controller = TextEditingController();
}
Tab0 TextField代码:
TextField(
controller: Provider.of<Data>(context).tab_0_controller;
)