颤振在颤振中传递两个 API

时间:2021-04-19 16:25:31

标签: flutter dart

enter image description here

各位开发者好

如你所见,我试图在图片中实现类似的结果

我假设他们一起使用了引用和未溅出的 API 来归档它

我有两个来自不同 GitHub 存储库的代码

请注意!!!

我是初学者,尝试边做边学,并在堆栈溢出中提出问题以获得直接反馈
如果你觉得我的问题很愚蠢,请耐心等待,让我走上正轨

****unsplash api app**** 

     import 'dart:convert';
    
    import 'package:flutter/material.dart';
    import 'dart:convert' as convert;
    import 'dart:async';
    import 'package:http/http.dart' as http;
    
    import 'data.dart';
    
    class Home extends StatefulWidget {
      @override
      _HomeState createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> {
      String _keyword = '';
      Timer _debounce;
      List<Results> _data = [];
    
      onChangeText(text) {
        _keyword = text;
        if (_debounce?.isActive ?? false) _debounce.cancel();
        _debounce = Timer(const Duration(milliseconds: 800), () async {
          var data = await fetchData(_keyword);
          setState(() {
            _data = data.results;
          });
        });
      }
    
      Future<Data> fetchData(keyword) async {
        var url =
            'https://unsplash.com/napi/search/photos?query=$keyword&xp=&per_page=50&page=1';
        var responseApi = await http.get(url);
        if (responseApi.statusCode == 200) {
          var resJSON = json.decode(responseApi.body);
          var data = Data.fromJson(resJSON);
          return data;
        } else {
          print('Error');
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(
              child: Column(
            children: <Widget>[
              Visibility(
                visible: _data.length > 0,
                child: Expanded(
                  child: Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: ListView.builder(
                        itemCount: _data.length,
                        itemBuilder: (BuildContext ctx, int index) {
                          return Padding(
                            padding: const EdgeInsets.only(bottom: 10),
                            child: ClipRRect(
                                borderRadius: BorderRadius.circular(10),
                                child: Image.network(_data[index].urls.small)),
                          );
                        }),
                  ),
                ),
              )
            ],
          )),
        );
      }
    }





   
    ******Quote app Api****** 
    
    
    
    import 'dart:convert';
    
    import 'package:flutter/material.dart';
    import 'package:flutterquote/widgets/quote_widget.dart';
    import 'package:http/http.dart' as http;
    import 'package:random_color/random_color.dart';
    
    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      var apiURL = "https://type.fit/api/quotes";
    
      Future<List<dynamic>> getPost() async {
        final response = await http.get('$apiURL');
        return postFromJson(response.body);
      }
    
      List<dynamic> postFromJson(String str) {
        List<dynamic> jsonData = json.decode(str);
        jsonData.shuffle();
        return jsonData;
      }
    
    
      
    
      RandomColor _randomColor = RandomColor();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: FutureBuilder<List<dynamic>>(
              future: getPost(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  if (snapshot.hasError) {
                    return ErrorWidget(snapshot.error);
                  }
                  return PageView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        var model = snapshot.data[index];
                        return QuoteWidget(
                          quote: model["text"].toString(),
                          author: model["author"].toString(),
                          bgColor: _randomColor.randomColor(
                            colorHue: ColorHue.multiple(
                              colorHues: [ColorHue.red, ColorHue.blue],
                            ),
                          ),
                        );
                      });
                } else
                  return Center(child: CircularProgressIndicator());
              }),
        );
      }

}

0 个答案:

没有答案