【问题标题】:how to refresh parent page in flutter如何在颤动中刷新父页面
【发布时间】:2021-11-19 19:14:40
【问题描述】:

我正在开发一个带有颤振和带有rest api的mysql的应用程序。我必须页单元和章节。当我更新章节的任何信息时,它不会显示在列表视图的单元页面中。如何重新加载列表视图?我的代码:

对于 staffunit.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:intl/intl.dart';
import 'package:studyplanner/helper/get_helper.dart';
import 'package:studyplanner/widgets/teacher/staff_unit_tile.dart';


class Staffunit extends StatefulWidget {
  final String iClassid;
  final String iSubjid;
  final String iStaffid;
  final String iPaperid;
  final String paper;
  final String maxdateunit;
  final String cClass;

  Staffunit({
    this.iClassid,
    this.iSubjid,
    this.iStaffid,
    this.iPaperid,
    this.paper,
    this.maxdateunit,
    this.cClass,
  });

  @override
  _StaffunitState createState() => _StaffunitState();
}

class _StaffunitState extends State<Staffunit> {
  var Staffunit;

  @override
  void initState() {
    Staffunit = GetHelper.getData4(
        widget.iClassid,
        widget.iSubjid,
        widget.iStaffid,
        widget.iPaperid,
        'get_staff_unit',
        'iClassid',
        'iSubjid',
        'iStaffid',
        'iPaperid');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: (AppBar(
        iconTheme: IconThemeData(
          color: Colors.white,
        ),
        title: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.cClass,
              style: TextStyle(fontSize: 18, color: Colors.white),
            ),
            Text(
              widget.paper,
              style: TextStyle(color: Colors.black, fontSize: 10),
            ),
            Text(
              widget.maxdateunit != null
                  ? "Last Updated on : " +
                      DateFormat('dd-MM-yyyy')
                          .format(DateTime.parse(widget.maxdateunit))
                  : "",
              style: TextStyle(color: Colors.black, fontSize: 12),
            )
          ],
        ),
        elevation: 0.0,
        bottomOpacity: 0.0,
        actions: <Widget>[
          Padding(
            padding: EdgeInsets.only(right: 20.0),
            child: GestureDetector(
              onTap: () {
                Navigator.of(context).pushNamed(MainTeacherPage.routeName);
              },
              child: Icon(
                Icons.home,
                size: 26.0,
              ),
            ),
          ),
        ],
      )),
      body: Container(
        width: double.infinity,
        decoration: BoxDecoration(
            gradient: LinearGradient(begin: Alignment.topCenter, colors: [
          Color.fromRGBO(51, 169, 214, 1),
          Color.fromRGBO(255, 255, 255, 1),
        ])),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
              child: Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius:
                          BorderRadius.only(topRight: Radius.circular(100))),
                  padding: EdgeInsets.all(20),

                  // we will use future builder to show the data in a list view
                  // we put the future variable
                  // check if there is no show a message to user no data
                  // else show a list view with tiles that show our data
                  child: FutureBuilder(
                    future: Staffunit,
                    builder: (context, snapshots) {
                      // print(widget.iPaperid);
                      // print(snapshots.toString());
                      if (!snapshots.hasData || snapshots.data.length == 0) {
                        return Center(
                          child: Text(
                            'No Unit Available',
                            style: TextStyle(
                              color: Colors.black,
                              fontWeight: FontWeight.bold,
                              fontSize: 30,
                            ),
                          ),
                        );
                      }

                      return ListView.builder(
                        itemCount: snapshots.data.length,
                        itemBuilder: (context, index) {
                          return Staffunittile(
                            id: snapshots.data[index]['id'],
                            unit: snapshots.data[index]['cDesc'],
                            per: snapshots.data[index]['per'],
                            Alloted: snapshots.data[index]['Alloted'],
                            Completed: snapshots.data[index]['Completed'],
                            iClassid: widget.iClassid,
                            iSubjid: widget.iSubjid,
                            iStaffid: widget.iStaffid,
                            iPaperid: widget.iPaperid,
                            cClass: widget.cClass,
                            cIcon: snapshots.data[index]['cIcon'],
                          );
                        },
                      );
                    },
                  )),
            ),
          ],
        ),
      ),
    );
  }
}

对于 Staffunittile.dart

import 'package:flutter/material.dart';
import 'package:studyplanner/screens/teacher/staffchapter.dart';
import 'package:studyplanner/global_var.dart' as global_var;

class Staffunittile extends StatelessWidget {
  final String id;
  final String unit;
  final String per;
  final String iClassid;
  final String iSubjid;
  final String iStaffid;
  final String iPaperid;
  final String Alloted;
  final String Completed;
  final String maxdatechapter;
  final String cClass;
  final String cIcon;

  Staffunittile({
    this.id = "",
    this.unit = "",
    this.per = "",
    this.iClassid = "",
    this.iSubjid = "",
    this.iStaffid = "",
    this.iPaperid = "",
    this.Alloted = "",
    this.Completed = "",
    this.maxdatechapter,
    this.cClass,
    this.cIcon = "",
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(color: Colors.lightBlue.shade900),
            ),
          ),
          child: ListTile(
            leading: SizedBox(
              child: Image.network(
                global_var.logoimageurl + cIcon,
                fit: BoxFit.scaleDown,
              ),
              width: 60,
            ),
            // isThreeLine: true,
            title: Text(
              unit,
              style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold,
                fontSize: 15,
              ),
            ),
            subtitle: Text(
              "Completed : " + Completed + "/" + Alloted + " (" + per + " %)",
              style: TextStyle(
                color: Colors.blueAccent,
                fontSize: 15,
              ),
            ),
            trailing: Icon(
              Icons.keyboard_arrow_right,
              color: Colors.red,
            ),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => Staffchapter(
                          iStaffid: iStaffid,
                          iClassid: this.iClassid,
                          iSubjid: this.iSubjid,
                          iPaperid: this.iPaperid,
                          iUnitid: this.id,
                          unit: unit,
                          cClass: cClass,
                        )),
              );
            },
          ),
        ),
        // Divider(
        //   thickness: 0.5,
        // )
      ],
    );
  }
}

对于staffchapter.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:studyplanner/helper/get_helper.dart';
import 'package:studyplanner/widgets/teacher/staff_chapter_tile.dart';
import 'package:studyplanner/screens/teacher/main_teacher_page.dart';

class Staffchapter extends StatefulWidget {
  final String iClassid;
  final String iSubjid;
  final String iStaffid;
  final String iPaperid;
  final String iUnitid;
  final String unit;
  final String cClass;
  final String chapterid;

  Staffchapter({
    this.iClassid,
    this.iSubjid,
    this.iStaffid,
    this.iPaperid,
    this.iUnitid,
    this.unit,
    this.cClass,
    this.chapterid,
  });

  @override
  _StaffchapterState createState() => _StaffchapterState();
}

class _StaffchapterState extends State<Staffchapter> {
  var Staffchapter;

  @override
  void initState() {
    fetchdatach();
    super.initState();
  }

  fetchdatach() {
    Staffchapter = GetHelper.getData5(
      widget.iClassid,
      widget.iSubjid,
      widget.iStaffid,
      widget.iPaperid,
      widget.iUnitid,
      'get_staff_chapter',
      'iClassid',
      'iSubjid',
      'iStaffid',
      'iPaperid',
      'iUnitid',
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: (AppBar(
        iconTheme: IconThemeData(
          color: Colors.white,
        ),
        title: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.cClass,
              style: TextStyle(fontSize: 18, color: Colors.white),
            ),
            Text(
              widget.unit,
              style: TextStyle(color: Colors.black, fontSize: 10),
            ),
          ],
        ),
        elevation: 0.0,
        bottomOpacity: 0.0,
        actions: <Widget>[
          Padding(
            padding: EdgeInsets.only(right: 20.0),
            child: GestureDetector(
              onTap: () {
                // Staffchapter.dispose();
                Navigator.pop(context);
              },
              child: Icon(
                Icons.home,
                size: 26.0,
              ),
            ),
          ),
        ],
      )),
      body: Container(
        width: double.infinity,
        decoration: BoxDecoration(
            gradient: LinearGradient(begin: Alignment.topCenter, colors: [
          Color.fromRGBO(51, 169, 214, 1),
          Color.fromRGBO(255, 255, 255, 1),
        ])),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
              child: Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius:
                          BorderRadius.only(topRight: Radius.circular(100))),
                  padding: EdgeInsets.all(20),

                  // we will use future builder to show the data in a list view
                  // we put the future variable
                  // check if there is no show a message to user no data
                  // else show a list view with tiles that show our data
                  child: FutureBuilder(
                    future: Staffchapter,
                    builder: (context, snapshots) {
                      // print(widget.iUnitid);
                      // print(snapshots.toString());
                      if (!snapshots.hasData || snapshots.data.length == 0) {
                        return Center(
                          child: Text(
                            'No Chapter Available',
                            style: TextStyle(
                              color: Colors.black,
                              fontWeight: FontWeight.bold,
                              fontSize: 30,
                            ),
                          ),
                        );
                      }

                      return ListView.builder(
                        itemCount: snapshots.data.length,
                        itemBuilder: (context, index) {
                          // print(widget.iPaperid);
                          return Staffchaptertile(
                            id: snapshots.data[index]['id'],
                            ron: snapshots.data[index]['ron'],
                            chapter: snapshots.data[index]['cDesc'],
                            per: snapshots.data[index]['per'],
                            dUpdate: snapshots.data[index]['dUpdate'],
                            chapterid: snapshots.data[index]['chapterid'],
                            iClassid: widget.iClassid,
                            iSubjid: widget.iSubjid,
                            iPaperid: widget.iPaperid,
                            iUnitid: widget.iUnitid,
                            cClass: widget.cClass,
                          );
                        },
                      );
                    },
                  )),
            ),
          ],
        ),
      ),
    );
  }
}

对于员工章节

import 'package:flutter/material.dart';
import 'package:studyplanner/helper/get_helper.dart';
import 'package:intl/intl.dart';

class Staffchaptertile extends StatelessWidget {
  final String id;
  final String chapter;
  final String per;
  final String iClassid;
  final String iSubjid;
  final String iPaperid;
  final String iUnitid;
  final String ron;
  final String dUpdate;
  final String cClass;
  final String chapterid;

  Staffchaptertile({
    this.id = "",
    this.chapter = "",
    this.per = "",
    this.iClassid = "",
    this.iSubjid = "",
    this.iPaperid = "",
    this.iUnitid = "",
    this.ron = "",
    this.dUpdate = "",
    this.cClass = "",
    this.chapterid = "",
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ListTile(
          // isThreeLine: true,
          title: Text(
            ron.toString() + "." + chapter,
            style: TextStyle(
              color: Colors.black,
              fontSize: 15,
            ),
          ),
          subtitle: Text(
            dUpdate != ""
                ? "\n(" +
                    DateFormat('dd-MM-yyyy').format(DateTime.parse(dUpdate)) +
                    " )\n"
                : dUpdate,
            style: TextStyle(
              color: Colors.blueAccent,
              fontSize: 15,
            ),
          ),
          trailing: Icon(
            per.toString() == "100"
                ? Icons.check_box_outlined
                : Icons.check_box_outline_blank,
            color: Colors.red,
          ),
          onTap: () {
            if (this.dUpdate == "") {
              print(this.chapterid);
              GetHelper.askYseNo(
                context,
                "Mark as COMPLETED?",
                1,
                int.parse(this.chapterid),
              );
            }
          },
        ),
        Divider(
          thickness: 1.5,
        )
      ],
    );
  }
}

如何刷新单位列表?

【问题讨论】:

    标签: mysql flutter dart


    【解决方案1】:

    您可以在父窗口小部件中使用带有周期性计时器的StreamBuilder,也可以使用ProviderBLoC 等状态管理来管理应用的状态。

    这样,您可以轻松更新父窗口小部件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-30
      • 2020-12-08
      • 2022-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2021-10-19
      相关资源
      最近更新 更多