标签栏应用栏中的数据向其子对象发送

时间:2019-09-16 11:44:53

标签: flutter dart

我创建了一个带有标签栏的应用。我想在appbar中的文本字段中将数据传输给其子级。数据用于对子视图中的列表视图进行排序。因此它也应该更改用户界面

下面是代码

class OrderFragment extends StatefulWidget {

@override
OrderFragmentState createState() => OrderFragmentState();

}
class Constants{
static const String Date = 'Date';
static const String Booking_Id = 'Booking Id';
static const String Confirmed = 'Confirmed';

static const List<String> choices = <String>[
Date,
Booking_Id ,
Confirmed
];
}

class OrderFragmentState extends State<OrderFragment> {
Icon cusIcon = Icon(Icons.search);
TextEditingController _nameFieldController = new TextEditingController();
Widget cusSearchBar = Text('Orders',style: TextStyle(
  fontWeight: FontWeight.bold,color: Colors.white),
 );


 int state = 0;
  DateTime selectedDate = new DateTime.now();
 String formattedDate;
 bool _isTextFieldVisible = true;
 String bookId1;

final List<OrderWidgets> widgetList = [
OrderWidgets(
  'New',
  OrderFragment()
),
OrderWidgets(
  'Conformed',
  Center(
    child: Text('Conformed'),

  ),
),
OrderWidgets(
  'Cancelled',
  Center(
    child: Text('Cancelled'),
  ),
  ),
  ];

 @override
 Widget build(BuildContext context) {

 return DefaultTabController(

  length: 3,
  child: Scaffold(
    appBar: PreferredSize(
      preferredSize: Size.fromHeight(90.0),

      child: AppBar(

        actions: <Widget>[
          PopupMenuButton<String>(
            icon: Icon(Icons.more_vert),
            onSelected: choiceAction,
            itemBuilder: (BuildContext context){
              return Constants.choices.map((String choice){
                return PopupMenuItem<String>(
                  value: choice,
                  child: Text(choice),
                );
              }).toList();
            },
          )
        ],
        iconTheme: IconThemeData(
          color: Colors.white,
        ),
        title: cusSearchBar,
        bottom: TabBar(

          labelColor: Colors.white,
          tabs: <Widget>[
            Tab(
              text: 'New',

            ),
            Tab(
              text: 'Confirmed'
            ),
            Tab(
              text: 'Cancelled',
            )
          ],
        ),
      ),
    ),
    body: TabBarView(
      children: <Widget>[
        NewOrdersFragment(),
        ConfirmedOrdersFragment(),
        CancelOrdersFragment(),
       ],       ),     ),   ); }
   Future<Null> selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
    context: context,
    initialDate: selectedDate,
    firstDate: DateTime(2015, 8),
    lastDate: DateTime(2101));
    if (picked != null && picked != selectedDate)
    setState(() {
    selectedDate = picked;
    formattedDate = DateFormat('yyyy-MM-dd').format(selectedDate);
     });

      print('selected date  is '+ formattedDate.toString());
         }
       void choiceAction(String choice){
     if(choice == Constants.Confirmed){
      print('Confirmed');

        }else if(choice == Constants.Booking_Id){
           print('Booking_Id');
          setState(() {
         this.cusSearchBar = TextField(
       textInputAction: TextInputAction.go,
       decoration: InputDecoration(
        border: InputBorder.none,
        hintText: "Search here",
      ),
      style: TextStyle(
        color: Colors.white,
        fontSize: 16.0
      ),

      onChanged:(text){
        bookId1 = text;
        print(bookId1);    },
       );
       });

     }else if(choice == Constants.Date){
     print('Date');
    selectDate(context);
     print(selectedDate);
      var route =new  MaterialPageRoute(
     builder: (BuildContext context) => new ConfirmedOrdersFragment(),

  );
}
}}

这是选项卡代码,我想将bookid1转移到其子屏幕,如何在通过navigator.push发送数据时获取它。其他任何方式都无法正常工作。

0 个答案:

没有答案