import 'dart:convert'; import 'package:flutter/material.dart'; import…
- 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
- Assistant
To see the graph, you need to add a
_webViewvariable in theHomePageclass that stores theWebViewwidget. When the_sendDatafunction is called, it sets the_webViewvariable with a newWebViewwidget to display the graph in a new screen.In the current implementation, you are not assigning the returned
WebViewwidget to the_webViewvariable. To fix this, update the_sendDatafunction 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
setStatemethod to update the_webViewvariable with a newWebViewwidget when the function is called.Then, in the
buildmethod, you need to add a condition to check if the_webViewvariable is notnulland display theWebViewwidget along with a close button. Update thebuildmethod 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.fillwidget to display theWebViewwidget that fills the entire screen. It also includes a close button that resets the_webViewvariable tonullwhen pressed.