【发布时间】:2021-03-23 03:12:44
【问题描述】:
我想问一下flutter,我无法解决代码,我从登录中获取数据,我想放入tuple2,如何将用户名从登录推送到tuple2?任何人都可以帮助我吗?
这是我的登录代码
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:fluttertoast/fluttertoast.dart';
import 'package:testing/admin/portfolio_page.dart';
import 'package:testing/user/portfolio_page.dart';
void main() {
runApp(MyApp());
}
String username = '';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Login Dengan MySql',
home: MyHomePage(),
routes: <String, WidgetBuilder>{
'/PortfolioPage': (BuildContext context) => new PortfolioPage(
username: username,
),
// '/PortfolioPageUser': (BuildContext context) => new PortfolioPageUser(
// username: username,
// ),
'/MyHomePage': (BuildContext context) => new MyHomePage(),
},
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isHidePassword = true;
void _togglePassword() {
setState(() {
_isHidePassword = !_isHidePassword;
});
}
TextEditingController user = new TextEditingController();
TextEditingController pass = new TextEditingController();
Future<List> _login() async {
final response =
await http.post("http://192.168.56.1/basic/loginmultiuser/login.php", body: {
"username": user.text,
"password": pass.text,
});
var datauser = json.decode(response.body);
if (datauser.length == 0) {
setState(() {
tampil();
});
} else {
if (datauser[0]['level'] == 'admin') {
Navigator.pushReplacementNamed(context, '/PortfolioPage');
} else if (datauser[0]['level'] == 'member') {
Navigator.pushReplacementNamed(context, '/MemberPage');
}
setState(() {
username = datauser[0]['username'];
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Login"),
),
body: Padding(
padding: const EdgeInsets.fromLTRB(25, 50, 25, 0),
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(18, 0, 18, 18),
child: TextField(
controller: user,
decoration: InputDecoration(
//hintText: 'Username'
fillColor: Colors.lightGreen[40],
filled: true,
prefixIcon: Icon(
Icons.person,
color: Colors.blue,
),
prefixStyle: TextStyle(
color: Colors.blue, fontWeight: FontWeight.w600),
labelText: "Username",
hintText: 'Username anda',
hintStyle: TextStyle(
color: Colors.black, fontWeight: FontWeight.w200),
labelStyle: TextStyle(
color: Colors.blue, fontWeight: FontWeight.w600),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(18, 0, 18, 0),
child: TextField(
controller: pass,
obscureText: _isHidePassword,
decoration: InputDecoration(
//hintText: 'Password'
fillColor: Colors.lightGreen[40],
filled: true,
prefixIcon: Icon(
Icons.lock,
color: Colors.blue,
),
prefixStyle: TextStyle(
color: Colors.blue, fontWeight: FontWeight.w600),
labelText: "Password",
hintText: 'Password anda',
hintStyle: TextStyle(
color: Colors.black, fontWeight: FontWeight.w200),
labelStyle: TextStyle(
color: Colors.blue, fontWeight: FontWeight.w600),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
suffixIcon: GestureDetector(
onTap: () {
_togglePassword();
},
child: Icon(
_isHidePassword
? Icons.visibility_off
: Icons.visibility,
color: _isHidePassword ? Colors.grey : Colors.blue,
),
),
isDense: true,
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
child: RaisedButton(
child: Text(
"Login",
style: TextStyle(fontSize: 20),
),
padding: EdgeInsets.fromLTRB(60, 10, 60, 10),
textColor: Colors.white,
color: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16.0))),
onPressed: () {
_login();
},
),
),
],
),
),
),
);
}
void tampil() {
Fluttertoast.showToast(
msg: "LOGIN GAGAL",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
}
这是我的 tuple2
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:testing/admin/portfolio_gallery_sub_page.dart';
import 'package:testing/admin/portfolio_tutorials_sub_page.dart';
import 'package:testing/components/portfolio_sliver_app_bar.dart';
import 'package:tuple/tuple.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:tuple/tuple.dart';
class PortfolioPage extends StatefulWidget {
// PortfolioPage({Key key}) : super(key: key);
PortfolioPage({this.username});
final String username;
@override
_PortfolioPageState createState() => _PortfolioPageState();
}
class _PortfolioPageState extends State<PortfolioPage> {
// String data(){
// return widget.username;
// }
// String get username => data();
final List<Tuple2> _pages = [
Tuple2('Tutorials', PortfolioTutorialsSubPage()),
Tuple2('Tutorials', PortfolioTutorialsSubPage()),
Tuple2('Gallery', Admin(username: widget.username)),
];
int _selectedPage = 0;
PageController _pageController = PageController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
PortfolioSliverAppBar(_pages[_selectedPage].item1),
];
},
body: PageView(
children: _pages.map<Widget>((Tuple2 page) => page.item2).toList(),
onPageChanged: (index) {
setState(() {
_selectedPage = index;
});
},
controller: _pageController,
),
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.video_library),
title: Text('Tutorials'),
),
BottomNavigationBarItem(
icon: Icon(Icons.video_library),
title: Text('Tutorials'),
),
BottomNavigationBarItem(
icon: Icon(Icons.image),
title: Text('Gallery'),
),
],
currentIndex: _selectedPage,
onTap: (index) {
setState(() {
_selectedPage = index;
_pageController.animateToPage(_selectedPage,
duration: Duration(milliseconds: 300), curve: Curves.linear);
});
},
),
);
}
}
我的代码不起作用,因为 Tuple2('Gallery', Admin(用户名: widget.username)), 无法获取用户名,如何解决?
【问题讨论】:
标签: android flutter android-studio