Flutter: fitur Tambah Saham (tab Saham + form kode/perusahaan/sektor)
- Tab baru 'Saham' di NavigationBar (daftar master saham dari saat.saham) - FAB konteks: di tab Saham buka form Tambah Saham, di tab lain catat transaksi - EditSahamScreen: form tambah saham baru (kode ticker, perusahaan, sektor) - StockService: method addSaham(), updateSaham(), hapusSaham() ke Odoo
This commit is contained in:
@@ -5,6 +5,7 @@ import '../models/portfolio.dart';
|
||||
import '../models/saham_transaksi.dart';
|
||||
import '../services/stock_service.dart';
|
||||
import 'add_transaksi_screen.dart';
|
||||
import 'edit_saham_screen.dart';
|
||||
|
||||
class HomeScreen extends StatelessWidget {
|
||||
const HomeScreen({
|
||||
@@ -24,7 +25,8 @@ class HomeScreen extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final client = OdooClient(baseUrl: baseUrl, db: db, username: user, password: pass);
|
||||
final client = OdooClient(
|
||||
baseUrl: baseUrl, db: db, username: user, password: pass);
|
||||
final service = StockService(client);
|
||||
return _HomeShell(
|
||||
service: service,
|
||||
@@ -53,12 +55,23 @@ class _HomeShellState extends State<_HomeShell> {
|
||||
void _reload() => setState(() => _reloadTick++);
|
||||
|
||||
Future<void> _openAdd() async {
|
||||
final added = await Navigator.of(context).push<bool>(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => AddTransaksiScreen(service: widget.service),
|
||||
),
|
||||
);
|
||||
if (added == true) _reload();
|
||||
if (_tab == 1) {
|
||||
// Tab Saham -> buka tambah saham
|
||||
final added = await Navigator.of(context).push<bool>(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => EditSahamScreen(service: widget.service),
|
||||
),
|
||||
);
|
||||
if (added == true) _reload();
|
||||
} else {
|
||||
// Tab lain -> buka buat transaksi
|
||||
final added = await Navigator.of(context).push<bool>(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => AddTransaksiScreen(service: widget.service),
|
||||
),
|
||||
);
|
||||
if (added == true) _reload();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -67,20 +80,26 @@ class _HomeShellState extends State<_HomeShell> {
|
||||
appBar: AppBar(
|
||||
title: const Text('Saat Invest'),
|
||||
actions: [
|
||||
IconButton(icon: const Icon(Icons.refresh), tooltip: 'Muat ulang',
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: 'Muat ulang',
|
||||
onPressed: _reload),
|
||||
IconButton(icon: const Icon(Icons.logout), tooltip: 'Keluar', onPressed: widget.onLogout),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.logout),
|
||||
tooltip: 'Keluar',
|
||||
onPressed: widget.onLogout),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _openAdd,
|
||||
tooltip: 'Catat transaksi baru',
|
||||
child: const Icon(Icons.add),
|
||||
tooltip: _tab == 1 ? 'Tambah Saham' : 'Catat Transaksi',
|
||||
child: Icon(_tab == 1 ? Icons.add_business : Icons.add),
|
||||
),
|
||||
body: IndexedStack(
|
||||
index: _tab,
|
||||
children: [
|
||||
_PortfolioTab(key: ValueKey('pf$_reloadTick'), service: widget.service),
|
||||
_SahamTab(key: ValueKey('sh$_reloadTick'), service: widget.service),
|
||||
_TransaksiTab(key: ValueKey('tx$_reloadTick'), service: widget.service),
|
||||
],
|
||||
),
|
||||
@@ -89,6 +108,7 @@ class _HomeShellState extends State<_HomeShell> {
|
||||
onDestinationSelected: (i) => setState(() => _tab = i),
|
||||
destinations: const [
|
||||
NavigationDestination(icon: Icon(Icons.pie_chart), label: 'Portofolio'),
|
||||
NavigationDestination(icon: Icon(Icons.trending_up), label: 'Saham'),
|
||||
NavigationDestination(icon: Icon(Icons.receipt_long), label: 'Transaksi'),
|
||||
],
|
||||
),
|
||||
@@ -96,7 +116,7 @@ class _HomeShellState extends State<_HomeShell> {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- Portofolio / Equity tab ----------------
|
||||
// ---------------- Portofolio tab ----------------
|
||||
|
||||
class _PortfolioTab extends StatefulWidget {
|
||||
const _PortfolioTab({super.key, required this.service});
|
||||
@@ -107,7 +127,6 @@ class _PortfolioTab extends StatefulWidget {
|
||||
|
||||
class _PortfolioTabState extends State<_PortfolioTab> {
|
||||
late Future<List<Portfolio>> _fut;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -128,8 +147,7 @@ class _PortfolioTabState extends State<_PortfolioTab> {
|
||||
return _ErrorRetry(message: '${snap.error}', onRetry: _reload);
|
||||
}
|
||||
final list = snap.data ?? [];
|
||||
final totalNilai = list.fold<double>(
|
||||
0, (a, p) => a + p.nilaiSekarang);
|
||||
final totalNilai = list.fold<double>(0, (a, p) => a + p.nilaiSekarang);
|
||||
final totalModal = list.fold<double>(0, (a, p) => a + p.avgCost * p.sisaLembar);
|
||||
final totalPl = list.fold<double>(0, (a, p) => a + p.unrealizedPl + p.realizedPl);
|
||||
return RefreshIndicator(
|
||||
@@ -144,21 +162,24 @@ class _PortfolioTabState extends State<_PortfolioTab> {
|
||||
textAlign: TextAlign.center),
|
||||
),
|
||||
...list.map((p) => Card(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
child: ListTile(
|
||||
title: Text(p.sahamTicker, style: const TextStyle(fontWeight: FontWeight.bold)),
|
||||
subtitle: Text('${p.sisaLembar} lbr · avg ${_fmt(p.avgCost)} · hrga ${_fmt(p.hargaSekarang)}'),
|
||||
trailing: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(_fmt(p.nilaiSekarang)),
|
||||
Text(_rm(p.unrealizedPl), style: TextStyle(
|
||||
color: p.unrealizedPl >= 0 ? Colors.green : Colors.red)),
|
||||
],
|
||||
),
|
||||
),
|
||||
)),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
child: ListTile(
|
||||
title: Text(p.sahamTicker,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold)),
|
||||
subtitle: Text(
|
||||
'${p.sisaLembar} lbr · avg ${_fmt(p.avgCost)} · hrga ${_fmt(p.hargaSekarang)}'),
|
||||
trailing: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(_fmt(p.nilaiSekarang)),
|
||||
Text(_rm(p.unrealizedPl),
|
||||
style: TextStyle(
|
||||
color: p.unrealizedPl >= 0 ? Colors.green : Colors.red)),
|
||||
],
|
||||
),
|
||||
),
|
||||
)),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
),
|
||||
@@ -181,23 +202,85 @@ class _EquityCard extends StatelessWidget {
|
||||
margin: const EdgeInsets.all(12),
|
||||
color: const Color(0xFF0B6E4F),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||
Text('Total Equity', style: TextStyle(color: Colors.white70, fontSize: 14)),
|
||||
Text('Rp ${_fmt(equity, force: true)}',
|
||||
style: const TextStyle(color: Colors.white, fontSize: 28, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
Text('Modal dikeluarkan: ${_fmt(modal)}',
|
||||
style: const TextStyle(color: Colors.white70)),
|
||||
Row(children: [
|
||||
Text('P/L: ', style: const TextStyle(color: Colors.white70)),
|
||||
Text(_rm(pl),
|
||||
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.w600)),
|
||||
const SizedBox(width: 8),
|
||||
Text('(${pct.toStringAsFixed(2)}%)', style: const TextStyle(color: Colors.white70)),
|
||||
]),
|
||||
]),
|
||||
),
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||
Text('Total Equity',
|
||||
style: TextStyle(color: Colors.white70, fontSize: 14)),
|
||||
Text('Rp ${_fmt(equity, force: true)}',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 28,
|
||||
fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
Text('Modal dikeluarkan: ${_fmt(modal)}',
|
||||
style: const TextStyle(color: Colors.white70)),
|
||||
Row(children: [
|
||||
Text('P/L: ', style: const TextStyle(color: Colors.white70)),
|
||||
Text(_rm(pl),
|
||||
style: const TextStyle(
|
||||
color: Colors.white, fontWeight: FontWeight.w600)),
|
||||
const SizedBox(width: 8),
|
||||
Text('(${pct.toStringAsFixed(2)}%)',
|
||||
style: const TextStyle(color: Colors.white70)),
|
||||
]),
|
||||
])),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------- Saham tab ----------------
|
||||
|
||||
class _SahamTab extends StatefulWidget {
|
||||
const _SahamTab({super.key, required this.service});
|
||||
final StockService service;
|
||||
@override
|
||||
State<_SahamTab> createState() => _SahamTabState();
|
||||
}
|
||||
|
||||
class _SahamTabState extends State<_SahamTab> {
|
||||
late Future<List<Saham>> _fut;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_fut = widget.service.listSaham();
|
||||
}
|
||||
|
||||
void _reload() => setState(() => _fut = widget.service.listSaham());
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<List<Saham>>(
|
||||
future: _fut,
|
||||
builder: (context, snap) {
|
||||
if (snap.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snap.hasError) {
|
||||
return _ErrorRetry(message: '${snap.error}', onRetry: _reload);
|
||||
}
|
||||
final list = snap.data ?? [];
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async => _reload(),
|
||||
child: ListView.builder(
|
||||
itemCount: list.length,
|
||||
itemBuilder: (c, i) {
|
||||
final s = list[i];
|
||||
return Card(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: const Color(0xFF0B6E4F),
|
||||
child: Text(s.kode.substring(0, 1).toUpperCase(),
|
||||
style: const TextStyle(color: Colors.white)),
|
||||
),
|
||||
title: Text('${s.kode} — ${s.perusahaan ?? '-'}'),
|
||||
subtitle: Text(s.sektor ?? 'Sektor: -'),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -213,7 +296,6 @@ class _TransaksiTab extends StatefulWidget {
|
||||
|
||||
class _TransaksiTabState extends State<_TransaksiTab> {
|
||||
late Future<List<Transaksi>> _fut;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -247,9 +329,10 @@ class _TransaksiTabState extends State<_TransaksiTab> {
|
||||
final buy = t.isBuy;
|
||||
return ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: buy ? Colors.green : Colors.red,
|
||||
child: Text(buy ? 'B' : 'J', style: const TextStyle(color: Colors.white)),
|
||||
),
|
||||
backgroundColor:
|
||||
buy ? Colors.green : Colors.red,
|
||||
child: Text(buy ? 'B' : 'J',
|
||||
style: const TextStyle(color: Colors.white))),
|
||||
title: Text('${t.sahamTicker} · ${t.volume} lbr'),
|
||||
subtitle: Text('${t.tanggal} @ ${_fmt(t.harga)} · fee ${_fmt(t.fee)}'),
|
||||
trailing: Text(_fmt(t.total)),
|
||||
@@ -271,23 +354,24 @@ class _ErrorRetry extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) => Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
const Icon(Icons.error_outline, color: Colors.red, size: 40),
|
||||
const SizedBox(height: 8),
|
||||
Text(message, textAlign: TextAlign.center),
|
||||
const SizedBox(height: 12),
|
||||
OutlinedButton(onPressed: onRetry, child: const Text('Coba lagi')),
|
||||
]),
|
||||
),
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
const Icon(Icons.error_outline, color: Colors.red, size: 40),
|
||||
const SizedBox(height: 8),
|
||||
Text(message, textAlign: TextAlign.center),
|
||||
const SizedBox(height: 12),
|
||||
OutlinedButton(onPressed: onRetry, child: const Text('Coba lagi')),
|
||||
])),
|
||||
);
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
String _fmt(double v, {bool force = false}) {
|
||||
final fmt = force
|
||||
? v.toStringAsFixed(0).replaceAllMapped(RegExp(r'\B(?=(\d{3})+(?!\d))'), (m) => '.')
|
||||
: v.toStringAsFixed(0).replaceAllMapped(RegExp(r'\B(?=(\d{3})+(?!\d))'), (m) => '.');
|
||||
: v.toStringAsFixed(0).replaceAllMapped(
|
||||
RegExp(r'\B(?=(\d{3})+(?!\d))'), (m) => '.');
|
||||
return fmt;
|
||||
}
|
||||
|
||||
@@ -296,5 +380,4 @@ String _rm(double v) =>
|
||||
String _formatAbs(double v) {
|
||||
final a = v.abs().toStringAsFixed(0);
|
||||
return a.replaceAllMapped(RegExp(r'\B(?=(\d{3})+(?!\d))'), (m) => '.');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user