Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

import 'dart:convert'; import 'package:flutter/material.dart'; import…

  1. User

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

    import 'package:http/http.dart' as http; import 'package:login_ui/Feature/Login%20Screen/Login_Screen.dart'; import 'package:login_ui/Feature/home/Response.dart';

    class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); }

    class _HomePageState extends State { final List imagePaths = [ 'assets/1.jpeg', 'assets/2.jpeg', 'assets/3.jpeg', 'assets/4.jpeg', 'assets/5.jpeg', 'assets/6.jpeg', 'assets/7.jpeg', 'assets/8.jpeg' ];

    List filteredImagePaths = []; bool isSearching = false;

    Map<int, String> imageData = { 0: 'HDFCBANK.NS', 1: 'ICIC', 2: 'data for image 3', 3: 'data for image 4', 4: 'data for image 5', 5: 'data for image 6', 6: 'data for image 7', 7: 'data for image 8', };

    TextEditingController _searchController = TextEditingController();

    @override void initState() { super.initState(); filteredImagePaths = imagePaths; } Future _sendData(BuildContext context, String symbol) async { try { var url = Uri.parse('https://36d3-203-109-66-92.in.ngrok.io/hello'); var response = await http.post( url, headers: <String, String>{ 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode(<String, String>{'symbol': symbol}), );

      print('Data sent: $symbol');
      print('Response status code: ${response.statusCode}');
      print('Response body: ${response.body}');
    
      Map<String, dynamic> graphData = jsonDecode(response.body);
      String graphUrl = graphData['url'];
      await Navigator.of(context).push(
        MaterialPageRoute(
          builder: (_) => Scaffold(
            appBar: AppBar(
              title: Text(symbol),
            ),
            body: WebView(
              initialUrl: graphUrl,
              javascriptMode: JavascriptMode.unrestricted,
            ),
          ),
        ),
      );
    } catch (e) {
      print('Error: $e');
    }
    

    }

    WebView? _webView;

    @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: !isSearching ? Text('Stocks') : TextField( controller: _searchController, decoration: InputDecoration( hintText: 'Search', icon: Icon(Icons.search), ), onSubmitted: (value) { _sendData(context, value); }, ), actions: [ IconButton( icon: Icon(Icons.search), onPressed: () { setState(() { isSearching = !isSearching; filteredImagePaths = imagePaths; }); }, ), ], ), body: Stack( children: [ Container( decoration: BoxDecoration( image: DecorationImage( image: AssetImage('assets/theme.jpg'), fit: BoxFit.cover, ), ), child: GridView.count( crossAxisCount: 2, padding: EdgeInsets.all(8), mainAxisSpacing: 8, crossAxisSpacing: 8, children: List.generate(filteredImagePaths.length, (index) { return GestureDetector( onTap: () async { String symbol = imageData[index]!; await _sendData(context, symbol); },

                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(8),
                    child: Stack(
                      fit: StackFit.expand,
                      children: [
                        Image.asset(
                          filteredImagePaths[index],
                          fit: BoxFit.cover,
                        ),
                        Container(
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              begin: Alignment.bottomCenter,
                              end: Alignment.topCenter,
                              colors: [
                                Colors.black.withOpacity(0.8),
                                Colors.transparent
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              }),
            ),
          ),
          if (_webView != null)
            Positioned.fill(
              child: Column(
                children: [
                  Expanded(
                    child: _webView!,
                  ),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        _webView = null;
                      });
                    },
                    child: Text('Close Graph'),
                  ),
                ],
              ),
            ),
        ],
      ),
    );
    

    } } cant see graph solve it

  2. Assistant

    To see the graph, you need to add a _webView variable in the HomePage class that stores the WebView widget. When the _sendData function is called, it sets the _webView variable with a new WebView widget to display the graph in a new screen.

    In the current implementation, you are not assigning the returned WebView widget to the _webView variable. To fix this, update the _sendData function as follows:

    Future<void> _sendData(BuildContext context, String symbol) async {
      try {
        var url = Uri.parse('https://36d3-203-109-66-92.in.ngrok.io/hello');
        var response = await http.post(
          url,
          headers: <String, String>{
            'Content-Type': 'application/json; charset=UTF-8',
          },
          body: jsonEncode(<String, String>{'symbol': symbol}),
        );
    
        print('Data sent: $symbol');
        print('Response status code: ${response.statusCode}');
        print('Response body: ${response.body}');
    
        Map<String, dynamic> graphData = jsonDecode(response.body);
        String graphUrl = graphData['url'];
    
        setState(() {
          _webView = WebView(
            initialUrl: graphUrl,
            javascriptMode: JavascriptMode.unrestricted,
          );
        });
      } catch (e) {
        print('Error: $e');
      }
    }
    

    This code uses the setState method to update the _webView variable with a new WebView widget when the function is called.

    Then, in the build method, you need to add a condition to check if the _webView variable is not null and display the WebView widget along with a close button. Update the build method as follows:

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: !isSearching
              ? Text('Stocks')
              : TextField(
                  controller: _searchController,
                  decoration: InputDecoration(
                    hintText: 'Search',
                    icon: Icon(Icons.search),
                  ),
                  onSubmitted: (value) {
                    _sendData(context, value);
                  },
                ),
          actions: [
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                setState(() {
                  isSearching = !isSearching;
                  filteredImagePaths = imagePaths;
                });
              },
            ),
          ],
        ),
        body: Stack(
          children: [
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/theme.jpg'),
                  fit: BoxFit.cover,
                ),
              ),
              child: GridView.count(
                crossAxisCount: 2,
                padding: EdgeInsets.all(8),
                mainAxisSpacing: 8,
                crossAxisSpacing: 8,
                children: List.generate(filteredImagePaths.length, (index) {
                  return GestureDetector(
                    onTap: () async {
                      String symbol = imageData[index]!;
                      await _sendData(context, symbol);
                    },
    
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(8),
                      child: Stack(
                        fit: StackFit.expand,
                        children: [
                          Image.asset(
                            filteredImagePaths[index],
                            fit: BoxFit.cover,
                          ),
                          Container(
                            decoration: BoxDecoration(
                              gradient: LinearGradient(
                                begin: Alignment.bottomCenter,
                                end: Alignment.topCenter,
                                colors: [
                                  Colors.black.withOpacity(0.8),
                                  Colors.transparent
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                }),
              ),
            ),
            if (_webView != null)
              Positioned.fill(
                child: Column(
                  children: [
                    Expanded(
                      child: _webView!,
                    ),
                    ElevatedButton(
                      onPressed: () {
                        setState(() {
                          _webView = null;
                        });
                      },
                      child: Text('Close Graph'),
                    ),
                  ],
                ),
              ),
          ],
        ),
      );
    }
    

    This code uses a Positioned.fill widget to display the WebView widget that fills the entire screen. It also includes a close button that resets the _webView variable to null when pressed.