WebView显示空白页(我认为未加载)

时间:2019-07-03 00:28:28

标签: flutter dart android-webview

所以我在做项目时正在探索颤动。我想制作一个带有动态列表和Webview的简单新闻列表应用程序,以打开全文。但是,WebViewScaffold显示空白页。

尝试了Flutter Inspector尝试查找错误,但无法获取。尝试更改类和状态。

import 'package:flutter/material.dart';
import 'makeCard.dart';
import 'webService.dart';
import 'constants.dart';
import 'NewsArticle.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'newsDetail.dart';


void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(primaryColor: Color.fromRGBO(58, 66, 86, 1.0)),
      home: NewsList(),
    );
  }
}

class NewsListState extends State<NewsList> {
  List<NewsArticle> _newsArticles = List<NewsArticle>();

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

  void _populateNewsArticles() {
    Webservice().load(NewsArticle.all).then((newsArticles) => {
          setState(() => {_newsArticles = newsArticles})
        });
  }

  ListTile _buildItemsForListView(BuildContext context, int index) {
    return ListTile(
      title: _newsArticles[index].urlToImage == null
          ? Image.asset(Constants.NEWS_PLACEHOLDER_IMAGE_ASSET_URL)
          : Image.network(_newsArticles[index].urlToImage),
      subtitle:
          Text(_newsArticles[index].title, style: TextStyle(fontSize: 18)),
      contentPadding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('News'),
        ),
        body: new Container(
          margin: const EdgeInsets.only(left: 2.0, right: 2.0),
          child: new Card(
            child: new ListView.separated(
              separatorBuilder: (context, index) => Divider(
                    height: 10.0,
                  ),
              itemCount: _newsArticles.length,
              itemBuilder: _buildItemsForListView,
            ),
          ),
        ));
  }
}

class NewsList extends StatefulWidget {
  @override
  createState() => NewsListState();
}
class Constants {

  static final String HEADLINE_NEWS_URL = 'https://newsapi.org/v2/top-headlines?country=us&apiKey=3f85510c67ef4757b65bc2cd96a9eaf9';
  static final String NEWS_PLACEHOLDER_IMAGE_ASSET_URL = 'assets/placeholder.png';

}
class Resource<T> {
  final String url;

  T Function(Response response) parse;

  Resource({this.url, this.parse});
}

class Webservice {
  Future<T> load<T>(Resource<T> resource) async {
    final response = await http.get(resource.url);
    if (response.statusCode == 200) {
      return resource.parse(response);
    } else {
      throw Exception('Failed to load data!');
    }
  }
}
class DetailPage extends StatefulWidget {
  DetailPage(this.url);

  final String url;

  @override
  createState() => DetailPageState(this.url);
}

  class DetailPageState extends State<DetailPage> {
  var _url;
  DetailPageState(this._url);



  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text(
          "Full Article",
        ),
      ),
      body: new Container(
        child: new Column(
          children: <Widget>[
            MaterialApp(
              routes: {
                "/": (_) => new WebviewScaffold(
                  url: _url,
                  appBar: new AppBar(title: new Text("")),
                  hidden:true,
                )
              },
            ),
          ],
        ),
      ),
      );
  }
}
class NewsArticle {

  final String url;
  final String title;
  final String descrption;
  final String urlToImage;
  final String publishedAt;
  final String content;


  NewsArticle({this.url,this.title, this.descrption, this.urlToImage,this.publishedAt,this.content
  });

  factory NewsArticle.fromJson(Map<String,dynamic> json) {
    return NewsArticle(
        title: json['title'],
        descrption: json['description'],
        urlToImage: json['urlToImage'] ?? Constants.NEWS_PLACEHOLDER_IMAGE_ASSET_URL,
        url: json['url'],
        publishedAt: json['publishedAt'],
        content: json['content']
    );
  }
  static Resource<List<NewsArticle>> get all => Resource(
      url: Constants.HEADLINE_NEWS_URL,
      parse: (response) {
        final result = json.decode(response.body);
        Iterable list = result['articles'];
        return list.map((model) => NewsArticle.fromJson(model)).toList();
      }
  );
}
class NewsListState extends State<NewsList> {
  List<NewsArticle> _newsArticles = List<NewsArticle>();

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

  void _populateNewsArticles() {
    Webservice().load(NewsArticle.all).then((newsArticles) => {
          setState(() => {_newsArticles = newsArticles})
        });
  }

