简体   繁体   English

Flutter 视图未更新

[英]Flutter view not updating

I have a view in flutter.我对 flutter 有看法。 The code for which is below.代码如下。 The problem is that on calling setState() to update the view leads to only partial updates.问题是调用setState()来更新视图只会导致部分更新。 Using the value of selectedDate in the build() function works correctly.build() function 中使用 selectedDate 的值可以正常工作。 But calling the _loadHtml() underneath that does not reflect the change in the date.但是调用下面的_loadHtml()并不能反映日期的变化。 Could someone help me with this or tell me what I am doing wrong?有人可以帮我解决这个问题或告诉我我做错了什么吗?

class somePage extends StatefulWidget {
  static const String routeName = '/somePage';

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

class _somePageState extends State<somePage> {
  DateTime selectedDate = DateTime.now().toLocal();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        ...
        body: LayoutReadings(
            selectedDate: '${selectedDate.year}-${selectedDate.month}-${selectedDate.day}'
        ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _selectDate(context),
        tooltip: 'Select Date',
        child: Icon(Icons.calendar_today_sharp),
      ),
    );
  }

  Future<void> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2021, 2),
        lastDate: DateTime(2031));
    if (picked != null && picked != selectedDate) {
      setState(() {
        selectedDate = picked;
      });
    }
  }

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

class LayoutSomePage extends StatefulWidget {
  final String selectedDate;
  WebViewController _controller;

  LayoutSomePage({this.selectedDate});

  _LayoutSomePageState createState() => _LayoutSomePageState();

}

class _LayoutSomePageState extends State<LayoutSomePage> {

  @override
  Widget build(BuildContext context) {
    print('layout called with date: ' + widget.selectedDate); // works correctly
    return new WebView(
      initialUrl: 'about:blank',
      onWebViewCreated: (WebViewController webViewController) {
        widget._controller = webViewController;
        _loadHtml();
      },
    );
  }

  void _loadHtml() async {
    String content = '<!doctype html>';
    content += '<html>';
    content += '<head></head>';
    content += '<body>';
    content += widget.selectedDate; // doesn't work correctly
    content += '</body>';
    content += '</html>';
    widget._controller.loadUrl(
        Uri.dataFromString(
            content,
            mimeType: 'text/html',
            encoding: Encoding.getByName('utf-8')
        ).toString()
    );
  }
}

The reason is you are using class parameter not the value that u created as final.原因是您使用的是 class 参数,而不是您创建的最终值。

Change this改变这个

content += widget.selectedDate; // doesn't work correctly

to

content += selectedDate // this is your variable and when you use set state u changing this value not the one in the widget tree

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM