Ventacademy
  • Socials
  • Business Fundamentals
    • Investments
  • High Income Skills
  • Programming
  • Software Review
  • Website Development
    • WordPress
    • Search Engine Optimization
    • Free Templates and Plugins
      • Support
  • Finance
No Result
View All Result
Knowledge base
No Result
View All Result
Home Programming Free download

Build Android & IOS App For WordPress Blog with Flutter

admin by admin
March 2, 2021
Share on FacebookShare on Twitter


So i wanted to make a app for our website flutternerd.com well turns out it was easy than i thought

So let’s get started

Step 1: Download & Activate Plugin For wordpress blog

Link: https://wordpress.org/plugins/easily-generate-rest-api-url/

Step 2: In the wordpress dashboard

go to settings (hover over it) > (Click on) Generate Rest API Url, scroll to the bottom you will have a api url you can modify it if you want.

Step 3: Create a Flutter Project

If not already install and setup flutter from https://flutter.dev/docs/get-started/install

i used vs code but you can use android studio as well will work just the same

Step 4: Create functions to fetch posts and post image url

Create a file i named it wp-api.dart then add http package add this function

Future<List> fetchWpPosts() async {
  final response = await http.get(
      "https://flutternerd.com/index.php/wp-json/wp/v2/posts",
      headers: {"Accept": "application/json"});
  var convertDatatoJson = jsonDecode(response.body);
  return convertDatatoJson;
}

Step 5: Create frontend and use this function by importing the file

import 'package:flutter/material.dart';
import 'package:nativewordpress/views/PostPage.dart';
import 'package:nativewordpress/wp-api.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String removeAllHtmlTags(String htmlText) {
    RegExp exp = RegExp(r"<[^>]*>", multiLine: true, caseSensitive: true);

    return htmlText.replaceAll(exp, '');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FlutterNerd"),
      ),
      body: Container(
        padding: EdgeInsets.only(top: 24),
        child: FutureBuilder(
          future: fetchWpPosts(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, int index) {
                    Map wppost = snapshot.data[index];
                    return PostTile(
                        imageApiUrl: wppost['_links']["wp:featuredmedia"][0]
                            ["href"],
                        excerpt: removeAllHtmlTags(wppost['excerpt']['rendered']
                            .replaceAll("&#8217;", "")),
                        desc: wppost['content']['rendered'],
                        title: wppost['title']['rendered']
                            .replaceAll("#038;", ""));
                  });
            }

            return Center(child: CircularProgressIndicator());
          },
        ),
      ),
    );
  }
}

class PostTile extends StatefulWidget {
  final String imageApiUrl, title, desc, excerpt;
  PostTile({this.imageApiUrl, this.title, this.desc, this.excerpt});

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

class _PostTileState extends State<PostTile> {
  String imageUrl = "";

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (imageUrl != "") {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => PostPage(
                        title: widget.title,
                        imageUrl: imageUrl,
                        desc: widget.desc,
                      )));
        }
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 4),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            FutureBuilder(
                future: fetchWpPostImageUrl(widget.imageApiUrl),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    imageUrl = snapshot.data["guid"]["rendered"];
                    return Image.network(imageUrl);
                  }
                  return Center(child: CircularProgressIndicator());
                }),
            SizedBox(height: 8),
            Text(
              widget.title,
              style: TextStyle(fontSize: 16),
            ),
            SizedBox(height: 5),
            Text(widget.excerpt)
          ],
        ),
      ),
    );
  }
}

Step 6: We will need to create a separate function to get image url of each post

fetchWpPostImageUrl(url) async {
  final response = await http.get(url, headers: {"Accept": "application/json"});
  var convertDatatoJson = jsonDecode(response.body);
  return convertDatatoJson;
}

Step 7: Now let’s create post page

We need to add flutter_html_view package then

import 'package:flutter/material.dart';
import 'package:flutter_html_view/flutter_html_view.dart';

class PostPage extends StatefulWidget {
  final String imageUrl, title, desc;
  PostPage({this.title, this.desc, this.imageUrl});

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

class _PostPageState extends State<PostPage> {
  Widget postContent(htmlContent) {
    return HtmlView(
      data: htmlContent, // optional, type String
      onLaunchFail: (url) {
        // optional, type Function
        print("launch $url failed");
      },
      scrollable: false,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              widget.imageUrl != null
                  ? Image.network(widget.imageUrl)
                  : Container(),
              SizedBox(height: 8),
              Text(
                widget.title,
                style: TextStyle(fontSize: 18),
              ),
              SizedBox(height: 6),
              postContent(
                widget.desc,
              )
            ],
          ),
        ),
      ),
    );
  }
}

And that is how we can build android and ios app for your wordpress blog if face any problem feel free to comment it down below

admin

admin

Related Posts

An open-source public transport app

April 20, 2021

An open-source pubic transport app

April 20, 2021

Flutter Firebase Chat Core – Download source code on GitHub

April 19, 2021

Food Delivery App made by Flutter and Bloc

April 19, 2021
Next Post

TCN hits power transmission of 5,615.40MW

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

Magic in the Mundane: Your Life, Documented Beautifully with Laura Beth Davidson

Magic in the Mundane: Your Life, Documented Beautifully with Laura Beth Davidson

December 1, 2020

8 New Ways You Can Still Have An Epic Workout At Home

October 28, 2020

Subscribe.

Trending.

No Content Available

About Us

Knowledge base

We bring you the best technology news, Finance news and lots more. you can download premium themes, plugins scripts, courses and lots more here.

Follow Us

Categories

  • All
  • Books
  • Business Fundamentals
  • Finance
  • Free Course
  • Free download
  • Free Templates and Plugins
  • Health
  • High Income Skills
  • Investments
  • NSFW
  • Photography
  • Search Engine Optimization
  • Software Review
  • Support
  • Technical Analysis
  • Website Development
  • Wordpress
  • World

Activity

FREEDOWNLOAD

Oxygen 5.9 – WooCommerce WordPress Theme

Preston 1.2.1 – Real Estate WordPress Theme

Support Board 3.2.4 – WordPress Chat Plugin

YITH WooCommerce Customize My Account Page 3.1.1 Nulled

YITH Cost of Goods for WooCommerce 1.2.13 Nulled

BoomBox 2.7.7 Nulled – Viral Magazine WordPress Theme

Calendarista 13.3 – Premium WordPress Booking Plugin

Starto 1.7.2 Nulled – Saas Software Startup WordPress

BioLinks 7.0.0 Nulled – Instagram & TikTok Bio Links & URL Shortener

WizeStore 1.14.5 Nulled – Multipurpose WooCommerce Shop

Midone 3.0.4 – Laravel 8 Admin Dashboard Template + HTML Version + XD Design File

Mosto 4.0.1 – App Landing Page

An open-source public transport app

Infix LMS 2.0.1 Nulled – Learning Management System

An open-source pubic transport app

  • Socials
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2020 Ventacademy

No Result
View All Result
  • Socials
  • Business Fundamentals
    • Investments
  • High Income Skills
  • Programming
  • Software Review
  • Website Development
    • WordPress
    • Search Engine Optimization
    • Free Templates and Plugins
      • Support

© 2020 Ventacademy