  ListTile _buildItemsForListView(BuildContext context, int index) {
    return ListTile(
      title: _newsArticles[index].urlToImage == null
          ? Image.asset(Constants.NEWS_PLACEHOLDER_IMAGE_ASSET_URL)
          : Image.network(_newsArticles[index].urlToImage),
      subtitle:
          Text(_newsArticles[index].title, style: TextStyle(fontSize: 18)),
      contentPadding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
      onTap: () {
        var url = _newsArticles[index].url;
        Navigator.push(context,
            new MaterialPageRoute(
              builder: (context) => new DetailPage(url),
        ));
      },
    );
  }

  Card makeCard(BuildContext context, int index) {
    return Card(
      elevation: 8.0,
      margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
      child: Container(
          decoration: BoxDecoration(color: Color.fromRGBO(64, 75, 96, .9)),
          child: _buildItemsForListView(context, index)
      ),
    );
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
      appBar: AppBar(
        elevation: 0.1,
        backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
        title: Text('News'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.list),
            onPressed: () {},
          )
        ],
      ),
      body: Container(
        child: ListView.builder(
          itemCount: _newsArticles.length,
          itemBuilder: makeCard,
        ),
      ),
      bottomNavigationBar: Container(
        height: 55.0,
        child: BottomAppBar(
          color: Color.fromRGBO(58, 66, 86, 1.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.home, color: Colors.white),
                onPressed: () {},
              ),
              IconButton(
                icon: Icon(Icons.blur_on, color: Colors.white),
                onPressed: () {},
              ),
              IconButton(
                icon: Icon(Icons.hotel, color: Colors.white),
                onPressed: () {},
              ),
              IconButton(
                icon: Icon(Icons.account_box, color: Colors.white),
                onPressed: () {},
              )
            ],
          ),
        ),
      ),
    );
  }
}

class NewsList extends StatefulWidget {
  @override
  createState() => NewsListState();
}


The console log for the error -

D/ViewRootImpl@e393259[MainActivity]( 3890): ViewPostIme pointer 0
D/ViewRootImpl@e393259[MainActivity]( 3890): ViewPostIme pointer 1
I/flutter ( 3890): Another exception was thrown: BoxConstraints forces an infinite height.
I/flutter ( 3890): Another exception was thrown: RenderBox was not laid out: RenderStack#59935 relayoutBoundary=up8 NEEDS-PAINT
I/flutter ( 3890): Another exception was thrown: RenderBox was not laid out: _RenderTheatre#f69a8 relayoutBoundary=up7 NEEDS-PAINT
I/flutter ( 3890): Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#677d7 relayoutBoundary=up6 NEEDS-PAINT
I/flutter ( 3890): Another exception was thrown: RenderBox was not laid out: RenderAbsorbPointer#b985b relayoutBoundary=up5 NEEDS-PAINT
I/flutter ( 3890): Another exception was thrown: RenderBox was not laid out: RenderPointerListener#86c3b relayoutBoundary=up4 NEEDS-PAINT
I/flutter ( 3890): Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#b0c68 relayoutBoundary=up3 NEEDS-PAINT
I/flutter ( 3890): Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#c9989 relayoutBoundary=up2 NEEDS-PAINT
I/flutter ( 3890): Another exception was thrown: RenderBox was not laid out: RenderFlex#050d5 relayoutBoundary=up1 NEEDS-PAINT
I/flutter ( 3890): Another exception was thrown: NoSuchMethodError: The method '<=' was called on null.

2 个答案:

答案 0 :(得分:0)

将此行添加到AndroidManifest.xml,然后重新启动应用程序。

步骤1: 查找{"Offset":0,"Total":0,"Results":[{"Id":1,"RefId":1,"SecondRefId":1,"ThirdRefId":111,"Name":"test1","JSON":"TEST1","JsonType":"test","Desc":"test1","AuditId":0,"AuditStamp":"\/Date(1577190306230-0000)\/"}],"Meta":{}} 文件。

文件路径: AndroidManifest.xml

第2步:Android >> app >> main >> AndroidManifest.xml标记

之前添加此行
application

第3步: 重新启动您的应用程序。

希望这个答案对您有所帮助:)

答案 1 :(得分:0)

另一个可能的原因是将一个小部件传递给 WebviewScaffold.bottomNavigationBar。我不知道为什么会发生这种情况,但是当我删除传递给它的小部件并将小部件传递给 WebviewScaffold.persistentFooterButtons 时,页面被呈现,我的页脚小部件被呈现。似乎传递给 bottomNavigationBar 的小部件呈现在 WebView 之上。