Some of pit flutter Starter

Some of pit flutter Starter

I did not write a blog for a long time, most recently in preparation for the game of things, the way to taking the time to learn a little flutter, it took nearly two weeks to complete a view blog post is a small APPdemo, just cut the two maps, as follows:

Home Park blog page, only these things must log

The whole project is quite simple ( Project Site ) nothing complicated logic and interface (after all, is just learning, can not afford to go and get complex logic and interface), at present, so be it. Next, I will record it as well as some of the difficulties encountered in the development of solutions.

flutter blog Park

Certified Solution

Here is the URL blog api Park ( https://api.cnblogs.com/ ), said here about local garden blog document comparing the pit, garden blog login is based on OAuth 2.0 authentication, certification of the students do not understand this check know, the simple point is that the user returns a code to your blog after logging garden, then you get through this code token , token you can get through the user's information.

In the blog garden, token, there are two ways to obtain, first you forget it, it is impossible to obtain the data, 2.1 and 2.2 is the way to get the data correct, emm, so why 1 appears in the park's official blog document it?

 


 

 

OK, then we discuss this second acquisition mode, the second acquisition mode when the user enter the account password, click Login, Park will jump to a blog interface, this interface, which displays the code. Then we should be in the middle flutter how to do it?

The idea is that, first we open the login screen blog Park, then listens for the current URL is changed, if the change occurred, has been representing the jump, and then we get the code by executing the jump page js code on the line.

 


 

 

By executing code_value JS code you can get the code

 


 

 

In the initial page init, we can monitor whether the jump page.

// 监听页面跳转
flutterWebViewPlugin.onUrlChanged.listen((url) {
    // 该页面会接收code,然后根据code换取AccessToken,并将获取到的token及其他信息,通过js的get()方法返回
    if (url != null && url.length > 0 && url.contains("code=")) {
        // 在parseReslut中,我们获得code,然后再去获取token
        parseResult();
    }
});

Then we can get the code by executing js code

  /// 通过code获得token
  parseResult() {
      // 执行code_value js代码获得code
    flutterWebViewPlugin.evalJavascript("code_value").then((code) {
      if (code != null && code.length != 0) {
          // 下面是获取token,不需要管
        _cnblogNetService.getToken(code, storageToken);
      }
    });
  }

Logged-on user access to information

Incidentally blog here to obtain basic information about the user's Park api's it, you can Tucao about, official documentation api blog Park is the second pit api documentation I have seen, the first is Baidu statistics api documentation. (However, the official blog of the park is still pretty awesome, Bo asked in the early morning above questions are answered right away)

Here, we can not do without BEARER .

 


 

 

Blog Home Park acquired data

In the garden of API blog, which is written like this, but in fact, so you can not get home data. Because you are missing a header, header data and the same as above (Do not forget Bearer ), while pageIndex not from zero, but from the beginning of.

 


 

 

flutter load built-in HTML

In the project, the need to produce a AboutMe page, but do not want to write a new page, Markdown will turn directly into html, then loaded on the line.

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';

/// aboutme的页面
class AboutMePage extends StatefulWidget {
  @override
  AboutMePageState createState() {
    return AboutMePageState();
  }
}

class AboutMePageState extends State<AboutMePage> {
  WebViewController _controller;
    
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('关于')),
      body: WebView(
        initialUrl: '',
        onWebViewCreated: (WebViewController webViewController) {
          _controller = webViewController;
            // 加载本地html
          _loadHtmlFromAssets();
        },
      ),
    );
  }
    
  // 从本地加载html文件,需要使用异步操作
  _loadHtmlFromAssets() async {
    String fileText = await rootBundle.loadString('image/README.html');
    _controller.loadUrl(Uri.dataFromString(fileText,
            mimeType: 'text/html', encoding: Encoding.getByName('utf-8'))
        .toString());
  }
}

Of course, we need to set assets:

 


 

 

For now, only we are experiencing these problems, because this app is just a simple demo, yet complex pages, so basically there is no question what the controls.

The project is still a lot of bug, but how can I do, I am helpless ah.

This is the app's Download ~ ~ ~ ~

 


 

 

Guess you like

Origin www.cnblogs.com/xiaohuiduan/p/11627827.html