我无法在下面的代码中插入导航链接。我想学习如何在第二个页面内插入导航链接
onPressed: () {
// TODO(实现)。 }
ListView窗口小部件:“已批准”,“待定”等是应包含指向其他页面的链接的项目。通过单击FontAwesome.chevronRight
可以访问页面。
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
class AppTab2 extends StatefulWidget {
@override
_AppTab2State createState() => _AppTab2State();
}
class _AppTab2State extends State<AppTab2> {
PageController _pageController = PageController(initialPage: 2);
@override
build(BuildContext context) {
final Map<String, Widget> pages = <String, Widget>{
'My Music': Center(
child: Text('My Music not implemented'),
),
'Shared': Center(
child: Text('Shared not implemented'),
),
'Feed': Feed(),
};
TextTheme textTheme = Theme.of(context).textTheme;
return Stack(
children: [
Container(
decoration: BoxDecoration(
// Gradient color the background
gradient: LinearGradient(
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter,
colors: [
const Color.fromARGB(255, 253, 72, 72),
const Color.fromARGB(255, 87, 97, 249),
],
stops: [0.0, 1.0],
)),
// child: Align(
// alignment: FractionalOffset.bottomCenter,
// child: Container(
// padding: const EdgeInsets.all(10.0),
// child: Text(
// 'Demo',
// style: textTheme.headline.copyWith(
// color: Colors.grey.shade800.withOpacity(0.8),
// fontWeight: FontWeight.bold,
// ),
// ),
// ),
// ),
),
Scaffold(
backgroundColor: Colors.white,
// appBar: AppBar(
// backgroundColor: const Color(0x00000000),
// elevation: 0.0,
// leading: Center(
// child: ClipOval(
// child: Image.network(
// 'http://i.imgur.com/TtNPTe0.jpg',
// ),
// ),
// ),
// actions: [
// IconButton(
// icon: Icon(Icons.add),
// onPressed: () {
// // TODO: implement
// },
// ),
// ],
//// title: const Text('tofu\'s songs'),
// bottom: CustomTabBar(
// pageController: _pageController,
// pageNames: pages.keys.toList(),
// ),
// ),
body: PageView(
controller: _pageController,
children: pages.values.toList(),
),
),
],
);
}
}
class CustomTabBar extends AnimatedWidget implements PreferredSizeWidget {
CustomTabBar({this.pageController, this.pageNames})
: super(listenable: pageController);
final PageController pageController;
final List<String> pageNames;
@override
final Size preferredSize = Size(0.0, 40.0);
@override
Widget build(BuildContext context) {
TextTheme textTheme = Theme.of(context).textTheme;
return Container(
height: 40.0,
margin: const EdgeInsets.all(10.0),
padding: const EdgeInsets.symmetric(horizontal: 20.0),
decoration: BoxDecoration(
color: Colors.grey.shade800.withOpacity(0.5),
borderRadius: BorderRadius.circular(20.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: List.generate(pageNames.length, (int index) {
return InkWell(
child: Text(pageNames[index],
style: textTheme.subhead.copyWith(
fontSize: 30.0,
fontFamily: 'OpenSans',
color: Colors.white.withOpacity(
index == pageController.page ? 1.0 : 0.2,
),
)),
onTap: () {
pageController.animateToPage(
index,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
});
}),
),
);
}
}
class Feed extends StatefulWidget {
@override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
@override
Widget build(BuildContext context) {
return ListView(
children: [
Content(
title: 'Approved',
),
Content(
title: 'Pending',
),
Content(
title: 'Published',
),
Content(
title: 'Withdrawn',
),
Content(
title: 'Declined',
),
Content(
title: 'Content Rights',
),
],
);
}
}
class Content extends StatefulWidget {
const Content({
this.title,
});
final String title;
@override
_ContentState createState() => _ContentState();
}
class _ContentState extends State<Content> {
@override
Widget build(BuildContext context) {
TextTheme textTheme = Theme.of(context).textTheme;
return Container(
margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
decoration: BoxDecoration(
color: Colors.grey.shade300.withOpacity(0.5),
borderRadius: BorderRadius.circular(5.0),
),
child: IntrinsicHeight(
child: Row(
children: <Widget>[
// Container(
// margin:
// const EdgeInsets.only(top: 4.0, bottom: 4.0, right: 10.0),
// child: CircleAvatar(
// backgroundImage: NetworkImage(
// 'http://thecatapi.com/api/images/get?format=src'
// '&size=small&type=jpg#${title.hashCode}'),
// radius: 20.0,
// ),
// ),
SizedBox(
height: 15.0,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(widget.title, style: textTheme.subhead),
],
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Icon(
FontAwesomeIcons.chevronRight,
size: 25.0,
color: Colors.deepPurpleAccent,
),
onTap: () {
// TODO(implement)
},
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
// Icon(Icons.favorite, size: 25.0),
// Text('${likes ?? ''}'),
],
),
onTap: () {
// TODO(implement)
},
),
),
],
),
));
}
}
答案 0 :(得分:2)
您的代码中有一些错误。为了演示,我修改了您的一些代码。
您可以在下面的
代码段
child: ListView(
children: [
Content(
title: 'Approved',
screenPage: Approved(),
),
Content(
title: 'Pending',
screenPage: Pending(),
),
...
class Content extends StatefulWidget {
const Content({
this.title,
this.screenPage,
});
final String title;
final Widget screenPage;
...
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => widget.screenPage),
);
},
完整代码
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Feed(),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class AppTab2 extends StatefulWidget {
@override
_AppTab2State createState() => _AppTab2State();
}
class _AppTab2State extends State<AppTab2> {
PageController _pageController = PageController(initialPage: 2);
@override
build(BuildContext context) {
final Map<String, Widget> pages = <String, Widget>{
'My Music': Center(
child: Text('My Music not implemented'),
),
'Shared': Center(
child: Text('Shared not implemented'),
),
'Feed': Feed(),
};
TextTheme textTheme = Theme.of(context).textTheme;
return Stack(
children: [
Container(
decoration: BoxDecoration(
// Gradient color the background
gradient: LinearGradient(
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter,
colors: [
const Color.fromARGB(255, 253, 72, 72),
const Color.fromARGB(255, 87, 97, 249),
],
stops: [0.0, 1.0],
)),
// child: Align(
// alignment: FractionalOffset.bottomCenter,
// child: Container(
// padding: const EdgeInsets.all(10.0),
// child: Text(
// 'Demo',
// style: textTheme.headline.copyWith(
// color: Colors.grey.shade800.withOpacity(0.8),
// fontWeight: FontWeight.bold,
// ),
// ),
// ),
// ),
),
Scaffold(
backgroundColor: Colors.white,
// appBar: AppBar(
// backgroundColor: const Color(0x00000000),
// elevation: 0.0,
// leading: Center(
// child: ClipOval(
// child: Image.network(
// 'http://i.imgur.com/TtNPTe0.jpg',
// ),
// ),
// ),
// actions: [
// IconButton(
// icon: Icon(Icons.add),
// onPressed: () {
// // TODO: implement
// },
// ),
// ],
//// title: const Text('tofu\'s songs'),
// bottom: CustomTabBar(
// pageController: _pageController,
// pageNames: pages.keys.toList(),
// ),
// ),
body: PageView(
controller: _pageController,
children: pages.values.toList(),
),
),
],
);
}
}
class CustomTabBar extends AnimatedWidget implements PreferredSizeWidget {
CustomTabBar({this.pageController, this.pageNames})
: super(listenable: pageController);
final PageController pageController;
final List<String> pageNames;
@override
final Size preferredSize = Size(0.0, 40.0);
@override
Widget build(BuildContext context) {
TextTheme textTheme = Theme.of(context).textTheme;
return Container(
height: 40.0,
margin: const EdgeInsets.all(10.0),
padding: const EdgeInsets.symmetric(horizontal: 20.0),
decoration: BoxDecoration(
color: Colors.grey.shade800.withOpacity(0.5),
borderRadius: BorderRadius.circular(20.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: List.generate(pageNames.length, (int index) {
return InkWell(
child: Text(pageNames[index],
style: textTheme.subhead.copyWith(
fontSize: 30.0,
fontFamily: 'OpenSans',
color: Colors.white.withOpacity(
index == pageController.page ? 1.0 : 0.2,
),
)),
onTap: () {
pageController.animateToPage(
index,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 300),
);
});
}),
),
);
}
}
class Approved extends StatefulWidget {
@override
_ApprovedState createState() => _ApprovedState();
}
class _ApprovedState extends State<Approved> {
@override
Widget build(BuildContext context) {
return Text('Approved');
}
}
class Pending extends StatefulWidget {
@override
_PendingState createState() => _PendingState();
}
class _PendingState extends State<Pending> {
@override
Widget build(BuildContext context) {
return Text('Pending');
}
}
class Feed extends StatefulWidget {
@override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
@override
Widget build(BuildContext context) {
return Container(
height: 300,
child: ListView(
children: [
Content(
title: 'Approved',
screenPage: Approved(),
),
Content(
title: 'Pending',
screenPage: Pending(),
),
Content(
title: 'Published',
),
Content(
title: 'Withdrawn',
),
Content(
title: 'Declined',
),
Content(
title: 'Content Rights',
),
],
),
);
}
}
class Content extends StatefulWidget {
const Content({
this.title,
this.screenPage,
});
final String title;
final Widget screenPage;
@override
_ContentState createState() => _ContentState();
}
class _ContentState extends State<Content> {
@override
Widget build(BuildContext context) {
TextTheme textTheme = Theme.of(context).textTheme;
return Container(
margin: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 10.0),
decoration: BoxDecoration(
color: Colors.grey.shade300.withOpacity(0.5),
borderRadius: BorderRadius.circular(5.0),
),
child: IntrinsicHeight(
child: Row(
children: <Widget>[
Text("this is Content"),
// Container(
// margin:
// const EdgeInsets.only(top: 4.0, bottom: 4.0, right: 10.0),
// child: CircleAvatar(
// backgroundImage: NetworkImage(
// 'http://thecatapi.com/api/images/get?format=src'
// '&size=small&type=jpg#${title.hashCode}'),
// radius: 20.0,
// ),
// ),
SizedBox(
height: 15.0,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(widget.title, style: textTheme.subhead),
],
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Icon(
FontAwesomeIcons.chevronRight,
size: 25.0,
color: Colors.deepPurpleAccent,
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => widget.screenPage),
);
},
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 5.0),
child: InkWell(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
// Icon(Icons.favorite, size: 25.0),
// Text('${likes ?? ''}'),
],
),
onTap: () {
// TODO(implement)
},
),
),
],
),
));
}
}