android – Change API JSON call Link whenever i click the next tab for flutter river pods

[ad_1]

Iam using flutter river pods for this project. I want to change the API call when press on the second and third tab. I.e make the JSON API link dynamic. How can i do that ?

Each widget is in it’s different file

Here is my code

main.dart

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: true,
      home: HomeView(),
    );
  }
}

Home_views.dart

class HomeView extends ConsumerWidget {
  const HomeView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context,  WidgetRef ref) {
    TabController controller;
    final ModelView = ref.watch(getFutureData);
    return Consumer(
        builder: (context, WidgetRef ref, _) {
          return TabBarWidget;
        }
    );
  }
}

Data_model.dart

class DataModel {
  const DataModel({
     required this.id,
    required this.joke,
    required this.categories,
  });

  final int id;
  final String joke;
  final String categories;
}

data_controllers.dart

final getFutureData = ChangeNotifierProvider<GetApiDATA>((ref) => GetApiDATA());
class GetApiDATA extends ChangeNotifier{

  List<DataModel> ListOfDataModels = [];
  GetApiDATA(){
    getDataApi();
  }


  Future getDataApi() async {
    ListOfDataModels = [];
    try{
      http.Response ApiResponse = await http.get(Uri.parse("https://v2.jokeapi.dev/joke/Programming?type=single&contains=joke&amount=10"));

      if (ApiResponse.statusCode == 200) {
        final body = jsonDecode(ApiResponse.body) as Map<String, dynamic>;
        if (body['error'] == false) {
          final jokes = body['jokes'];
          for (final joke in jokes) {
            final jokeMap = joke as Map<String, dynamic>;
            print("jokeText is ${jokeMap['joke']}");

            final id = jokeMap['id'] as int;
            final jokeText = jokeMap['joke'] as String;
            final categories = jokeMap['category'] as String;
            ListOfDataModels.add(DataModel(id: id, joke: jokeText, categories: categories));

          }
          print("Length is ${ListOfDataModels.length}");
          notifyListeners();
        }
      }
    }
    catch(e)
    {
      print(e.toString());
    }


  }


}

TabBarWidget.dart

import 'package:assignment_moe/Controllers/data_controllers.dart';
import 'package:assignment_moe/Widgets/TabViewWidget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

Widget TabBarWidget =  Consumer(
    builder: (context, WidgetRef ref, _) {
      final ModelView = ref.watch(getFutureData);
      return MaterialApp(
        home: DefaultTabController(
          length: 3,
          child: Scaffold(
            backgroundColor: Color(0XffF7F7F7),
            appBar: AppBar(
              backgroundColor: Color(0xff1B9C8F),
              title: Text('Jokes API'),
            ),

            body:  Column(
              children: [
                Container(
                  margin: const EdgeInsets.only(top: 30, left: 10,right: 10),
                  height: 35,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(15),
                    color: Colors.white,
                  ),
                  child: TabBar(
                    indicator:  BoxDecoration(
                      color: Color(0xff1B9C8F),
                      borderRadius: BorderRadius.all(Radius.circular(10)),
                    ),
                    labelColor:  Colors.white,
                    unselectedLabelColor:  Color(0xff1B9C8F),
                    tabs:  [
                      Tab(
                        text: 'Programming',
                      ),
                      Tab(
                        text: 'Dark',
                      ),
                      Tab(
                        text: 'Spooky',
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: TabBarView(
                    children: [
                      TabViewWidget,
                      TabViewWidget,
                      TabViewWidget,
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      );


    }
);

TabViewWidget.dart

import 'package:assignment_moe/Controllers/data_controllers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

Widget TabViewWidget = Consumer (
builder: (context, WidgetRef ref, _) {
  final ModelView = ref.watch(getFutureData);
  return  ModelView.ListOfDataModels.isEmpty ?
  Center(child: CircularProgressIndicator(),) :
  ListView.builder(
    itemCount: ModelView.ListOfDataModels.length,
    itemBuilder: (context, index) =>
        Container(
          margin: EdgeInsets.only(top: 20,left: 20,right: 20),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(15),
            color: Colors.white,
          ),
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: ListTile(
              title: Text(ModelView.ListOfDataModels[index].categories
              ),
              subtitle: Padding(
                padding: const EdgeInsets.only(top: 10),
                child: Text(ModelView.ListOfDataModels[index].joke),
              ),
              leading: Container(
                alignment: Alignment.center,
                width: 35,
                height: 35,
                decoration: BoxDecoration(
                    color: Color(0xff1B9C8F),
                    borderRadius: BorderRadius.circular(20)),
                child: Text(
                  ModelView.ListOfDataModels[index].id.toString(),
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),

        ),
  );

}
);

Below is the screen shotenter image description here

ANy help would be appreciated

[ad_2]

Source link

Leave a Reply

Your email address will not be published.