WebView показывает пустую страницу (думаю, не загружается) - PullRequest
0 голосов
/ 03 июля 2019

Итак, я изучаю флаттер, работая над проектом. Я хочу сделать простое приложение со списком новостей с динамическим списком и веб-просмотром, чтобы открыть полную статью. Тем не менее, WebViewScaffold показывает пустую страницу.

Попробовал Инспектор флаттера, чтобы попытаться найти ошибку, но не смог ее получить. Пробовал менять классы и состояния.

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.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...