Skip to content

Instantly share code, notes, and snippets.

@Lxxyx
Created February 11, 2025 21:09
Show Gist options
  • Save Lxxyx/0387133689048b3bb4710cae4a210786 to your computer and use it in GitHub Desktop.
Save Lxxyx/0387133689048b3bb4710cae4a210786 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Trading Bot',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorSchemeSeed: Colors.blue,
),
home: const MyHomePage(title: 'Trading Bot'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
super.key,
required this.title,
});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _totalInvestment = 0;
int _totalProfit = 0;
final List<Stock> _stocks = [
Stock(symbol: 'AAPL', quantity: 0),
Stock(symbol: 'GOOG', quantity: 0),
Stock(symbol: 'MSFT', quantity: 0),
Stock(symbol: 'AMZN', quantity: 0),
];
void _buyStock(Stock stock) {
setState(() {
stock.quantity++;
_totalInvestment += 100; // assuming the stock price is $100
});
}
void _sellStock(Stock stock) {
setState(() {
stock.quantity--;
_totalProfit += 100; // assuming the stock price is $100
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _stocks.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_stocks[index].symbol),
subtitle: Text('Quantity: ${_stocks[index].quantity}'),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: () => _buyStock(_stocks[index]),
child: const Text('Buy'),
),
const SizedBox(width: 10),
ElevatedButton(
onPressed: () => _sellStock(_stocks[index]),
child: const Text('Sell'),
),
],
),
);
},
),
),
Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Total Investment: '),
Text('\$$_totalInvestment'),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Total Profit: '),
Text('\$$_totalProfit'),
],
),
],
),
),
],
),
);
}
}
class Stock {
final String symbol;
int quantity;
Stock({
required this.symbol,
this.quantity = 0,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment