Я хочу просматривать файл PDF, когда пользователь нажимает на обложку - PullRequest
0 голосов
/ 04 августа 2020

Я хочу просматривать файл PDF, когда пользователь нажимает на обложку. Я новичок во Flutter.

Ребята, вы можете узнать, что не так в моем коде? Когда я нажимаю на книгу, она ничего не делает.

Я думаю, проблема в функции PDF Viewer.

Я использую advance_pdf_viewer 1.1.6 .

class Books extends StatefulWidget {
  @override
  _BooksState createState() => _BooksState();
}

class _BooksState extends State<Books> {
  bool _isLoading = true;
  PDFDocument document;
  var url;

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

  loadDocument() async {
    document = await PDFDocument.fromURL(url);

    setState(() => _isLoading = false);
  }

  changePDF(value) async {
    setState(() => _isLoading = true);
    if (value == 1) {
      document = await PDFDocument.fromURL(url);
    } else {
      print('nothing');
    }

    setState(() => _isLoading = false);
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: Firestore.instance.collection('books').snapshots(),
        builder: (
          context,
          snapshot,
        ) {
          if (snapshot.data == null)
            return Center(
              child: CircularProgressIndicator(
                backgroundColor: Colors.red,
                valueColor: new AlwaysStoppedAnimation<Color>(Colors.teal),
              ),
            );
          return GridView.builder(
            shrinkWrap: true,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3, childAspectRatio: 0.7),
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) => Padding(
              padding: const EdgeInsets.all(8.0),
              child: GridTile(
                child: InkWell(
                  onTap: () async {
                    PDFDocument.fromURL(snapshot.data.documents[index]['url']);

                    _isLoading
                        ? Center(child: CircularProgressIndicator())
                        : PDFViewer(document: document);
                  },
                  child: Container(
                    height: 200,
                    width: 110,
                    decoration: BoxDecoration(
                        boxShadow: [
                          BoxShadow(
                            color: Colors.red[500].withOpacity(0.6),
                            spreadRadius: 0.5,
                            blurRadius: 1,
                            offset: Offset(2, 0),
                          ),
                        ],
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(3),
                        border: Border.all(
                            style: BorderStyle.solid,
                            color: Colors.red[500],
                            width: 0.3)),
                    child: Column(children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(5.0),
                        child: Container(
                          child: Image.network(
                            snapshot.data.documents[index]['image'],
                            width: 100,
                          ),
                        ),
                      ),
                      SizedBox(height: 5),
                      Text(
                        snapshot.data.documents[index]['name'],
                      )
                    ]),
                  ),
                ),
              ),
            ),
          );
        });
  }
}

1 Ответ

1 голос
/ 04 августа 2020

PDFViewer возвращает виджет. Если вы хотите просмотреть файл pdf при нажатии на InkWell, вам необходимо создать виджет, который отображает виджет, возвращаемый PDFViewer, например,

class PDFScreen extends StatelessWidget {
  PDFDocument document;
  PDFScreen({@required this.document});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      child: PDFViewer(document: document)
    );
  }
}

И изменить onTap() InkWell на:

onTap: () async {
    PDFDocument.fromURL(snapshot.data.documents[index]['url']);

    _isLoading
    ? Center(child: CircularProgressIndicator())
        : Navigator.push(context, MaterialPageRoute(builder: (context) => PDFScreen(document: document)));
  },

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...